C++順序表實現圖書管理系統
本文為大家分享了C++順序表實現圖書管理系統的具體代碼,供大家參考,具體內容如下
圖書信息表包括以下10項常用的基本操作:圖書信息表的創(chuàng)建和輸出、排序、修改、逆序存儲、最貴圖書的查找、最愛圖書的查找、最佳位置圖書的查找、新圖書的入庫、舊圖書的出庫、圖書去重。
代碼:
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//函數結果狀態(tài)代碼
#define OK 1
#define ERROR 0
#define OVERFLOW -2
//Status是函數返回值類型,其值是函數結果狀態(tài)代碼
typedef int Status;
#define MAXSIZE 100
struct BOOK
{
string ib;//isbn
string name;//名字
float price;//價格
};
typedef struct
{
BOOK *elem;//存儲空間的基地址
int length; //當前長度
} SqList;
//1、創(chuàng)建一個圖書信息表
Status CreateTheList(SqList &L)
{
L.elem = new BOOK[MAXSIZE];//為順序表分配一個大小為MAXSIZE的數組空間
if(!L.elem) exit(OVERFLOW);//存儲分配失敗退出
L.length = 0;//將長度初始化為0
return OK;
}
//3.根據圖書價格對圖書信息表進行降序排序
Status SortTheList(SqList &L)
{
//讓每一個數據與其后面的價格都進行對比,若價格小于其后面的便進行交換,以保證價格小的在后面
for(int i=0;i<L.length;++i)
{
for(int j=i+1;j<L.length;++j)
{
if(L.elem[i].price<L.elem[j].price)
{
BOOK temp;
temp=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=temp;
}
}
}
cout<<"排序成功"<<endl;
}
//4.修改圖書信息表
Status ChangeTheList(SqList &L)
{
//求平均值
float total=0;
for(int i=0;i<L.length;i++)
{
total += L.elem[i].price;
}
float ave=total/L.length;
cout<<"平均價格為:"<<ave<<endl;
//修改每本圖書的價格
for(int i=0;i<L.length;i++)
{
if(L.elem[i].price>ave)
L.elem[i].price *= 1.1;
else
L.elem[i].price *= 1.2;
}
cout<<"修改成功"<<endl;
}
//5.實現該圖書信息表的逆序存儲
Status ReverseTheList(SqList &L)
{
//將第一個元素與最后一個交換位置,第二個與倒數第二個交換,以此類推
for(int i=0;i<L.length/2;i++)
{
BOOK temp;
temp = L.elem[i];
L.elem[i] = L.elem[L.length-1-i];
L.elem[L.length-i-1] = temp;
}
cout<<"逆序存儲成功"<<endl;
}
//6.查找圖書信息表中最貴的圖書
Status TheExpensiveBook(SqList &L)
{
//先找最貴的價格
float max=0;
int i,number=0;
for(i=0;i<L.length;i++)
{
if(L.elem[i].price>max)
max=L.elem[i].price;
}
cout<<"圖書最貴的價格為:"<<max<<endl;
//再遍歷圖書表尋找價格等于max的圖書并輸出
for(i=0;i<L.length;i++)
{
if(L.elem[i].price == max)
{
number++;
cout<<setiosflags(ios::fixed);//設置小數點的位數
cout<<L.elem[i].ib<<" "<<L.elem[i].name<<" "<<setprecision(2)<<L.elem[i].price<<endl;
}
}
cout<<"最貴的圖書數量為:"<<number<<endl;
}
//7.查找最愛的圖書
Status TheFavouriteBook(SqList &L)
{
int x;
cout<<"請輸入要查找的次數:"<<endl;
cin>>x;//要找的書的數量
for(int i=1;i<=x;i++)
{
cout<<"請輸入要查找的書名:"<<endl;
string name1;
cin>>name1;//輸入書名
int number=0;//記錄與輸入的書名相同的書的數量
for(int j=0;j<L.length;j++)
{
if(L.elem[j].name == name1)
{
number++;
cout<<setiosflags(ios::fixed);//設置小數點的位數
cout<<L.elem[j].ib<<" "<<L.elem[j].name<<" "<<setprecision(2)<<L.elem[j].price<<endl;
}
}
if(number==0)
{
cout<<"抱歉,沒有你的最愛!"<<endl;
}
else
{
cout<<"第"<<i<<"次輸入的書名對應的書有:"<<number<<"本"<<endl;
}
}
}
//8.查找最佳位置上的圖書
Status TheBestPosition(SqList &L)
{
int x;
cout<<"請輸入要查找的次數:"<<endl;
cin>>x;//要查找的次數
for(int i=1;i<=x;i++)
{
cout<<"請輸入要查找的位置:"<<endl;
int pos;
cin>>pos;//輸入位置
if(pos<=0 || pos>L.length)
{
cout<<"抱歉,最佳位置上的圖書不存在!"<<endl;
}
else
{
cout<<setiosflags(ios::fixed);//設置小數點的位數
cout<<L.elem[pos-1].ib<<" "<<L.elem[pos-1].name<<" "<<setprecision(2)<<L.elem[pos-1].price<<endl;
}
}
}
//9.新圖書入庫
Status InsertABook(SqList &L)
{
cout<<"輸入要插入的位置:"<<endl;
int x;
cin>>x;//輸入要插入的位置
//判斷位置是否合法
if(x<1 || x>L.length+1)
{
cout<<"抱歉,入庫位置非法!"<<endl;
}
else
{
cout<<"輸入要插入的圖書信息:"<<endl;
string ib1;
cin>>ib1;
string name1;
cin>>name1;
float price1;
cin>>price1;
BOOK e;//e包含要插入的書的信息
e.ib = ib1;
e.name = name1;
e.price = price1;
//將插入位置之后的元素后移一位
for(int j=L.length-1;j>=x-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[x-1]=e;//插入元素
L.length +=1;//表長加一
cout<<"插入成功"<<endl;
}
}
//10.舊圖書出庫
Status DeleteABook(SqList &L,int x)
{
//判斷位置是否合法
if(x<1 || x>L.length)
{
cout<<"抱歉,出庫位置非法!"<<endl;
}
else
{
//被刪除元素之后的元素前移一位
for(int j=x;j<=L.length-1;j++)
{
L.elem[j-1]=L.elem[j];
}
L.length -=1;//表長減一
cout<<"刪除成功"<<endl;
}
}
//11.圖書信息表去重
Status DeleteTheRepeat(SqList &L)
{
int i,j;
for(i=0;i<L.length;i++)
{
for(j=i+1;j<L.length;j++)
{
if(L.elem[i].ib == L.elem[j].ib)
{
DeleteABook(L,j+1);
j=j-1;
}
}
}
cout<<"去重完成"<<endl;
cout<<"(輸出刪除成功表示信息表中有重復的書籍存在)"<<endl;
}
//12.輸出圖書信息表
Status PrintTheList(SqList &L)
{
int i=0;
cout<<"圖書的數量為:"<<L.length<<endl;
for(;i<L.length;i++)
{
cout<<setiosflags(ios::fixed);//設置小數點的位數
cout<<L.elem[i].ib<<" "<<L.elem[i].name<<" "<<setprecision(2)<<L.elem[i].price<<endl;
}
}
int main()
{
SqList l;
while(true){
cout<<"歡迎使用圖書管理系統"<<endl;
cout<<"1.創(chuàng)建一個圖書信息表"<<endl;
cout<<"2.錄入圖書信息"<<endl;
cout<<"3.根據圖書價格對圖書信息表進行降序排序"<<endl;
cout<<"4.修改圖書信息表,修改規(guī)則如下:"<<endl;
cout<<"計算所有圖書的平均價格,將所有低于平均價格的圖書價格提高20%,所有高于或等于平均價格的圖書價格提高10%,最后打印該表"<<endl;
cout<<"5.實現該圖書信息表的逆序存儲"<<endl;
cout<<"6.查找圖書信息表中最貴的圖書"<<endl;
cout<<"7.查找最愛的圖書"<<endl;
cout<<"8.查找最佳位置上的圖書"<<endl;
cout<<"9.新圖書入庫"<<endl;
cout<<"10.舊圖書出庫"<<endl;
cout<<"11.圖書信息表去重"<<endl;
cout<<"12.輸出圖書信息表"<<endl;
cout<<"13.退出系統"<<endl;
cout<<"請輸入您的選擇:"<<endl;
int x,i=0;
cin>>x;
switch(x){
case 1:
if(CreateTheList(l))
cout<<"創(chuàng)建成功"<<endl;
else
cout<<"創(chuàng)建失敗"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
break;
case 2:
cout<<"請輸入圖書的信息:"<<endl;
while(true){
//輸入圖書的信息
string ib1;
cin>>ib1;
string name1;
cin>>name1;
float price1;
cin>>price1;
//判斷是不是輸入結束
if(ib1 =="0" && name1 =="0" && price1 == 0)
{
break;
}
//向后插入讀入的圖書信息
l.elem[i].ib = ib1;
l.elem[i].name = name1;
l.elem[i].price = price1;
i++;//圖書數量加一
}
l.length = i;
cout<<"錄入完畢"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
break;
case 3:
SortTheList(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 4:
ChangeTheList(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 5:
ReverseTheList(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 6:
TheExpensiveBook(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 7:
TheFavouriteBook(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 8:
TheBestPosition(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 9:
InsertABook(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 10:
cout<<"輸入要刪除的書籍的位置:"<<endl;
int x;
cin>>x;//輸入要刪除的位置
DeleteABook(l,x);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 11:
DeleteTheRepeat(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 12:
PrintTheList(l);
cout<<"-------------------------------------------------------------"<<endl;
break;
case 13:
exit(0);
}
}
}
測試數據:
9787302257646 程序設計基礎 25.00
9787302164340 程序設計基礎第二版 20.00
9787302219972 單片機技術及應用 32.00
9787302219972 單片機技術及應用 32.00
9787302203513 單片機原理與技術應用 26.00
9787810827430 工業(yè)計算機控制技術原理與應用 31.00
9787811234923 匯編語言程序設計教程 21.00
9787811234923 匯編語言程序設計教程 21.00
運行結果:
創(chuàng)建與輸出

對圖書價格進行降序排序

修改圖書價格

實現逆序存儲

查找最貴圖書

查找最愛的圖書

查找最佳位置的圖書

新圖書入庫


舊圖書出庫


圖書信息去重(根據ISBN去重)
去重前書籍信息

去重后

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C語言MultiByteToWideChar和WideCharToMultiByte案例詳解
這篇文章主要介紹了C語言MultiByteToWideChar和WideCharToMultiByte案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

