c语言实践实验设备管理完整.docx
《c语言实践实验设备管理完整.docx》由会员分享,可在线阅读,更多相关《c语言实践实验设备管理完整.docx(17页珍藏版)》请在冰点文库上搜索。
![c语言实践实验设备管理完整.docx](https://file1.bingdoc.com/fileroot1/2023-5/6/34f85f0e-d19a-4f7d-abee-cd4494be9cfb/34f85f0e-d19a-4f7d-abee-cd4494be9cfb1.gif)
c语言实践实验设备管理完整
中国地质大学(武汉)机械设计制造及其自动化专业072105——陈乾
#include
#include
#defineAMOUNT50
typedefstruct{
intyear。
intmonth。
intday。
}DATE。
structEQU{
intnum。
/*本程序默认设备编号各不相同且不为0*/
chartype[20]。
charname[20]。
intprice。
DATEbuy_time。
intscrap。
/*0表示报废1表示不报废*/
DATEscr_time。
}equ[AMOUNT]。
/*公共函数保存与加载函数*/
voidsave()
{
FILE*fp。
inti。
if((fp=fopen("record","wb"))==NULL)
printf("cannotopenfile\n")。
for(i=0。
ii++)
if(fwrite(&equ[i],sizeof(structEQU),1,fp)!
=1)
printf("filewriteerror\n")。
fclose(fp)。
}
voidload()
{
FILE*fp。
inti。
if((fp=fopen("record","rb"))==NULL)
printf("cannotopenfile\n")。
for(i=0。
ii++)
if(fread(&equ[i],sizeof(structEQU),1,fp)!
=1)
{
if(feof(fp))fclose(fp)。
printf("filereaderror\n")。
}
fclose(fp)。
}
/*录入模块*/
voidadd()
{
intflag。
structEQU*p。
load()。
/*程序缺陷如果record文件中存储数据不足将重复输出多个filereaderror原因在于for语句对其进行循环AMOUNT次循环如不写本行记录将被覆盖*/
p=equ。
do
{
if(p->num!
=0)p++。
printf("请输入设备编号\n")。
scanf("%d",&p->num)。
printf("请输入设备种类\n")。
scanf("%s",p->type)。
printf("请输入设备名称\n")。
scanf("%s",p->name)。
printf("请输入设备价格\n")。
scanf("%d",&p->price)。
printf("请输入设备购买日期年月日以空格隔开\n")。
scanf("%d%d%d",&p->buy_time.year,&p->buy_time.month,&p->buy_time.day)。
p->scrap=1。
p->scr_time.year=0。
p->scr_time.month=0。
p->scr_time.day=0。
printf("\n以下记录录入成功\n")。
printf("编号:
%d设备种类:
%s设备名称:
%s\n价格:
%d购买日期:
%d%d%d\n是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
printf("继续录入请按1,退出请按2\n")。
scanf("%d",&flag)。
}while(flag==1)。
save()。
}
/*修改模块*/
voidchange()
{
intflag,n,i=0。
structEQU*p。
p=equ。
load()。
printf("请输入要更改的设备编号\n")。
scanf("%d",&flag)。
for(i=0,p=equ。
ii++,p++)
{
if(p->num==flag){
do
{
printf("请输入要更改的信息:
\n1设备编号\n2设备种类\n3设备名称\n4设备价格\n5购买日期\n6是否报废\n7退出\n")。
scanf("%d",&n)。
switch(n)
{
case1:
printf("请输入新的设备编号\n")。
scanf("%d",&p->num)。
break。
case2:
printf("请输入新的设备种类\n")。
scanf("%s",p->type)。
break。
case3:
printf("请输入新的设备名称\n")。
scanf("%s",p->name)。
break。
case4:
printf("请输入新的价格\n")。
scanf("%d",&p->price)。
break。
case5:
printf("请输入新的设备购买日期年月日\n")。
scanf("%d%d%d",&p->buy_time.year,&p->buy_time.month,&p->buy_time.day)。
break。
case6:
printf("请输入是否报废设备\n")。
scanf("%d",&p->scrap)。
printf("请输入报废日期\n")。
scanf("%d%d%d",&p->scr_time.year,&p->scr_time.month,&p->scr_time.day)。
break。
case7:
break。
}
}while(n!
=7)。
printf("修改后的记录为:
")。
printf("编号:
%d设备种类:
%s设备名称:
%s\n价格:
%d购买日期:
%d%d%d\n是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
}
}
save()。
}
/*查询模块*/
voidsearch()
{
intn,num,i,year,month,day。
chars[20]。
structEQU*p。
p=equ。
load()。
do{
printf("1.按编号查询\n")。
printf("2.按设备种类查询\n")。
printf("3.按设备名称查询\n")。
printf("4.按购买日期查询\n")。
printf("5.查询正常的设备\n")。
printf("6.退出\n")。
scanf("%d",&n)。
switch(n){
case1:
printf("请输入要查询的设备编号\n")。
scanf("%d",&num)。
for(i=0,p=equ。
ii++,p++){
if(p->num==num)
printf("编号:
%d设备种类:
%s设备名称:
%s\n价格:
%d购买日期:
%d%d%d\n是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
}break。
case2:
printf("请输入要查询的设备种类\n")。
scanf("%s",s)。
for(i=0,p=equ。
ii++,p++){
if(strcmp(s,p->type)==0)
printf("编号:
%d设备种类:
%s设备名称:
%s价格:
%d购买日期:
%d%d%d是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
}break。
case3:
printf("请输入要查询的设备名称\n")。
scanf("%s",s)。
for(i=0,p=equ。
ii++,p++){
if(strcmp(s,p->name)==0)
printf("编号:
%d设备种类:
%s设备名称:
%s价格:
%d购买日期:
%d%d%d是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
}break。
case4:
printf("请输入要查询的设备购买日期年月日\n")。
scanf("%d%d%d",&year,&month,&day)。
for(i=0,p=equ。
ii++,p++){
if(year==p->buy_time.year&&month==p->buy_time.month&&day==p->buy_time.day)
printf("编号:
%d设备种类:
%s设备名称:
%s价格:
%d购买日期:
%d%d%d是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
}break。
case5:
printf("未报废的设备有:
\n")。
for(i=0,p=equ。
ii++,p++){
if(p->scrap==1)
printf("编号:
%d设备种类:
%s设备名称:
%s价格:
%d购买日期:
%d%d%d是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
}break。
case6:
break。
}
}while(n!
=6)。
}
/*统计模块*/
voidstatis()
{
intflag,n,i,j,k=0。
chars[20]。
structEQU*p。
structEQUstr[AMOUNT],str1[1]。
p=equ。
load()。
printf("请输入要查询的设备种类\n")。
scanf("%s",&s[0])。
for(i=0,n=0,p=equ。
ii++,p++){
if(strcmp(s,p->type)==0){
str[n].num=p->num。
str[n].price=p->price。
strcpy(str[n].name,p->name)。
str[n].scrap=p->scrap。
strcpy(str[n].type,p->type)。
str[n].buy_time.year=p->buy_time.year。
str[n].buy_time.month=p->buy_time.month。
str[n].buy_time.day=p->buy_time.day。
str[n].scr_time.day=p->scr_time.day。
str[n].scr_time.month=p->scr_time.month。
str[n].scr_time.year=p->scr_time.year。
n++。
}
}
/*以上程序是将需要查询的设备种类的所有设备信息存入数组str中*/
do{
printf("1.按编号排序\n")。
printf("2.按价格排序\n")。
printf("3.按购买日期排序\n")。
printf("4.退出\n")。
/*这段程序之后出错*/
scanf("%d",&flag)。
switch(flag){
case1:
for(j=0。
jj++)/*起泡法排序*/
for(i=0。
ii++)
if(str[i].num>str[i+1].num)
{
str1[0].num=str[i].num。
str1[0].buy_time.day=str[i].buy_time.day。
/*t=a[i]*/
str1[0].buy_time.month=str[i].buy_time.month。
str1[0].buy_time.year=str[i].buy_time.year。
str1[0].scr_time.year=str[i].scr_time.year。
str1[0].scr_time.month=str[i].scr_time.month。
str1[0].scr_time.day=str[i].scr_time.day。
str1[0].price=str[i].price。
str1[0].scrap=str[i].scrap。
strcpy(str1[0].name,str[i].name)。
strcpy(str1[0].type,str[i].type)。
str[i].num=str[i+1].num。
str[i].buy_time.day=str[i+1].buy_time.day。
/*a[i]=a[i+1]*/
str[i].buy_time.month=str[i+1].buy_time.month。
str[i].buy_time.year=str[i+1].buy_time.year。
str[i].scr_time.year=str[i+1].scr_time.year。
str[i].scr_time.month=str[i+1].scr_time.month。
str[i].scr_time.day=str[i+1].scr_time.day。
str[i].price=str[i+1].price。
str[i].scrap=str[i+1].scrap。
strcpy(str[i].name,str[i+1].name)。
strcpy(str[i].type,str[i+1].type)。
str[i+1].num=str1[0].num。
str[i+1].buy_time.day=str1[0].buy_time.day。
/*a[i+1]=t*/
str[i+1].buy_time.month=str1[0].buy_time.month。
str[i+1].buy_time.year=str1[0].buy_time.year。
str[i+1].scr_time.year=str1[0].scr_time.year。
str[i+1].scr_time.month=str1[0].scr_time.month。
str[i+1].scr_time.day=str1[0].scr_time.day。
str[i+1].price=str1[0].price。
str[i+1].scrap=str1[0].scrap。
strcpy(str[i+1].name,str1[0].name)。
strcpy(str[i+1].type,str1[0].type)。
}
for(i=0,p=str。
ii++,p++)
printf("编号:
%d设备种类:
%s设备名称:
%s价格:
%d购买日期:
%d%d%d是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
break。
case2:
for(j=0。
jj++)/*起泡法排序*/
for(i=0。
ii++)
if(str[i].price>=str[i+1].price)
{
str1[0].num=str[i].num。
str1[0].buy_time.day=str[i].buy_time.day。
/*t=a[i]*/
str1[0].buy_time.month=str[i].buy_time.month。
str1[0].buy_time.year=str[i].buy_time.year。
str1[0].scr_time.year=str[i].scr_time.year。
str1[0].scr_time.month=str[i].scr_time.month。
str1[0].scr_time.day=str[i].scr_time.day。
str1[0].price=str[i].price。
str1[0].scrap=str[i].scrap。
strcpy(str1[0].name,str[i].name)。
strcpy(str1[0].type,str[i].type)。
str[i].num=str[i+1].num。
str[i].buy_time.day=str[i+1].buy_time.day。
/*a[i]=a[i+1]*/
str[i].buy_time.month=str[i+1].buy_time.month。
str[i].buy_time.year=str[i+1].buy_time.year。
str[i].scr_time.year=str[i+1].scr_time.year。
str[i].scr_time.month=str[i+1].scr_time.month。
str[i].scr_time.day=str[i+1].scr_time.day。
str[i].price=str[i+1].price。
str[i].scrap=str[i+1].scrap。
strcpy(str[i].name,str[i+1].name)。
strcpy(str[i].type,str[i+1].type)。
str[i+1].num=str1[0].num。
str[i+1].buy_time.day=str1[0].buy_time.day。
/*a[i+1]=t*/
str[i+1].buy_time.month=str1[0].buy_time.month。
str[i+1].buy_time.year=str1[0].buy_time.year。
str[i+1].scr_time.year=str1[0].scr_time.year。
str[i+1].scr_time.month=str1[0].scr_time.month。
str[i+1].scr_time.day=str1[0].scr_time.day。
str[i+1].price=str1[0].price。
str[i+1].scrap=str1[0].scrap。
strcpy(str[i+1].name,str1[0].name)。
strcpy(str[i+1].type,str1[0].type)。
}
for(i=0,p=str。
ii++,p++)
printf("编号:
%d设备种类:
%s设备名称:
%s价格:
%d购买日期:
%d%d%d是否报废:
%d报废日期:
%d%d%d\n",p->num,p->type,p->name,p->price,p->buy_time.year,p->buy_time.month,p->buy_time.day,p->scrap,p->scr_time.year,p->scr_time.month,p->scr_time.day)。
break。
case3:
/*由于程序过于复杂需连续用3次起泡法所以按购买日期排序只对年份排序*/
for(j=0。
jj++)/*起泡法排序*/
for(i=0。
ii++)
if(str[i].buy_ti