1. 线性表之应用(顺序存储结构)
list.h:
1
void 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
}11
ElemType 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
}20
void InitList(List& L)21


{22
L.size=0;23
}24
int ListSize(List& L)25


{26
return L.size;27
}28
void TraverseList(List& L)29


{30
for(int i=0;i<L.size;i++)31
cout<<L.list[i];32
cout<<endl;33
}34
int 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
}44
int 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
}54
int 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
}74
void 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
else85
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"6
const int MaxSize=10;7
struct goods8


{9
char code[5]; //商品代码10
char name[15]; //商品名称11
int minq; //最低库存量12
int curq; //当前库存量13
};14

15
typedef goods ElemType;16
struct List17


{18
ElemType list[MaxSize];19
int size;20
};21

22
int operator ==(const ElemType& e1,const ElemType& e2)23


{24
return (strcmp(e1.code,e2.code)==0);25
}26
int operator <(const ElemType& e1,const ElemType& e2)27


{28
return (strcmp(e1.code,e2.code)==-1);29
}30
ostream& 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

/**//*38
void 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
}48
ElemType 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
}57
void InitList(List& L)58
{59
L.size=0;60
}61
int ListSize(List& L)62
{63
return L.size;64
}65
void TraverseList(List& L)66
{67
for(int i=0;i<L.size;i++)68
cout<<L.list[i];69
cout<<endl;70
}71
int 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
}81
int 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
}91
int 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
//---------------------------------------------------------113
void 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

131
void 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
}147
void 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
}164
void main()165


{166
char *FileGoods="D:goods.dat";167
SetupGoddsFile(FileGoods);168
List L2; //说明一个线性表L2169
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
else205

{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

/**//*227
Y-12 toothbrush 10 25228
F-13 soap 20 48229
W-01 toiletpaper 10 36230
M-48 towel 15 90231
C-24 chinacup 10 52232
S-05 schoolbag 5 20233
1 打印整个库存表234
2 修改库存表中的记录235
3 删除库存表中的记录236
4 对库存表排序237
5 结束处理过程238
输入你的选择:1239

240
Y-12 toothbrush 10 25241
F-13 soap 20 48242
W-01 toiletpaper 10 36243
M-48 towel 15 90244
C-24 chinacup 10 52245
S-05 schoolbag 5 20246

247
1 打印整个库存表248
2 修改库存表中的记录249
3 删除库存表中的记录250
4 对库存表排序251
5 结束处理过程252
输入你的选择:2253

254
输入待修改的商品代号:W-01255
输入该商品的修正量:10256
1 打印整个库存表257
2 修改库存表中的记录258
3 删除库存表中的记录259
4 对库存表排序260
5 结束处理过程261
输入你的选择:1262

263
Y-12 toothbrush 10 25264
F-13 soap 20 48265
W-01 toiletpaper 10 46266
M-48 towel 15 90267
C-24 chinacup 10 52268
S-05 schoolbag 5 20269

270
1 打印整个库存表271
2 修改库存表中的记录272
3 删除库存表中的记录273
4 对库存表排序274
5 结束处理过程275
输入你的选择:2276

277
输入待修改的商品代号:D-20278
输入新商品记录的其余字段的内容:279
pencil 40 80280
1 打印整个库存表281
2 修改库存表中的记录282
3 删除库存表中的记录283
4 对库存表排序284
5 结束处理过程285
输入你的选择:1286

287
Y-12 toothbrush 10 25288
F-13 soap 20 48289
W-01 toiletpaper 10 46290
M-48 towel 15 90291
C-24 chinacup 10 52292
S-05 schoolbag 5 20293
D-20 pencil 40 80294

295
1 打印整个库存表296
2 修改库存表中的记录297
3 删除库存表中的记录298
4 对库存表排序299
5 结束处理过程300
输入你的选择:3301

302
输入待删除商品的商品代号:M-48303
1 打印整个库存表304
2 修改库存表中的记录305
3 删除库存表中的记录306
4 对库存表排序307
5 结束处理过程308
输入你的选择:1309

310
Y-12 toothbrush 10 25311
F-13 soap 20 48312
W-01 toiletpaper 10 46313
C-24 chinacup 10 52314
S-05 schoolbag 5 20315
D-20 pencil 40 80316

317
1 打印整个库存表318
2 修改库存表中的记录319
3 删除库存表中的记录320
4 对库存表排序321
5 结束处理过程322
输入你的选择:4323

324
1 打印整个库存表325
2 修改库存表中的记录326
3 删除库存表中的记录327
4 对库存表排序328
5 结束处理过程329
输入你的选择:1330

331
C-24 chinacup 10 52332
D-20 pencil 20 40333
F-13 soap 20 48334
S-05 schoolbag 5 20335
W-01 toiletpaper 10 56336
Y-12 toothbrush 10 25337

338
1 打印整个库存表339
2 修改库存表中的记录340
3 删除库存表中的记录341
4 对库存表排序342
5 结束处理过程343
输入你的选择:5344

345
本次处理结束,再见!346
pos is out range!347
Press any key to continue348
*/
浙公网安备 33010602011771号