數據結構之線性表(順序表,單鏈表)——圖書管理系統


順序表:

代碼如下:

  1 #include<iostream>
  2 #include<fstream>
  3 #include<string>
  4 #include<iomanip>
  5 using namespace std;
  6 #define OK 1
  7 #define ERROR 0
  8 #define OVERFLOW -2
  9 typedef int Status;
 10 typedef int ElemType;
 11 
 12 #define MAXSIZE 100        
 13 struct Book
 14 {
 15     string id;
 16     string name;
 17     double price;
 18 };
 19 typedef struct 
 20 {
 21     Book *elem; 
 22     int length; 
 23 }SqList;
 24 
 25 Status InitList_Sq(SqList &L) 
 26 { 
 27     
 28     L.elem=new Book[MAXSIZE]; 
 29     if (!L.elem)
 30         exit(OVERFLOW); 
 31     L.length = 0; 
 32     return OK;
 33 }
 34 
 35 Status GetElem(SqList L, int i, Book &e) 
 36 {
 37     if (i < 1 || i > L.length)
 38         return ERROR; 
 39     e=L.elem[i-1];                                                       
 40     return OK;
 41 }
 42 
 43 int LocateElem_Sq(SqList L, double e) 
 44 {
 45     for (int i = 0; i < L.length; i++)
 46         if (L.elem[i].price == e)
 47             return i + 1;
 48     return 0;
 49 }
 50 
 51 Status ListInsert_Sq(SqList &L, int i, Book e) 
 52 { 
 53     if ((i < 1) || (i > L.length + 1))
 54         return ERROR; 
 55     if (L.length == MAXSIZE)
 56         return ERROR; 
 57        for(int j=L.length-1;j>=i-1;j--)
 58          L.elem[j+1]=L.elem[j]; 
 59         L.elem[i-1]=e;                                                        
 60     ++L.length; 
 61     return OK;
 62 }
 63 
 64 Status ListDelete_Sq(SqList &L, int i) 
 65 { 
 66     if ((i < 1) || (i > L.length))
 67         return ERROR;
 68     for (int j = i; j < L.length; j++)
 69         L.elem[j - 1] = L.elem[j]; 
 70     --L.length; 
 71     return OK;
 72 }
 73 
 74 int main() {
 75     SqList L;
 76     int i = 0, temp, a, c, choose;
 77     double price;
 78     Book e;
 79     string head_1, head_2, head_3;
 80     cout << "1. 建立\n";
 81     cout << "2. 輸入\n";
 82     cout << "3. 取值\n";
 83     cout << "4. 查找\n";
 84     cout << "5. 插入\n";
 85     cout << "6. 刪除\n";
 86     cout << "7. 輸出\n";
 87     cout << "0. 退出\n\n";
 88 
 89     choose = -1;
 90     while (choose != 0) {
 91         cout << "請選擇:";
 92         cin >> choose;
 93         switch (choose) 
 94                 {
 95         case 1:
 96             if (InitList_Sq(L))
 97                 cout << "成功建立順序表\n\n";
 98             else
 99                 cout << "順序表建立失敗\n\n";
100             break;
101         case 2: {
102             i = 0;
103             L.elem = new Book[MAXSIZE];
104             if (!L.elem)
105                 exit(OVERFLOW);
106             L.length = 0;
107             fstream file;
108             file.open("book.txt");
109             if (!file) {
110                 cout << "錯誤!未找到文件!" << endl;
111                 exit(ERROR);
112             }
113             file >> head_1 >> head_2 >> head_3;
114             while (!file.eof()) {
115                 file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
116                 i++;
117             }
118             cout << "輸入 book.txt 信息完畢\n\n";
119             L.length = i;
120             file.close();
121         }
122             break;
123         case 3:
124             cout << "請輸入一個位置用來取值:\n";
125             cin >> i;
126             temp = GetElem(L, i, e);
127             if (temp != 0) {
128                 cout << "查找成功\n";
129                 cout << "" << i << "本圖書的信息是:\n";
130                 cout << left << setw(15) << e.id << "\t" << left << setw(50)
131                         << e.name << "\t" << left << setw(5) << e.price << endl
132                         << endl;
133             } else
134                 cout << "查找失敗!位置超出范圍\n\n";
135             break;
136         case 4: 
137             cout << "請輸入所要查找價格:";
138             cin >> price;
139             temp = LocateElem_Sq(L, price);
140             if (temp != 0) {
141                 cout << "查找成功\n";
142                 cout << "該價格對應的書名為:" << L.elem[temp - 1].name << endl << endl;
143             } else
144                 cout << "查找失敗!沒有這個價格對應的書籍\n\n";
145             break;
146         case 5: 
147             cout << "請輸入插入的位置和書本信息,包括:編號 書名 價格(用空格隔開):";
148             cin >> a;
149             cin >> e.id >> e.name >> e.price; 
150             if (ListInsert_Sq(L, a, e))
151                 cout << "插入成功.\n\n";
152             else
153                 cout << "插入失敗.\n\n";
154             break;
155         case 6: 
156             cout << "請輸入所要刪除的書籍的位置:";
157             cin >> c;
158             if (ListDelete_Sq(L, c))
159                 cout << "刪除成功.\n\n";
160             else
161                 cout << "刪除失敗.\n\n";
162             break;
163         case 7: 
164             cout << "當前圖書系統信息(順序表)讀出:\n";
165             for (i = 0; i < L.length; i++)
166                 cout << left << setw(15) << L.elem[i].id << "\t" << left
167                         << setw(50) << L.elem[i].name << "\t" << left
168                         << setw(5) << L.elem[i].price << endl;
169             cout << endl;
170             break;
171         }
172     }
173     return 0;
174 }

 

