1. 线性表之应用(顺序存储结构)
list.h:
对线性表的各种操作函数
1void InsertRear(List& L,const ElemType& item)
2{
3 if(L.size==MaxSize)
4 {
5 cerr<<"List overfolw!"<<endl;
6 exit(1);
7 }
8 L.list[L.size]=item;
9 L.size++;
10}
11ElemType GetElem(List& L,int pos)
12{
13 if(pos<1||pos>L.size)
14 {
15 cerr<<"pos is out range!"<<endl;
16 exit(1);
17 }
18 return L.list[pos-1];
19}
20void InitList(List& L)
21{
22 L.size=0;
23}
24int ListSize(List& L)
25{
26 return L.size;
27}
28void TraverseList(List& L)
29{
30 for(int i=0;i<L.size;i++)
31 cout<<L.list[i];
32 cout<<endl;
33}
34int Find(List& L,ElemType& item)
35{
36 for(int i=0;i<L.size;i++)
37 if(L.list[i]==item)
38 {
39 item=L.list[i];
40 return 1;
41 }
42 return 0;
43}
44int Update(List& L,const ElemType& item)
45{
46 for(int i=0;i<L.size;i++)
47 if(L.list[i]==item)
48 {
49 L.list[i]=item;
50 return 1;
51 }
52 return 0;
53}
54int Delete(List& L,const ElemType& item)
55{
56 if(L.size==0)
57 {
58 cerr<<"L is an empty list!"<<endl;
59 return 0;
60 }
61 for(int i=0;i<L.size;i++)
62 if(L.list[i]==item)
63 break;
64 if(i==L.size)
65 {
66 cerr<<"Delete element is not exist!"<<endl;
67 return 0;
68 }
69 for(int j=i+1;j<L.size;j++)
70 L.list[j-1]=L.list[j];
71 L.size--;
72 return 1;
73}
74void Sort(List& L)
75{
76 int i,j;
77 ElemType x;
78 for(i=0;i<L.size;i++)
79 {
80 x=L.list[i];
81 for(j=i-1;j>=0;j--)
82 if(x<L.list[j])
83 L.list[j+1]=L.list[j];
84 else
85 break;
86 L.list[j+1]=x;
87 }
88}
1void InsertRear(List& L,const ElemType& item)
2{
3 if(L.size==MaxSize)
4 {
5 cerr<<"List overfolw!"<<endl;
6 exit(1);
7 }
8 L.list[L.size]=item;
9 L.size++;
10}
11ElemType GetElem(List& L,int pos)
12{
13 if(pos<1||pos>L.size)
14 {
15 cerr<<"pos is out range!"<<endl;
16 exit(1);
17 }
18 return L.list[pos-1];
19}
20void InitList(List& L)
21{
22 L.size=0;
23}
24int ListSize(List& L)
25{
26 return L.size;
27}
28void TraverseList(List& L)
29{
30 for(int i=0;i<L.size;i++)
31 cout<<L.list[i];
32 cout<<endl;
33}
34int Find(List& L,ElemType& item)
35{
36 for(int i=0;i<L.size;i++)
37 if(L.list[i]==item)
38 {
39 item=L.list[i];
40 return 1;
41 }
42 return 0;
43}
44int Update(List& L,const ElemType& item)
45{
46 for(int i=0;i<L.size;i++)
47 if(L.list[i]==item)
48 {
49 L.list[i]=item;
50 return 1;
51 }
52 return 0;
53}
54int Delete(List& L,const ElemType& item)
55{
56 if(L.size==0)
57 {
58 cerr<<"L is an empty list!"<<endl;
59 return 0;
60 }
61 for(int i=0;i<L.size;i++)
62 if(L.list[i]==item)
63 break;
64 if(i==L.size)
65 {
66 cerr<<"Delete element is not exist!"<<endl;
67 return 0;
68 }
69 for(int j=i+1;j<L.size;j++)
70 L.list[j-1]=L.list[j];
71 L.size--;
72 return 1;
73}
74void Sort(List& L)
75{
76 int i,j;
77 ElemType x;
78 for(i=0;i<L.size;i++)
79 {
80 x=L.list[i];
81 for(j=i-1;j>=0;j--)
82 if(x<L.list[j])
83 L.list[j+1]=L.list[j];
84 else
85 break;
86 L.list[j+1]=x;
87 }
88}
warehouse_manage.cpp:
商品管理
1#include"iostream.h"
2#include"stdlib.h"
3#include"iomanip.h"
4#include"string.h"
5#include"fstream.h"
6const int MaxSize=10;
7struct goods
8{
9 char code[5]; //商品代码
10 char name[15]; //商品名称
11 int minq; //最低库存量
12 int curq; //当前库存量
13};
14
15typedef goods ElemType;
16struct List
17{
18 ElemType list[MaxSize];
19 int size;
20};
21
22int operator ==(const ElemType& e1,const ElemType& e2)
23{
24 return (strcmp(e1.code,e2.code)==0);
25}
26int operator <(const ElemType& e1,const ElemType& e2)
27{
28 return (strcmp(e1.code,e2.code)==-1);
29}
30ostream& operator <<(ostream& ostr,const ElemType& x)
31{
32 ostr<<x.code<<setw(12)<<x.name<<setw(4)<<x.minq<<setw(4)<<x.curq<<endl;
33 return ostr;
34}
35
36#include"list.h"
37/**//*
38void InsertRear(List& L,const ElemType& item)
39{
40 if(L.size==MaxSize)
41 {
42 cerr<<"List overfolw!"<<endl;
43 exit(1);
44 }
45 L.list[L.size]=item;
46 L.size++;
47}
48ElemType GetElem(List& L,int pos)
49{
50 if(pos<1||pos>L.size)
51 {
52 cerr<<"pos is out range!"<<endl;
53 exit(1);
54 }
55 return L.list[pos-1];
56}
57void InitList(List& L)
58{
59 L.size=0;
60}
61int ListSize(List& L)
62{
63 return L.size;
64}
65void TraverseList(List& L)
66{
67 for(int i=0;i<L.size;i++)
68 cout<<L.list[i];
69 cout<<endl;
70}
71int Find(List& L,ElemType& item)
72{
73 for(int i=0;i<L.size;i++)
74 if(L.list[i]==item)
75 {
76 item=L.list[i];
77 return 1;
78 }
79 return 0;
80}
81int Update(List& L,const ElemType& item)
82{
83 for(int i=0;i<L.size;i++)
84 if(L.list[i]==item)
85 {
86 L.list[i]=item;
87 return 1;
88 }
89 return 0;
90}
91int Delete(List& L,const ElemType& item)
92{
93 if(L.size==0)
94 {
95 cerr<<"L is an empty list!"<<endl;
96 return 0;
97 }
98 for(int i=0;i<L.size;i++)
99 if(L.list[i]==item)
100 break;
101 if(i==L.size)
102 {
103 cerr<<"Delete element is not exist!"<<endl;
104 return 0;
105 }
106 for(int j=i+1;j<L.size;j++)
107 L.list[j-1]=L.list[j];
108 L.size--;
109 return 1;
110}
111*/
112//---------------------------------------------------------
113void SetupGoddsFile(char *fname)
114{
115 ofstream ofstr(fname);
116 if(!ofstr)
117 {
118 cerr<<"File 'goods' no create!"<<endl;
119 exit(1);
120 }
121 char a[30];
122 for(int i=0;i<6;i++)
123 {
124 cin.getline(a,30);//每循环一次从键盘上读入一条商品记录
125 //(即一行数据,每个域值用空格分开,最后以按下回车键结束)到字符数组a中,然后再把它写入文件中
126 ofstr<<a<<endl;
127 }
128 ofstr.close();
129}
130
131void SetupGoodsList(List& L,char *fname)
132{
133 ifstream ifstr(fname,ios::in|ios::nocreate);
134 if(!ifstr)
135 {
136 cerr<<"File 'goods' not found!"<<endl;
137 exit(1);
138 }
139 goods g;
140 while(ifstr>>g.code)
141 {
142 ifstr>>g.name>>g.minq>>g.curq;
143 InsertRear(L,g);
144 }
145 ifstr.close();
146}
147void WriteGoodsFile(char* fname,List& L)
148{
149 ofstream ofstr(fname);
150 if(!ofstr)
151 {
152 cerr<<"File 'goods' no create!"<<endl;
153 exit(1);
154 }
155 goods g;
156 int n=ListSize(L);
157 for(int i=0;i<=n;i++)
158 {
159 g=GetElem(L,i);
160 ofstr<<g.code<<" "<<g.name<<" "<<g.minq<<" "<<g.curq<<endl;
161 }
162 ofstr.close();
163}
164void main()
165{
166 char *FileGoods="D:goods.dat";
167 SetupGoddsFile(FileGoods);
168 List L2; //说明一个线性表L2
169 InitList(L2);
170 SetupGoodsList(L2,FileGoods);//把文件D:goods.dat”中的记录顺序读到线性表L2中
171 int i,flag=1;
172 while(flag)
173 {
174 cout<<"1 打印整个库存表"<<endl;
175 cout<<"2 修改库存表中的记录"<<endl;
176 cout<<"3 删除库存表中的记录"<<endl;
177 cout<<"4 对库存表排序"<<endl;
178 cout<<"5 结束处理过程"<<endl;
179 cout<<"输入你的选择:";
180 cin>>i;
181 while(i<1||i>5)
182 {
183 cout<<"请重新输入选择(1-5):";
184 cin>>i;
185 }
186 cout<<endl;
187 switch(i)
188 {
189 case 1: //打印
190 TraverseList(L2);
191 break;
192 case 2: //修改
193 goods g;
194 int x;
195 cout<<"输入待修改的商品代号:";
196 cin>>g.code;
197 if(Find(L2,g))
198 {
199 cout<<"输入该商品的修正量:";
200 cin>>x;
201 g.curq+=x;
202 Update(L2,g);
203 }
204 else
205 {
206 cout<<"输入新商品记录的其余字段的内容:"<<endl;
207 cin>>g.name>>g.minq>>g.curq;
208 InsertRear(L2,g);
209 }
210 break;
211 case 3: //删除
212 cout<<"输入待删除商品的商品代号:";
213 cin>>g.code;
214 Delete(L2,g);
215 break;
216 case 4: //排序
217 Sort(L2);
218 break;
219 case 5: //结束
220 cout<<"本次处理结束,再见!"<<endl;
221 flag=0;
222 }
223 }
224 WriteGoodsFile(FileGoods,L2);
225}
226/**//*
227Y-12 toothbrush 10 25
228F-13 soap 20 48
229W-01 toiletpaper 10 36
230M-48 towel 15 90
231C-24 chinacup 10 52
232S-05 schoolbag 5 20
2331 打印整个库存表
2342 修改库存表中的记录
2353 删除库存表中的记录
2364 对库存表排序
2375 结束处理过程
238输入你的选择:1
239
240Y-12 toothbrush 10 25
241F-13 soap 20 48
242W-01 toiletpaper 10 36
243M-48 towel 15 90
244C-24 chinacup 10 52
245S-05 schoolbag 5 20
246
2471 打印整个库存表
2482 修改库存表中的记录
2493 删除库存表中的记录
2504 对库存表排序
2515 结束处理过程
252输入你的选择:2
253
254输入待修改的商品代号:W-01
255输入该商品的修正量:10
2561 打印整个库存表
2572 修改库存表中的记录
2583 删除库存表中的记录
2594 对库存表排序
2605 结束处理过程
261输入你的选择:1
262
263Y-12 toothbrush 10 25
264F-13 soap 20 48
265W-01 toiletpaper 10 46
266M-48 towel 15 90
267C-24 chinacup 10 52
268S-05 schoolbag 5 20
269
2701 打印整个库存表
2712 修改库存表中的记录
2723 删除库存表中的记录
2734 对库存表排序
2745 结束处理过程
275输入你的选择:2
276
277输入待修改的商品代号:D-20
278输入新商品记录的其余字段的内容:
279pencil 40 80
2801 打印整个库存表
2812 修改库存表中的记录
2823 删除库存表中的记录
2834 对库存表排序
2845 结束处理过程
285输入你的选择:1
286
287Y-12 toothbrush 10 25
288F-13 soap 20 48
289W-01 toiletpaper 10 46
290M-48 towel 15 90
291C-24 chinacup 10 52
292S-05 schoolbag 5 20
293D-20 pencil 40 80
294
2951 打印整个库存表
2962 修改库存表中的记录
2973 删除库存表中的记录
2984 对库存表排序
2995 结束处理过程
300输入你的选择:3
301
302输入待删除商品的商品代号:M-48
3031 打印整个库存表
3042 修改库存表中的记录
3053 删除库存表中的记录
3064 对库存表排序
3075 结束处理过程
308输入你的选择:1
309
310Y-12 toothbrush 10 25
311F-13 soap 20 48
312W-01 toiletpaper 10 46
313C-24 chinacup 10 52
314S-05 schoolbag 5 20
315D-20 pencil 40 80
316
3171 打印整个库存表
3182 修改库存表中的记录
3193 删除库存表中的记录
3204 对库存表排序
3215 结束处理过程
322输入你的选择:4
323
3241 打印整个库存表
3252 修改库存表中的记录
3263 删除库存表中的记录
3274 对库存表排序
3285 结束处理过程
329输入你的选择:1
330
331C-24 chinacup 10 52
332D-20 pencil 20 40
333F-13 soap 20 48
334S-05 schoolbag 5 20
335W-01 toiletpaper 10 56
336Y-12 toothbrush 10 25
337
3381 打印整个库存表
3392 修改库存表中的记录
3403 删除库存表中的记录
3414 对库存表排序
3425 结束处理过程
343输入你的选择:5
344
345本次处理结束,再见!
346pos is out range!
347Press any key to continue
348*/
1#include"iostream.h"
2#include"stdlib.h"
3#include"iomanip.h"
4#include"string.h"
5#include"fstream.h"
6const int MaxSize=10;
7struct goods
8{
9 char code[5]; //商品代码
10 char name[15]; //商品名称
11 int minq; //最低库存量
12 int curq; //当前库存量
13};
14
15typedef goods ElemType;
16struct List
17{
18 ElemType list[MaxSize];
19 int size;
20};
21
22int operator ==(const ElemType& e1,const ElemType& e2)
23{
24 return (strcmp(e1.code,e2.code)==0);
25}
26int operator <(const ElemType& e1,const ElemType& e2)
27{
28 return (strcmp(e1.code,e2.code)==-1);
29}
30ostream& operator <<(ostream& ostr,const ElemType& x)
31{
32 ostr<<x.code<<setw(12)<<x.name<<setw(4)<<x.minq<<setw(4)<<x.curq<<endl;
33 return ostr;
34}
35
36#include"list.h"
37/**//*
38void InsertRear(List& L,const ElemType& item)
39{
40 if(L.size==MaxSize)
41 {
42 cerr<<"List overfolw!"<<endl;
43 exit(1);
44 }
45 L.list[L.size]=item;
46 L.size++;
47}
48ElemType GetElem(List& L,int pos)
49{
50 if(pos<1||pos>L.size)
51 {
52 cerr<<"pos is out range!"<<endl;
53 exit(1);
54 }
55 return L.list[pos-1];
56}
57void InitList(List& L)
58{
59 L.size=0;
60}
61int ListSize(List& L)
62{
63 return L.size;
64}
65void TraverseList(List& L)
66{
67 for(int i=0;i<L.size;i++)
68 cout<<L.list[i];
69 cout<<endl;
70}
71int Find(List& L,ElemType& item)
72{
73 for(int i=0;i<L.size;i++)
74 if(L.list[i]==item)
75 {
76 item=L.list[i];
77 return 1;
78 }
79 return 0;
80}
81int Update(List& L,const ElemType& item)
82{
83 for(int i=0;i<L.size;i++)
84 if(L.list[i]==item)
85 {
86 L.list[i]=item;
87 return 1;
88 }
89 return 0;
90}
91int Delete(List& L,const ElemType& item)
92{
93 if(L.size==0)
94 {
95 cerr<<"L is an empty list!"<<endl;
96 return 0;
97 }
98 for(int i=0;i<L.size;i++)
99 if(L.list[i]==item)
100 break;
101 if(i==L.size)
102 {
103 cerr<<"Delete element is not exist!"<<endl;
104 return 0;
105 }
106 for(int j=i+1;j<L.size;j++)
107 L.list[j-1]=L.list[j];
108 L.size--;
109 return 1;
110}
111*/
112//---------------------------------------------------------
113void SetupGoddsFile(char *fname)
114{
115 ofstream ofstr(fname);
116 if(!ofstr)
117 {
118 cerr<<"File 'goods' no create!"<<endl;
119 exit(1);
120 }
121 char a[30];
122 for(int i=0;i<6;i++)
123 {
124 cin.getline(a,30);//每循环一次从键盘上读入一条商品记录
125 //(即一行数据,每个域值用空格分开,最后以按下回车键结束)到字符数组a中,然后再把它写入文件中
126 ofstr<<a<<endl;
127 }
128 ofstr.close();
129}
130
131void SetupGoodsList(List& L,char *fname)
132{
133 ifstream ifstr(fname,ios::in|ios::nocreate);
134 if(!ifstr)
135 {
136 cerr<<"File 'goods' not found!"<<endl;
137 exit(1);
138 }
139 goods g;
140 while(ifstr>>g.code)
141 {
142 ifstr>>g.name>>g.minq>>g.curq;
143 InsertRear(L,g);
144 }
145 ifstr.close();
146}
147void WriteGoodsFile(char* fname,List& L)
148{
149 ofstream ofstr(fname);
150 if(!ofstr)
151 {
152 cerr<<"File 'goods' no create!"<<endl;
153 exit(1);
154 }
155 goods g;
156 int n=ListSize(L);
157 for(int i=0;i<=n;i++)
158 {
159 g=GetElem(L,i);
160 ofstr<<g.code<<" "<<g.name<<" "<<g.minq<<" "<<g.curq<<endl;
161 }
162 ofstr.close();
163}
164void main()
165{
166 char *FileGoods="D:goods.dat";
167 SetupGoddsFile(FileGoods);
168 List L2; //说明一个线性表L2
169 InitList(L2);
170 SetupGoodsList(L2,FileGoods);//把文件D:goods.dat”中的记录顺序读到线性表L2中
171 int i,flag=1;
172 while(flag)
173 {
174 cout<<"1 打印整个库存表"<<endl;
175 cout<<"2 修改库存表中的记录"<<endl;
176 cout<<"3 删除库存表中的记录"<<endl;
177 cout<<"4 对库存表排序"<<endl;
178 cout<<"5 结束处理过程"<<endl;
179 cout<<"输入你的选择:";
180 cin>>i;
181 while(i<1||i>5)
182 {
183 cout<<"请重新输入选择(1-5):";
184 cin>>i;
185 }
186 cout<<endl;
187 switch(i)
188 {
189 case 1: //打印
190 TraverseList(L2);
191 break;
192 case 2: //修改
193 goods g;
194 int x;
195 cout<<"输入待修改的商品代号:";
196 cin>>g.code;
197 if(Find(L2,g))
198 {
199 cout<<"输入该商品的修正量:";
200 cin>>x;
201 g.curq+=x;
202 Update(L2,g);
203 }
204 else
205 {
206 cout<<"输入新商品记录的其余字段的内容:"<<endl;
207 cin>>g.name>>g.minq>>g.curq;
208 InsertRear(L2,g);
209 }
210 break;
211 case 3: //删除
212 cout<<"输入待删除商品的商品代号:";
213 cin>>g.code;
214 Delete(L2,g);
215 break;
216 case 4: //排序
217 Sort(L2);
218 break;
219 case 5: //结束
220 cout<<"本次处理结束,再见!"<<endl;
221 flag=0;
222 }
223 }
224 WriteGoodsFile(FileGoods,L2);
225}
226/**//*
227Y-12 toothbrush 10 25
228F-13 soap 20 48
229W-01 toiletpaper 10 36
230M-48 towel 15 90
231C-24 chinacup 10 52
232S-05 schoolbag 5 20
2331 打印整个库存表
2342 修改库存表中的记录
2353 删除库存表中的记录
2364 对库存表排序
2375 结束处理过程
238输入你的选择:1
239
240Y-12 toothbrush 10 25
241F-13 soap 20 48
242W-01 toiletpaper 10 36
243M-48 towel 15 90
244C-24 chinacup 10 52
245S-05 schoolbag 5 20
246
2471 打印整个库存表
2482 修改库存表中的记录
2493 删除库存表中的记录
2504 对库存表排序
2515 结束处理过程
252输入你的选择:2
253
254输入待修改的商品代号:W-01
255输入该商品的修正量:10
2561 打印整个库存表
2572 修改库存表中的记录
2583 删除库存表中的记录
2594 对库存表排序
2605 结束处理过程
261输入你的选择:1
262
263Y-12 toothbrush 10 25
264F-13 soap 20 48
265W-01 toiletpaper 10 46
266M-48 towel 15 90
267C-24 chinacup 10 52
268S-05 schoolbag 5 20
269
2701 打印整个库存表
2712 修改库存表中的记录
2723 删除库存表中的记录
2734 对库存表排序
2745 结束处理过程
275输入你的选择:2
276
277输入待修改的商品代号:D-20
278输入新商品记录的其余字段的内容:
279pencil 40 80
2801 打印整个库存表
2812 修改库存表中的记录
2823 删除库存表中的记录
2834 对库存表排序
2845 结束处理过程
285输入你的选择:1
286
287Y-12 toothbrush 10 25
288F-13 soap 20 48
289W-01 toiletpaper 10 46
290M-48 towel 15 90
291C-24 chinacup 10 52
292S-05 schoolbag 5 20
293D-20 pencil 40 80
294
2951 打印整个库存表
2962 修改库存表中的记录
2973 删除库存表中的记录
2984 对库存表排序
2995 结束处理过程
300输入你的选择:3
301
302输入待删除商品的商品代号:M-48
3031 打印整个库存表
3042 修改库存表中的记录
3053 删除库存表中的记录
3064 对库存表排序
3075 结束处理过程
308输入你的选择:1
309
310Y-12 toothbrush 10 25
311F-13 soap 20 48
312W-01 toiletpaper 10 46
313C-24 chinacup 10 52
314S-05 schoolbag 5 20
315D-20 pencil 40 80
316
3171 打印整个库存表
3182 修改库存表中的记录
3193 删除库存表中的记录
3204 对库存表排序
3215 结束处理过程
322输入你的选择:4
323
3241 打印整个库存表
3252 修改库存表中的记录
3263 删除库存表中的记录
3274 对库存表排序
3285 结束处理过程
329输入你的选择:1
330
331C-24 chinacup 10 52
332D-20 pencil 20 40
333F-13 soap 20 48
334S-05 schoolbag 5 20
335W-01 toiletpaper 10 56
336Y-12 toothbrush 10 25
337
3381 打印整个库存表
3392 修改库存表中的记录
3403 删除库存表中的记录
3414 对库存表排序
3425 结束处理过程
343输入你的选择:5
344
345本次处理结束,再见!
346pos is out range!
347Press any key to continue
348*/