#include <stdio.h>
#define N 50 /*假设有50名学生*/
struct student
{
char number[20]; /* 存储用户的卡号信息*/
char name[20]; /* 存储用户的姓名信息*/
int lose; /* 记录用户的挂失锁定信息*/
float value; /* 存储用户的卡上即时余额*/
};
void Solution_Bynum();
void main() // 主函数
{
char choose, yse_no;
do
{
Main_Menu();
choose=getche();
switch(choose)
{
case '1':Create_File(); break; /*创建*/
case '2':Consumption(); break; /*消费*/
case '3':Add_Money(); break; /*充值*/
case '4':Add_Newcard(); break; /*添加加新饭卡*/
case '5':Logout_Card(); break; /*注销旧饭卡*/
case '6':Setup_Remove(); break; /*设置与解除挂失*/ // 该功能已给出主要函数,请同学自己实现Setup_Remove()
case '7':Administrator_Enter(); break; /*管理员查询信息*/
case '0':break; /*创建*/
default: printf("\n您输入的信息不正确\n");break;
}
if(choose=='0')break;
printf("您要回到主菜单吗?(Y/N)\t\n");
do
{
yse_no=getchar();
} while (yse_no!='Y'&&yse_no!='N'&&yse_no!='n');
}while(yse_no=='Y'||yse_no=='y');
}
void Main_Menu() // 主菜单系界面
{
printf("学生食堂饭卡管理系统\n");
printf("1:保存文件\n");
printf("2:买饭\n");
printf("3:充值\n");
printf("4:添加新饭卡\n");
printf("5:注销旧饭卡\n");
printf("6:挂失与解挂\n");
printf("7:查看全部信息(管理员)\n");
printf("0:退出\n");
printf("请选择:\n");
}
void Create_File() // 添加饭卡数据信息,把信息写入文件中
{
FILE* fp=NULL;
int i=0;
char yes_no;
struct student card = {0};
fp=fopen ("card.txt", "w") ;
if(fp==NULL)
{
printf("对不起,系统无法打开该文件。\n");
return;
}
printf("请您按要求填写以下信息\n");
do
{
printf("\n请输人要创建的新饭卡的卡号:");
scanf ("%s",card.number) ;
printf("\n 请输人持有该饭卡学生的名字:");
scanf("%s",card.name);
printf("\n请输人该饭卡的初始充值金额:");
scanf("%f",&card.value);
fprintf(fp, "%10s%10s%10d%10.2f\n",card.number, card.name, card.lose, card.value);
printf("创建成功! 要继续创建饭卡吗?(Y/N)");
do
yes_no=getche();
while(yes_no!='Y'&&yes_no!='y'&& yes_no!='N'&& yes_no!='n');
}while (yes_no=='Y'|| yes_no=='y');
fclose(fp);
}
void Consumption() // 消费
{
FILE* fp=NULL;
int i=0,j=0,n=0,find=0;
float temp=0;
struct student card[N]={0}; // N自定义一个数值
char num1[20]="",yes_no;
fp=fopen("card.txt","r");
if(fp==NULL)
{
printf("对不起,系统无法打开该文件。\n");
return;
}
while(feof(fp)==0)
{
fscanf (fp, "%s%s%d%f\n", card[i].number, card[i].name, &card[i].lose,
&card[i].value);
i++;
}
fclose(fp);
do
{
printf("\n请输入要消费的饭卡卡号:");
scanf("%s", &num1);
for(j=0;j<i;j++)
if(strcmp(card[j].number, num1)==0)
{
find=1;n=j;break;
}
if(find==1) break;
else
{
printf("对不起,系统找不到该饭卡的信息!\n要继续选择吗?(Y/N)");
do{
yes_no=getchar();
}while(yes_no!='Y'&&yes_no!='y'&& yes_no!='N'&& yes_no!='n');
}
}while(yes_no== 'y'|| yes_no== 'Y');
if(find==1)
{
if(card[n].lose==1)
{
printf("该饭卡已挂失!\n");
return;
}
else if(card[n].value<5)
{
printf("您的饭卡余额不足,请及时充值\n");
return;
}
else
{
printf("\n请您输人消费金额:");
scanf("%f",&temp);
if(card[n].value>0)
{
printf("\n消费前:¥%.2f\n",card[n].value);
card[n].value=card[n].value-temp;
printf("\n消费后:¥%.2f\n",card[n].value);
}
}
}
fp=fopen("card.txt","w");
for(j=0; j<i; j++)
{
fprintf(fp, "%10s%10s%10.2f\n", card[j].number, card[j].name, card[j].lose, card[j].value);
}
fclose(fp);
}
void Add_Money() // 充值
{
FILE *fp=NULL;
struct student card[N]={0};
int i=0,j=0,n=0,find=0,mtemp=0;
char num2[20]="",yes_no='\0';
fp=fopen("card.txt","r");
if(fp==NULL)
{printf("对不起,系统无法打开该文件.\n");return;}
while(feof(fp)==0)
{
fscanf(fp,"%s%s%d%f\n", card[i].number, card[i].name, &card[i].lose, &card[i].value);
i++;
}
fclose(fp);
do{
printf("\n请您输要充值的卡卡号:");
scanf("%s",num2);
for(j=0;j<i;j++)
if(strcmp(card[j].number,num2)==0)
{find=1;n=j;break;}
if(find==1)break;
else
{
printf("对不起,系统找不到该饭卡的信息!n要继续吗?(Y/N)");
do{
yes_no=getche();
}while(yes_no!='Y'&&yes_no!='N'&&yes_no!='y'&&yes_no!='n');
}
}while(yes_no=='Y'||yes_no== 'y');
if(find==1)
{
printf("\n请输人您的充值金额:");
scanf("%d",&mtemp);
printf("\n充值前:¥%.2f",card[n].value);
card[n].value=card[n].value + mtemp;
printf("\n充值后:¥%.2f",card[n].value);
}
fp=fopen("card.txt","w");
if(fp==NULL)
{printf("对不起系统无法打开该文件。n");return;}
for(j=0;j<i;j++)
{
fprintf(fp,"%10s%10s%10d%10.2f\n",card[j].number,card[j].name,
card[j].lose,card[j].value);
}
fclose(fp);
}
void Add_Newcard() // 添加新卡
{
FILE *fp=NULL;
int i=0,j=0;
char yes_no;
struct student card[N]=(0);
fp=fopen("card.txt","r");
if(fp==NULL)
{
printf("对不起,系统无法打开该文件.\n");return;
}
while(feof(fp)==0)
{
fscanf(fp,"%s%s%d%f\n",card[i].number,card[i].name,&card[i].lose,
&card[i].value);
i++;
}
fclose(fp);
do{
printf("\n请输入要创建的新饭卡的卡号:");
scanf("%s", card[i].number);
printf("\n 请输人持有该饭卡学生的名字:");
scanf("%s", card[i].name);
printf("\n请输人该饭卡的初始充值金额:");
scanf("%f", &card[i].value);
i++;
printf("创建成功!要继续创建饭卡吗?(Y/N)");
do{
yes_no=getchar();
}while(yes_no!='Y'&&yes_no!='N'&&yes_no!='y'&&yes_no!='n');
}while(yes_no=='y'||yes_no== 'Y');
fp=fopen("card.txt","w");
if(fp==NULL)
{
printf("对不起,系统无法打开该文件\n");return;
}
for(j=0;j<i;j++)
fprintf(fp,"%10s%10s%10d%10.2f\n", card[j].number, card[j].name,
card[j].lose, card[j].value);
fclose(fp);
}
void Logout_Card() // 注销旧饭卡
{
FILE* fp=NULL;
int i=0,j=0,m=0,n=0;
char yes_no='\0',num2[20]="";
struct student card[N]={0};
fp=fopen("card.txt","r");
if(fp==NULL)
{printf("对不起,系统无法打开该文件.\n");return;}
while(feof(fp)==0)
{
fscanf(fp,"%s%s%d%f\n",card[i].number,card[i].name,
&card[i].lose, &card[i].value);
i++;
}
fclose(fp);
do
{
printf("\n请输入要注销的饭卡卡号:");
scanf("%s",num2);
for(j=0;j<i;j++)
if(strcmp(card[j].number,num2)==0)
{m=1;n=j;break;}
if(m==1)
{
printf("注销成功!");
for(j=n;j<i-1;j++)
{
card[j]=card[j+1];
}
}
else
printf("对不起,系统无法打开该文件.\n");
}while(yes_no=='y'||yes_no=='y');
fp=fopen("card.txt",