測試結果:

 

 

單鏈表:

代碼如下:

  1 #include<iostream>
  2 #include<string>
  3 #include<iomanip>
  4 #include<fstream>
  5 using namespace std;
  6 #define OK 1
  7 #define ERROR 0
  8 #define OVERFLOW -2
  9 typedef int Status;
 10 
 11 struct Book
 12 {
 13     string id;
 14     string name;
 15     double price;
 16 };
 17 typedef Book ElemType;
 18 typedef struct LNode
 19 {
 20     ElemType data;
 21     struct LNode *next;
 22 } LNode, *LinkList;     
 23 
 24 Status InitList_L(LinkList &L) 
 25 {
 26     L=new LNode;          
 27     L->next=NULL;        
 28     return OK;
 29 }
 30 
 31 Status GetElem_L(LinkList L, int i, Book &e) 
 32 {
 33     int j;
 34     LinkList p;
 35     p=L->next;
 36     j=1;                  
 37     while (j<i && p)      
 38      {
 39         p=p->next;      
 40         ++j;              
 41      }
 42     if(!p|| j>i)
 43         return ERROR;     
 44     e=p->data;
 45     return OK;
 46 }
 47 
 48 LNode *LocateElem_L(LinkList L, int e)
 49    {
 50     LinkList p;
 51     p = L->next;
 52     while(p&&p->data.price!=e)
 53         p=p->next;
 54     return p;
 55    }
 56 
 57 Status ListInsert_L(LinkList &L, int i, Book &e) {
 58     int j;
 59     LinkList p, s;
 60     p = L;
 61     j = 0;
 62     while(p&&(j<i-1))
 63       {
 64           p=p->next;
 65           ++j;
 66       }
 67     if (!p||(j>i-1))
 68           return ERROR;
 69       s = new LNode;
 70       s->data=e;
 71       s->next=p->next;
 72       p->next=s;
 73     return OK;
 74 }
 75 
 76 Status ListDelete_L(LinkList &L, int i) 
 77 {
 78       LinkList p, q;
 79       int j;
 80       p = L;
 81       j = 0;
 82     while((p->next)&&(j < i - 1))
 83     {
 84       p=p->next;
 85       ++j;                           
 86     }
 87     if (!(p->next)||(j>i-1))
 88       return ERROR;
 89       q=p->next;
 90       p->next=q->next;
 91       delete q;
 92     return OK;
 93 }
 94 
 95 
 96 
 97 int main() {
 98     int a,n,choose;
 99     double price;
100     Book e;
101     LinkList L,p;
102     string head_1, head_2, head_3;
103     cout<<"1. 建立\n";
104     cout<<"2. 輸入\n";
105     cout<<"3. 取值\n";
106     cout<<"4. 查找\n";
107     cout<<"5. 插入\n";
108     cout<<"6. 刪除\n";
109     cout<<"7. 輸出\n";
110     cout<<"0. 退出\n\n";
111 
112     choose = -1;
113     while (choose != 0)
114     {
115         cout<<"請選擇:";
116         cin >>choose;
117         switch (choose)
118         {
119         case 1:                       
120             if (InitList_L(L))
121                 cout<<"成功建立鏈表!\n\n";
122             break;
123         case 2:  
124      {
125         L = new LNode;
126         L->next = NULL; 
127         int length = 0;                 
128         fstream file;
129         file.open("book.txt");
130         if (!file) 
131         {
132             cout << "未找到相關文件,無法打開!" << endl;
133                 exit(ERROR);
134         }
135         file >> head_1 >> head_2 >> head_3;
136         while (!file.eof()) 
137         {
138             p = new LNode; 
139             file >> p->data.id >> p->data.name >> p->data.price; 
140             p->next = L->next;
141             L->next = p; 
142             length++;
143         }
144         file.close();
145     }
146             break; 
147         case 3:                 
148             cout<<"請輸入一個位置用來查找:";
149             cin >>a;
150             if(GetElem_L(L,a,e))
151                 {
152                 cout<<"查找成功\n";
153                 cout<<""<<a<<"本圖書的信息是:\n";
154                 cout<<left << setw (15) <<e.id<<"\t"<<left<<setw (50)
155                         <<e.name<<"\t"<<left<<setw (5)<<e.price<<endl
156                         <<endl;
157                 }
158             else
159                 cout<<"查找失敗\n\n";
160               break;
161         case 4:               
162             cout<<"請輸入所要查找的價格:";
163             cin >> price;
164             if (LocateElem_L(L, price)!=NULL)
165                 {
166                 cout<<"查找成功\n";
167                 cout<<"該價格對應的書名:"<<LocateElem_L(L, price)->data.name
168                         << endl << endl;
169                 }
170             else
171                 cout<<"查找失敗!定價"<<price<<" 沒有找到\n\n";
172             break;
173         case 5:                       
174             cout<<"請輸入插入的位置和書的信息,包括:編號 書名 價格(用空格隔開):";
175             cin >>a;
176             cin >>e.id>>e.name>>e.price;
177             if (ListInsert_L(L,a,e))
178                 cout<<"插入成功.\n\n";
179             else
180                 cout<<"插入失敗!\n\n";
181             break;
182         case 6:                      
183             cout<<"請輸入所要刪除的書籍位置:";
184             cin >>a;
185             if (ListDelete_L(L, a))
186                 cout<<"刪除成功!\n\n";
187             else
188                 cout<<"刪除失敗!\n\n";
189             break;
190         case 7:                    
191             cout << "當前圖書系統信息(鏈表)讀出:\n";
192             p=L->next;
193             while(p)
194             {
195             cout<<left<<setw(15)<<p->data.id<<"\t"<<left<<setw(
196                         50)<<p->data.name<<"\t"<<left<<setw(5)
197                         <<p->data.price<<endl;
198                    p=p->next;
199             }
200             cout<<endl;
201             break;
202         }
203     }
204         return 0;
205 }

 

測試結果:

 

     當然啦,只有這些代碼是不能運行的,得把book.txt文件與這個代碼所存儲的文件放到一個文件夾,然后運行

代碼就OK啦!

book.txt

ISBN                              書名                                   定價
9787302257646            程序設計基礎                    25
9787302219972            單片機技術及應用             32
9787302203513            編譯原理                           46
9787811234923            匯編語言程序設計教程      21
9787512100831            計算機操作系統                17
9787302265436            計算機導論實驗指導         18
9787302180630            實用數據結構                    29
9787302225065            數據結構(C語言版)       38
9787302171676           C#面向對象程序設計         39
9787302250692           C語言程序設計                  42
9787302150664           數據庫原理                        35 
9787302260806           Java編程與實踐                 56
9787302252887           Java程序設計與應用教程   39
9787302198505           嵌入式操作系統及編程       25
9787302169666           軟件測試                            24
9787811231557           Eclipse基礎與應用             35

把上面的那個類似表格的東西復制到記事本里,然后把記事本重命名為book.txt就ok啦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM