主题:[讨论]C语言编写设备管理系统,帮帮看下哪里有错?
我看了几遍又修改了几遍,语法还是错误,运行不了,所以请大家帮忙看下啊~
源代码:
[code=c]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int saveflag;
void Disp();
int Add();
int Del();
void Save();
void Modify();
void Tongji();
int numberinput();
void stringinput();
void Nofind();
int choice; /*输入用户选的功能*/
char ch; /*保存文件(Y,y,N,n)*/
int count; /*保存文件中的记录条数*/
int i;
/******************************************************************************/
/* 定义与设备有关的数据结构 */
typedef struct equipment /*标记为equipment*/
{
char num[10]; /*设备号*/
char name[15]; /*设备名称*/
char person[15]; /*领用人*/
char department[15]; /*所属部门*/
int quantity; /*数量*/
char date; /*购买日期*/
int price; /*价格*/
}SHEBEI;
/*********************************************************************************/
int Locate(SHEBEI tp[],int n,char findmess[],char nameornum[])
{
int i=0;
if(strcmp(nameornum,"num")==0) /*按设备号查询*/
{
while(i<n)
{
if(strcmp(tp[i].num,findmess)==0) /*若找到findmess值的设备号*/
return i;
i++;
}
}
else if(strcmp(nameornum,"name")==0) /*按设备名称查询*/
{
while(i<n)
{
if(strcmp(tp[i].name,findmess)==0) /*若找到findmess值的设备名称*/
return i;
i++;
}
}
return -1; /*若未找到,返回一个整数-1*/
}
/******************************************************/
/*增加设备信息记录*/
int Add(SHEBEI tp[],int n)
{
char ch,num[10];
int saveflag;
int i,flag=0;
system("cls");
Disp(tp,n); /*先打印出已有设备的信息*/
while(1) /*一次可输入多条记录,直至输入设备号为0的记录才结束添加操作*/
{
while(1)
{
/*输入设备号,保证该号码没有被使用过,若输入号码为0,则退出添加记录操作*/
stringinput(num,10,"input number(press '0'return menu):"); /*格式化输入号码并检验*/
flag=0;
if(strcmp(num,"0")==0) /*输入为0,则退出添加操作,返回主界面*/
{return n;}
i=0;
while(i<n) /*查询该编号是否已经存在,若存在则要求重新输入一个未被占用的编号*/
{
if(strcmp(tp[i].num,num)==0)
{
flag=1;
break;
}
i++;
}
if(flag==1) /*提示用户是否重新输入*/
{
getchar();
printf("==>The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return n;
}
else
{break;}
}
strcpy(tp[n].num,num); /*将字符串num拷贝到tp[].num中*/
stringinput(tp[n].name,15,"Name:");
stringinput(tp[n].num,15,"person:");
stringinput(tp[n].num,15,"department:");
tp[n].quantity=numberinput("quantity"); /*输入并检验设备数量*/
tp[n].date=numberinput("date"); /*输入并检验购买日期*/
tp[n].price=numberinput("price"); /*输入并检验价格*/
saveflag=1;
n++;
}
return n;
}
/*********************************************************************************/
/*修改记录,先按输入设备号查询到该记录,然后提示用户修改编号之外的值,编号不能改*/
void Modify(SHEBEI tp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{
system("cls");
printf("\n=====>No eqquitment record!\n");
getchar();
return;
}
system("cls");
printf("motify equipment recorder");
Disp(tp,n);
stringinput(findmess,10,"input the existing equipment number:"); /*输入并检验该编号*/
p=Locate(tp,n,findmess,"num"); /*查询到该数组元素,并返回下标值*/
if(p!=-1) /*/若p!=-1,表明已经找到该数组元素*/
{
printf("Number:%s,\n",tp[p].num);
printf("Name:%s,",tp[p].name);
stringinput(tp[p].name,15,"input new name:");
printf("person:%s,",tp[p].person);
stringinput(tp[p].person,15,"input new person:");
printf("department:%s,",tp[p].department);
stringinput(tp[p]. department,15,"input new department:");
printf("quantity:%d,",tp[p]. quantity);
tp[p].quantity=numberinput("quantity");
printf("date:%d,",tp[p].date);
tp[p].date=numberinput("date");
printf("price:%8.2f,",tp[p]. price);
tp[p].price=numberinput("price");
printf("\n========>modify success!\n");
getchar();
Disp(tp,n);
getchar();
saveflag=1;
}
else
{
Nofind();
getchar();
}
return;
}
/**********************************************************************/
void Save(SHEBEI tp[],int n)
{
FILE* fp;
int i=0;
fp=fopen("c:\\sb","wb"); /*以只写方式打开二进制文件*/
if(fp==NULL) //打开文件失败
{
printf("\n======>open file error!\n");
getchar();
return;
}
for(i=0;i<n;i++)
{
if(fwrite(&tp[i],sizeof(SHEBEI),1,fp)==1) /*每次写一条记录或一个结构数组元素至文件*/
{
continue;
}
else
{
break;
}
}
if(i>0)
{
getchar();
printf("\n\n======>save file complete,total saved’s record number is:%d\n",i);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("the current link is empty, no equipment record is saved!\n");
getchar();
}
fclose(fp); /*关闭此文件*/
}
/***********************************************************************/
/*删除记录:先找到保存该记录的数组元素的下标值,然后在数组中删除该数组元素*/
int Del(SHEBEI tp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{
system("cls");
printf("\n=====>No employee record!\n");
getchar();
return n;
}
system("cls");
Disp(tp,n);
printf("\n ======>1 Delete by number ======>2 Delete by name\n");
printf(" please choice[1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"input the existing equipment number:");
p=Locate(tp,n,findmess,"num");
getchar();
if(p!=-1)
{
for(i=p+1;i<n;i++) /*删除此记录,后面记录向前移*/
{
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
strcpy(tp[i-1].person,tp[i].person);
strcpy(tp[i-1].department,tp[i].department);
tp[i-1].quantity=tp[i].quantity;
tp[i-1].date=tp[i].date;
tp[i-1].price=tp[i].price;
}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2) /*先按设备名称查询到该记录锁在的数组元素的下标题*/
{
stringinput(findmess,10,"input the existing equipment number:");
p=Locate(tp,n,findmess,"name");
getchar();
if(p!=-1)
{
for(i=p+1;i<n;i++) /*删除此记录,后面记录向前移*/
{
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
strcpy(tp[i-1].person,tp[i].person);
strcpy(tp[i-1].department,tp[i].department);
tp[i-1].quantity=tp[i].quantity;
tp[i-1].date=tp[i].date;
tp[i-1].price=tp[i].price;
}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
return n;
}
/******************************************************************************/
/*输入字符串,并进行长度验证(长度<lens)*/
void stringinput(char *t,unsigned int lens,char *notice)
{
char n[255];
do
{
printf(notice); /*显示提示信息*/
scanf("%s",n); /*输入字符串*/
if(strlen(n)>lens)
printf("\n exceed the required length!\n"); /*进行长度校验,超过lens值重新输入*/
}while(strlen(n)>lens);
strcpy(t,n); /*将输入的字符串拷贝到字符串t中*/
}
/*输入数值,0<=数值*/
int numberinput(char *notice)
{
int t=0;
do
{
printf("notice"); /*显示提示信息*/
scanf("%f",&t); /*输入如设备等数值型的值*/
if(t<0) printf("\n score must >=0! \n"); /*进行数值校验*/
}while(t<0);
return t;
}
void main()
{
SHEBEI sb[10];
FILE *fp; /*文件指针*/
fp=fopen("c:\\SHEBEI","wb");
count=0;
printf(" The Equitment Management System \n");
printf(" **********************MENU*********************\n");
printf(" * 1 input record 2 delete record *\n");
printf(" * 3 modify record 4 count record *\n");
printf(" * 5 save record 6 quit system *\n");
printf(" ***********************************************\n");
printf(" please enter your choice(1~6):");
scanf("%d",&choice);
/*退出系统*/
if(choice==6)
{
if(saveflag==1)
{ getchar();
printf("\n==>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'&&ch=='Y')
Save(sb,count);
}
printf("\n===>thank you for useness!");
getchar();
}
switch(choice)
{
case 1:count=Add(sb,count);break;
case 2:count=Del(sb,count);break;
case 3:Modify(sb,count);break;
case 4:Tongji(sb,count);break;
case 5:Save(sb,count);break;
}
}
/************************************************************************************/
void Tongji(SHEBEI tp[],int n)
{
int sel;
float sum;
sum=0.000000;
printf("************************统计*************************/n");
printf("*1 统计所有设备的总价值 2 统计某个部门的设备总价值*/n");
printf("*3 统计某种设备的总价值 */n");
printf("*****************************************************/n");
printf("请输入您的选择:");
scanf("%d",&sel);
if(sel==1)
{
for(i=0;i<=n;i++)
{
sum=sum+tp[i]price;
printf("所有设备的总价值为:%f",sum);
}
}
else
if(sel==2)
{ char dep;
printf("请输入要进行统计的部门:/n");
scanf("%s",dep);
for(i=0;i<=n;i++)
{
if(strcmp(tp[i]department,dep)==0)
sum=sum+tp[i]price;
printf("该部门的设备总价值为:%f",sum);
}
}
else if(sel==3)
{ char name;
printf("请输入要进行统计的设备名称:/n");
scanf("%s",name);
for(i=0;i<=n;i++)
{
if(strcmp(tp[i]name,name)==0)
sum=sum+tp[i]price;
printf("这种设备的总价值为:%f",sum);
}
}
}
[/code][em10]
源代码:
[code=c]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int saveflag;
void Disp();
int Add();
int Del();
void Save();
void Modify();
void Tongji();
int numberinput();
void stringinput();
void Nofind();
int choice; /*输入用户选的功能*/
char ch; /*保存文件(Y,y,N,n)*/
int count; /*保存文件中的记录条数*/
int i;
/******************************************************************************/
/* 定义与设备有关的数据结构 */
typedef struct equipment /*标记为equipment*/
{
char num[10]; /*设备号*/
char name[15]; /*设备名称*/
char person[15]; /*领用人*/
char department[15]; /*所属部门*/
int quantity; /*数量*/
char date; /*购买日期*/
int price; /*价格*/
}SHEBEI;
/*********************************************************************************/
int Locate(SHEBEI tp[],int n,char findmess[],char nameornum[])
{
int i=0;
if(strcmp(nameornum,"num")==0) /*按设备号查询*/
{
while(i<n)
{
if(strcmp(tp[i].num,findmess)==0) /*若找到findmess值的设备号*/
return i;
i++;
}
}
else if(strcmp(nameornum,"name")==0) /*按设备名称查询*/
{
while(i<n)
{
if(strcmp(tp[i].name,findmess)==0) /*若找到findmess值的设备名称*/
return i;
i++;
}
}
return -1; /*若未找到,返回一个整数-1*/
}
/******************************************************/
/*增加设备信息记录*/
int Add(SHEBEI tp[],int n)
{
char ch,num[10];
int saveflag;
int i,flag=0;
system("cls");
Disp(tp,n); /*先打印出已有设备的信息*/
while(1) /*一次可输入多条记录,直至输入设备号为0的记录才结束添加操作*/
{
while(1)
{
/*输入设备号,保证该号码没有被使用过,若输入号码为0,则退出添加记录操作*/
stringinput(num,10,"input number(press '0'return menu):"); /*格式化输入号码并检验*/
flag=0;
if(strcmp(num,"0")==0) /*输入为0,则退出添加操作,返回主界面*/
{return n;}
i=0;
while(i<n) /*查询该编号是否已经存在,若存在则要求重新输入一个未被占用的编号*/
{
if(strcmp(tp[i].num,num)==0)
{
flag=1;
break;
}
i++;
}
if(flag==1) /*提示用户是否重新输入*/
{
getchar();
printf("==>The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return n;
}
else
{break;}
}
strcpy(tp[n].num,num); /*将字符串num拷贝到tp[].num中*/
stringinput(tp[n].name,15,"Name:");
stringinput(tp[n].num,15,"person:");
stringinput(tp[n].num,15,"department:");
tp[n].quantity=numberinput("quantity"); /*输入并检验设备数量*/
tp[n].date=numberinput("date"); /*输入并检验购买日期*/
tp[n].price=numberinput("price"); /*输入并检验价格*/
saveflag=1;
n++;
}
return n;
}
/*********************************************************************************/
/*修改记录,先按输入设备号查询到该记录,然后提示用户修改编号之外的值,编号不能改*/
void Modify(SHEBEI tp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{
system("cls");
printf("\n=====>No eqquitment record!\n");
getchar();
return;
}
system("cls");
printf("motify equipment recorder");
Disp(tp,n);
stringinput(findmess,10,"input the existing equipment number:"); /*输入并检验该编号*/
p=Locate(tp,n,findmess,"num"); /*查询到该数组元素,并返回下标值*/
if(p!=-1) /*/若p!=-1,表明已经找到该数组元素*/
{
printf("Number:%s,\n",tp[p].num);
printf("Name:%s,",tp[p].name);
stringinput(tp[p].name,15,"input new name:");
printf("person:%s,",tp[p].person);
stringinput(tp[p].person,15,"input new person:");
printf("department:%s,",tp[p].department);
stringinput(tp[p]. department,15,"input new department:");
printf("quantity:%d,",tp[p]. quantity);
tp[p].quantity=numberinput("quantity");
printf("date:%d,",tp[p].date);
tp[p].date=numberinput("date");
printf("price:%8.2f,",tp[p]. price);
tp[p].price=numberinput("price");
printf("\n========>modify success!\n");
getchar();
Disp(tp,n);
getchar();
saveflag=1;
}
else
{
Nofind();
getchar();
}
return;
}
/**********************************************************************/
void Save(SHEBEI tp[],int n)
{
FILE* fp;
int i=0;
fp=fopen("c:\\sb","wb"); /*以只写方式打开二进制文件*/
if(fp==NULL) //打开文件失败
{
printf("\n======>open file error!\n");
getchar();
return;
}
for(i=0;i<n;i++)
{
if(fwrite(&tp[i],sizeof(SHEBEI),1,fp)==1) /*每次写一条记录或一个结构数组元素至文件*/
{
continue;
}
else
{
break;
}
}
if(i>0)
{
getchar();
printf("\n\n======>save file complete,total saved’s record number is:%d\n",i);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("the current link is empty, no equipment record is saved!\n");
getchar();
}
fclose(fp); /*关闭此文件*/
}
/***********************************************************************/
/*删除记录:先找到保存该记录的数组元素的下标值,然后在数组中删除该数组元素*/
int Del(SHEBEI tp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{
system("cls");
printf("\n=====>No employee record!\n");
getchar();
return n;
}
system("cls");
Disp(tp,n);
printf("\n ======>1 Delete by number ======>2 Delete by name\n");
printf(" please choice[1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"input the existing equipment number:");
p=Locate(tp,n,findmess,"num");
getchar();
if(p!=-1)
{
for(i=p+1;i<n;i++) /*删除此记录,后面记录向前移*/
{
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
strcpy(tp[i-1].person,tp[i].person);
strcpy(tp[i-1].department,tp[i].department);
tp[i-1].quantity=tp[i].quantity;
tp[i-1].date=tp[i].date;
tp[i-1].price=tp[i].price;
}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2) /*先按设备名称查询到该记录锁在的数组元素的下标题*/
{
stringinput(findmess,10,"input the existing equipment number:");
p=Locate(tp,n,findmess,"name");
getchar();
if(p!=-1)
{
for(i=p+1;i<n;i++) /*删除此记录,后面记录向前移*/
{
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
strcpy(tp[i-1].person,tp[i].person);
strcpy(tp[i-1].department,tp[i].department);
tp[i-1].quantity=tp[i].quantity;
tp[i-1].date=tp[i].date;
tp[i-1].price=tp[i].price;
}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
return n;
}
/******************************************************************************/
/*输入字符串,并进行长度验证(长度<lens)*/
void stringinput(char *t,unsigned int lens,char *notice)
{
char n[255];
do
{
printf(notice); /*显示提示信息*/
scanf("%s",n); /*输入字符串*/
if(strlen(n)>lens)
printf("\n exceed the required length!\n"); /*进行长度校验,超过lens值重新输入*/
}while(strlen(n)>lens);
strcpy(t,n); /*将输入的字符串拷贝到字符串t中*/
}
/*输入数值,0<=数值*/
int numberinput(char *notice)
{
int t=0;
do
{
printf("notice"); /*显示提示信息*/
scanf("%f",&t); /*输入如设备等数值型的值*/
if(t<0) printf("\n score must >=0! \n"); /*进行数值校验*/
}while(t<0);
return t;
}
void main()
{
SHEBEI sb[10];
FILE *fp; /*文件指针*/
fp=fopen("c:\\SHEBEI","wb");
count=0;
printf(" The Equitment Management System \n");
printf(" **********************MENU*********************\n");
printf(" * 1 input record 2 delete record *\n");
printf(" * 3 modify record 4 count record *\n");
printf(" * 5 save record 6 quit system *\n");
printf(" ***********************************************\n");
printf(" please enter your choice(1~6):");
scanf("%d",&choice);
/*退出系统*/
if(choice==6)
{
if(saveflag==1)
{ getchar();
printf("\n==>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'&&ch=='Y')
Save(sb,count);
}
printf("\n===>thank you for useness!");
getchar();
}
switch(choice)
{
case 1:count=Add(sb,count);break;
case 2:count=Del(sb,count);break;
case 3:Modify(sb,count);break;
case 4:Tongji(sb,count);break;
case 5:Save(sb,count);break;
}
}
/************************************************************************************/
void Tongji(SHEBEI tp[],int n)
{
int sel;
float sum;
sum=0.000000;
printf("************************统计*************************/n");
printf("*1 统计所有设备的总价值 2 统计某个部门的设备总价值*/n");
printf("*3 统计某种设备的总价值 */n");
printf("*****************************************************/n");
printf("请输入您的选择:");
scanf("%d",&sel);
if(sel==1)
{
for(i=0;i<=n;i++)
{
sum=sum+tp[i]price;
printf("所有设备的总价值为:%f",sum);
}
}
else
if(sel==2)
{ char dep;
printf("请输入要进行统计的部门:/n");
scanf("%s",dep);
for(i=0;i<=n;i++)
{
if(strcmp(tp[i]department,dep)==0)
sum=sum+tp[i]price;
printf("该部门的设备总价值为:%f",sum);
}
}
else if(sel==3)
{ char name;
printf("请输入要进行统计的设备名称:/n");
scanf("%s",name);
for(i=0;i<=n;i++)
{
if(strcmp(tp[i]name,name)==0)
sum=sum+tp[i]price;
printf("这种设备的总价值为:%f",sum);
}
}
}
[/code][em10]