第1个回答 2007-10-04
这个是我自己做的管理系统,里面的汉字什么的你改下就OK了,想做什么系统只要把汉字部分换成相应的汉字就行,看着行你就给分,好的话追加,感觉垃圾就骂我。
#include<stdio.h>
struct material
{int num;
char name[20];
char leibie[20];
char shijian[20];
int shuliang;
int price;
int total;
int delet ; }
work[50];
static n=1;int f,g=1,e;
FILE *fp1;
char fname[20];
jianli() /*创建新文件*/
{
int i,x=0;FILE *fp;
system("cls");
menu1();
menu2();
printf("\t\t\t 请输入文件名!");
scanf("%s",fname);
fp=fopen(fname,"rb");
for(i=0;i<50;i++)
{fread(&work[i],sizeof(struct material),1,fp);
if(work[i].total!=0)x++;
}
fclose(fp);n=x;
}
r()
{
int i;FILE *fp;
if((fp=fopen(fname,"rb"))==NULL)
{
printf("\n\n\t\tcannot open file!\n");}
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
for(i=0;i<n;i++)
{
fread(&work[i],sizeof(struct material),1,fp);
if(work[i].delet==0)
printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",
work[i].num,work[i].name,work[i].leibie,work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
}
fclose(fp);
}
r1()
{
int i;FILE *fp;
if((fp=fopen(fname,"rb"))==NULL)
{
printf("\n\n\t\tcannot open file!\n");}
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
for(i=0;i<n;i++)
{
fread(&work[i],sizeof(struct material),1,fp);
if(work[i].delet==1)
printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",work[i].num,work[i].name,work[i].leibie,
work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
}
fclose(fp);
}
display2() /*显示所有信息*/
{
int i,d=0;FILE *fp;
fp=fopen(fname,"rb");
system("cls");menu1();
printf("以下是所有材料信息!\n");
for(i=0;i<n;i++)
fread(&work[i],sizeof(struct material),1,fp);
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
for(i=0;i<n;i++)
if(work[i].delet==0)
{d++;
printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",work[i].num,work[i].name,work[i].leibie,
work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
}
if(d==0)printf("没找着,想好了再找!\n");
printf("退出请按任意数字键!\n");
scanf("%d",&i);
}
display1() /*显示信息*/
{int i;int d=0;
printf("以下是排序后的信息 :\n");
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
for(i=0;i<n;i++)
if(work[i].delet==0)
{d++;
printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",work[i].num,work[i].name,work[i].leibie,
work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
}
if(d==0){printf("没找着,想好了再找!\n");system("pause");}
}
save() /*保存函数*/
{FILE *fp;int i;
system("cls");menu1();
printf("保存在原文件请按 1 另存为请按 2\n");
scanf("%d",&e);
while(1)
{
if(e==1)
{
if((fp=fopen(fname,"w"))==0)
{printf("cannot open\n");return;}
for(i=0;i<n;i++)
if(fwrite(&work[i],sizeof(struct material), 1,fp)!=1)
printf("error!\n");break;
}
else if(e==2)
{
printf("input jianli file name!\n");
scanf("%s",fname);
if((fp=fopen(fname,"w"))==0)
{printf("cannot open\n");return;}
for(i=0;i<n;i++)
if(fwrite(&work[i],sizeof(struct material), 1,fp)!=1)
printf("error!error!\n");break;
}
else printf("error!\n");
}
fclose(fp);
system("cls");menu1();
printf("OK了!\n\n");system("pause");
}
slect()
{
int d,i;system("cls");menu1();menu2();scanf("%d",&d);
if(d==1){add();slect();}
else if(d==2){display2();slect();}
else if(d==3){cha();slect();}
else if(d==4){delet();slect();}
else if(d==5){modify();slect();}
else if(d==6){order();slect();}
else if(d==7){recy();slect();}
else if(d==8){tongji();slect();}
else if(d==9){jianli();slect();}
else if(d==0){printf("exit!\n\n");exit();}
else {printf("error!\n");slect();}
}
cha() /*查询*/
{
int i,num,c,suc=0,suc1;char name[20];FILE *fp;
fp=fopen(fname,"rb");
for(i=0;i<n;i++)
fread(&work[i],sizeof(struct material),1,fp);
while(1)
{
system("cls");menu1();
printf("您有两种方式查询:\n");
printf("输入名称请按 1 输入材料号请按 2\n");
scanf("%d",&c);
system("cls");menu1();
if(c==1)
{printf("输入名称\n");
scanf("%s",name);break;}
else if(c==2)
{printf("输入材料号\n");
scanf("%d",&num);break;}
else while(1)
{ system("cls");menu1();printf("输入出错啦!!!!\n");
printf("重输请按 0 退出请按 1\n ");
scanf("%d",&suc);
if(suc==0||suc==1)
break;
else continue;
}
if(suc==1)break;
}
if(suc!=1)
{
for(i=0;i<n;i++)
if((strcmp(name,work[i].name)==0||num==work[i].num)&&work[i].delet==0)break;
if(i<n)
{ system("cls");menu1();
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",work[i].num,work[i].name,work[i].leibie,
work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
printf("请按任意数字键继续!\n");
scanf("%d",&i);
}
else {system("cls");menu1();
printf(" I’m sorry!我都快死机了也没找到....!\n\n",i);
printf("继续请按任意数字键!\n");
scanf("%d",&i);}
while(1)
{ system("cls");menu1();
printf("继续请按 0 退出请按 1\n ");
scanf("%d",&suc1);
if(suc1==0||suc1==1)
break;
else printf(" error!\n\n",i);
printf("继续请按任意数字键!\n");
scanf("%d",&i);
}
if(suc1==0) cha();
if(suc1==1) {printf("bye!\n");
system("pause");
}
}
else printf("bye!\n");
}
add() /*添加*/
{
int e,i,num,c,h,suc=0,suc1;char name[20];int a;
while(suc==0)
{if(n<50){a=n;n++;work[a].delet=0;}
else
{for(i=0;i<n;i++)
if(work[i].delet==2)break;
{a=i;work[a].delet=0;}
}
while(1)
{printf("文件已经存有以下信息:\n\n");
system("cls");menu1();r();
printf("请输入编号:\n");
scanf("%d",&work[a].num);
for(h=0;h<n;h++)
{if(h==a)h++;
if((work[a].num==work[h].num)&&work[h].delet==0)break;
}
if(h<n)
{system("cls");menu1();
printf("编号重复!想好了再写!\n"); printf("请按任意数字键继续!\n");
scanf("%d",&i);
}
else break;
}
system("cls");menu1();r();
while(1)
{
printf("请输入名称:name\n");
scanf("%s",&work[a].name);
for(h=0;h<n;h++)
{if(h==a)h++;
if(strcmp(work[a].name,work[h].name)==0&&work[h].delet==0)break;
}
if(h<n)
{system("cls");menu1();printf("名称重复!想好了写!\n");
printf("请按任意数字键继续!\n");
scanf("%d",&i);}
else break;
}
system("cls");menu1();r();
printf("请输入类别:\n");
scanf("%s",&work[a].leibie);system("cls");menu1();r();
printf("请输入登记时间:\n");
scanf("%s",&work[a].shijian);system("cls");menu1();r();
printf("请输入数量:\n");
scanf("%d",&work[a].shuliang);system("cls");menu1();r();
printf("请输入单价:\n");
scanf("%d",&work[a].price);system("cls");menu1();r();
work[a].total=work[a].shuliang*work[a].price;
work[a].delet=0;system("cls");menu1();r();
while(1)
{
printf("\n");
printf(" 保存请按 1 重输请按 2\n");
scanf("%d",&e);
if(e==1){save();suc=1;break;}
else if(e==2){n--;break;}
else {system("cls");menu1();printf("写错啦!想好了写!!\n");}
}
while(e==1)
{system("cls");menu1();
printf("继续请按 0 退出请按 1\n");
scanf("%d",&suc);
if(suc==0||suc==1)break;
else {system("cls");menu1();printf("输入有误!想好了写!\n\n");}
}
if(suc==1)break;
}
}
delet() /*删除*/
{
char j;int e,i,num,c,suc=0;
char name[20];
system("cls");menu1();r();
while(1)
{
printf("请输入您要删除的材料的信息:名称 或 材料号\n");
printf("\n");
printf("输入名称请按 1 输入材料号请按2\n");
scanf("%d",&c);
system("cls");menu1();r();
if(c==1)
{printf("输入名称\n");
scanf("%s",name);break;}
else if(c==2)
{printf("输入材料号\n");
scanf("%d",&num);break;}
else while(1)
{printf("输入出错啦!!!!\n");
printf("重输请按 0 确认请按 1\n ");
scanf("%d",&suc);
if(suc==0||suc==1)
break;
else continue;
}
if(suc==1)break;
}
if(suc!=1)
{
for(i=0;i<n;i++)
if((strcmp(name,work[i].name)==0||num==work[i].num)&&work[i].delet==0)break;
if(i<n)
while(1)
{ system("cls");menu1();r();
printf("您要删除 %s 的信息!\n ",work[i].name);
printf("\n");
printf("暂时删除请按 1 永久删除请按 2\n");
scanf("%d",&e);
if(e==1)
{work[i].delet=1;system("cls");menu1();printf("deleted sucessfully!\n\n");save();break; }
if(e==2)
{work[i].delet=2;system("cls");menu1();printf("deleted sucessfully!\n\n");save();break;}
else {system("cls");menu1(); printf(" 输入出错啦!!!!\n\n",i);
printf("继续请按任意数字键!\n"); scanf("%d",&i);}
}
if(i>=n){system("cls");menu1(); printf(" I’m sorry!我都快死机了也没找到....!\n\n",i);
printf("继续请按任意数字键!\n"); scanf("%d",&i);}
while(1)
{system("cls");menu1();
printf("继续请按 0 退出请按 1\n");scanf("%d",&e);
if(e==0||e==1)break;
else {system("cls");menu1();printf(" 输入出错啦!!!!\n\n",i);
printf("继续请按任意数字键!\n");scanf("%d",&i);}
}
if(e==0)delet();
else printf("bye!\n");
}
}
modify() /*修改*/
{ char name[20];int i,e=1,a,h;
system("cls");menu1();
r();
printf("请输入您要修改的材料的名称!\n");
scanf("%s",name);
for(i=0;i<n;i++)
if(strcmp(work[i].name,name)==0&&work[i].delet==0)break;
if(i<n)
{system("cls");menu1();
printf("您要修改的材料的编号为num: %d, 修改 0 放弃修改 1\n",work[i].num);
scanf("%d",&e);
if(e==0)
while(1)
{ printf("请输入编号:\n");
scanf("%d",&work[i].num);
for(h=0;h<n;h++)
{if(h==i)h++;
if((work[i].num==work[h].num)&&work[h].delet==0)break;
}
if(h<n) printf("编号重复!想好了写!\n");
else break;
}
system("cls");menu1();
printf("您要修改的材料的名称为:%s, 修改 0 放弃修改 1\n",work[i].name);
scanf("%d",&e);
if(e==0)
while(1)
{ printf("请输入名称:\n");
scanf("%s",&work[i].name);
for(h=0;h<n;h++)
{if(h==i)h++;
if(strcmp(work[i].name,work[h].name)==0&&work[h].delet==0)break;
}
if(h<n) printf("名称重复!想好了写!s\n");
else break;
}
system("cls");menu1();
printf("您要修改的材料的类别为 %s, 修改 0 放弃修改 1\n",work[i].leibie);
scanf("%d",&e);
if(e==0)
{printf("请输入类别:\n");
scanf("%s",&work[i].leibie);}
system("cls");menu1();
printf("您要修改的材料的登记时间为:%s, 修改 0 放弃修改 1\n",work[i].shijian);
scanf("%d",&e);
if(e==0)
{printf("请输入登记时间:\n");
scanf("%s",&work[i].shijian);}
system("cls");menu1();
printf("您要修改的材料的数量为%d, 修改 0 放弃修改 1\n", work[i].shuliang);
scanf("%d",&e);
if(e==0)
{printf("请输入数量:\n");
scanf("%d",&work[i].shuliang);}
system("cls");menu1();
printf("您要修改的材料的单价为%d, 修改 0 放弃修改 1\n",work[i].price);
scanf("%d",&e);
if(e==0)
{printf("请输入单价:\n");
scanf("%d",&work[i].price);}
work[i].total=work[i].shuliang*work[i].price;
system("cls");menu1();
printf("搞定! \n");
printf("新信息为:\n");
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",work[i].num,work[i].name,work[i].leibie,
work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
while(1)
{
printf(" 保存请按 1 取消请按 2\n");
scanf("%d",&e);
if(e==1){save();break;}
else if(e==2)break;
else {system("cls");menu1();printf("写错啦!想好了写!!\n");}
}
}
if(i==n) {system("cls");menu1();printf("no such a material!\n"); printf("继续请按任意数字键!\n");
scanf("%d",&i);}
while(e==1)
{system("cls");menu1();
printf("继续请按 0 退出请按 1\n");
scanf("%d",&e);
if(e==0||e==1)break;
else {system("cls");menu1();printf("输入有误!想好了写!\n\n");
printf("继续请按任意数字键!\n"); scanf("%d",&i);}
}
if(e==0)modify();
if(e==1)printf("exit!\n");
}
er() /*冒泡排序*/
{int t;char dai[20];
t=work[f].price;work[f].price=work[f+1].price;work[f+1].price=t;
t=work[f].num;work[f].num=work[f+1].num;work[f+1].num=t;
t=work[f].shuliang;work[f].shuliang=work[f+1].shuliang;work[f+1].shuliang=t;
t=work[f].total;work[f].total=work[f+1].total;work[f+1].total=t;
strcpy(dai,work[f].name);
strcpy(work[f].name,work[f+1].name);
strcpy(work[f+1].name,dai);
strcpy(dai,work[f].leibie);
strcpy(work[f].leibie,work[f+1].leibie);
strcpy(work[f+1].leibie,dai);
strcpy(dai,work[f].shijian);
strcpy(work[f].shijian,work[f+1].shijian);
strcpy(work[f+1].shijian,dai);
}
order1() /*按数量排序*/
{ int i,j,e;
system("cls");menu1();
for(j=0;j<n-1;j++)
for(i=0;i<n-1-j;i++)
if((work[i].shuliang>work[i+1].shuliang)&&work[i].delet==0)
{f=i;er();}
while(1)
{ system("cls");menu1();display1();
printf(" 保存请按 1 取消请按 2\n");
scanf("%d",&e);
if(e==1){save();break;}
else if(e==2)break;
else {system("cls");menu1();printf("写错啦!想好了写!!\n");
printf("继续请按任意数字键!\n");scanf("%d",&i);}
}
}
order2() /*按单价排序*/
{ int i,j;
system("cls");menu1();
for(j=0;j<n-1;j++)
for(i=0;i<n-1-j;i++)
if((work[i].price>work[i+1].price)&&work[i].delet==0)
{f=i;er();}
while(1)
{ system("cls");menu1();display1();
printf(" 保存请按 1 取消请按 2\n");
scanf("%d",&e);
if(e==1){save();break;}
else if(e==2)break;
else {system("cls");menu1();printf("写错啦!想好了写!!\n");
printf("继续请按任意数字键!\n");scanf("%d",&i);}
}
}
order3() /*按总价排序*/
{ int i,j;
system("cls");menu1();
for(j=0;j<n-1;j++)
for(i=0;i<n-1-j;i++)
if((work[i].total>work[i+1].total)&&work[i].delet==0)
{f=i;er();}
while(1)
{ system("cls");menu1();display1();
printf(" 保存请按 1 取消请按 2\n");
scanf("%d",&e);
if(e==1){save();break;}
else if(e==2)break;
else {system("cls");menu1();printf("写错啦!想好了写!!\n");
printf("继续请按任意数字键!\n");scanf("%d",&i);}
}
}
deci() /*退出继续选择*/
{ int e,i;
while(1)
{ system("cls");menu1();
printf(" 继续按 1 退出按 2 \n");
scanf("%d",&e);
if(e==1){g=1;break;}
if(e==2){g=2;break;}
else {printf("error!\n"); printf("继续请按任意数字键!\n");
scanf("%d",&i);}
}
}
order() /*排序函数*/
{ int e,i;
while(g==1)
{system("cls");menu1();
printf("按数量排序请按 1 \n");
printf("按单价排序请按 2 \n");
printf("按总价排序请按 3 \n");
scanf("%d",&e);
if(e==1){order1();deci();}
else if(e==2){order2();deci();}
else if(e==3){order3();deci();}
else {system("cls");menu1();printf("写错啦!想好了写!!\n"); printf("继续请按任意数字键!\n");
scanf("%d",&i);}
}
g=1;
}
recy() /*恢复函数*/
{
int i,e,num;FILE *fp;
system("cls");menu1();
fp=fopen(fname,"rb");
for(i=0;i<n;i++)
fread(&work[i],sizeof(struct material),1,fp);
printf("编号 名称 类别 登记时间 数量 单价 总价\n");
for(i=0;i<n;i++)
if(work[i].delet==1)
{ printf("%-5d %-10s %-10s %-10s %-6d %-6d %-6d \n",work[i].num,work[i].name,work[i].leibie,
work[i].shijian,work[i].shuliang,work[i].price,work[i].total);
}
else if(i==n){printf("没找着,想好了再找!\n");printf("继续请按任意数字键!\n");
scanf("%d",&i);}
while(1)
{ system("cls");menu1();r1();
printf("全部死刑(清空)0 宣判无罪(恢复)1 大赦天下(全部恢复)2 打道回府(退出)3 \n");
scanf("%d",&e);
if(e==0)
{for(i=0;i<n;i++)
if(work[i].delet==1)work[i].delet=2;break;
}
else if(e==1)
{ system("cls");menu1();r1();
printf("请输入要恢复的材料的编号:\n");
scanf("%d",&num);
for(i=0;i<n;i++)
if(num==work[i].num&&work[i].delet==1) break;
if(i<n) {work[i].delet=0;break;}
else if(i==n) {printf("没找着,想好了再找!\n");printf("继续请按任意数字键!\n");
scanf("%d",&i); break;}
}
else if(e==2)
{for(i=0;i<n;i++)
if(work[i].delet==1)work[i].delet=0;break;
}
else if(e==3)break;
else {printf("写错啦!想好了写!!\n"); printf("继续请按任意数字键!\n");
scanf("%d",&i);}
}
system("cls");menu1();printf("搞定!\n");
printf("按任意数字键退出!\n");scanf("%d",&i);save();
}
tongji() /*统计函数*/
{int i,e,n1=0,n2=0,n3=0,n4=0,n5=0,z=0,c=0;FILE *fp;
system("cls");menu1();
fp=fopen(fname,"rb");
for(i=0;i<n;i++)
fread(&work[i],sizeof(struct material),1,fp);
for(i=0;i<n;i++)
{if(work[i].delet==0){z=z+work[i].total;c++;}
if(work[i].total<3000&&work[i].delet==0)n1++;
if(work[i].total>=3000&&work[i].total<4000&&work[i].delet==0)n2++;
if(work[i].total>=4000&&work[i].total<5000&&work[i].delet==0)n3++;
if(work[i].total>=5000&&work[i].delet==0)n4++;
}
n5=z/c;
printf(" 总价 材料种类数 \n",n1);
printf(" 3000以下: %d\n",n1);
printf(" 3000 --- 4000: %d\n",n2);
printf(" 4000 --- 5000: %d\n",n3);
printf(" 5000以下: %d\n",n4);
printf("平均价格: %d\n",n5);
printf("\n\n");
system("pause");
while(1)
{ system("cls");menu1();
printf(" 保存请按 1 取消请按 2\n");
scanf("%d",&e);
if(e==1){save();break;}
else if(e==2)break;
else {printf("写错啦!想好了写!!\n"); printf("继续请按任意数字键!\n");
scanf("%d",&i);}
}
printf("按任意数字键退出!\n");
scanf("%d",&i);
}
menu1() /*欢迎界面*/
{
printf("\t \n");
printf("\t 课 程 设 计 \n");
printf("\t \n");
printf("\t 材料管理系统 \n");
printf("\t \n");
printf("\t 自动化 \n");
printf("\t 王璇 \n");
printf("\t \n");
printf("\t \n");
printf("\t \n");
printf("\t\n");
printf("\t \n");
printf("\t\n");
}
menu2() /*菜单函数*/
{
printf("\t1.添加材料信息\n");
printf("\t2.查看全部材料信息\n");
printf("\t3.查找材料资料\n");
printf("\t4.删除材料资料\n");
printf("\t5.修改材料数据\n");
printf("\t6.材料信息排序\n");
printf("\t7.已经删除的材料档案\n");
printf("\t8.材料信息记录统计\n");
printf("\t9.文件管理\n");
printf("\t0.退出\n");
}
main() /*主函数*/
{
system("cls");
menu1();
jianli();
slect();
}
本回答被网友采纳