首页 > 其他 > 详细

数据结构之线性表(顺序表,单链表)——图书管理系统

时间:2018-04-16 23:40:36      阅读:513      评论:0      收藏:0      [点我收藏+]

顺序表:

代码如下:

  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];                                                            //??请同学在此填写代码 //elem[i-1]单元存储第i个数据元素
 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啦

数据结构之线性表(顺序表,单链表)——图书管理系统

原文:https://www.cnblogs.com/Trojan00/p/8859038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!