主题:[讨论]求高手解决难题,查找错误,改正
[size=3]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int length=0;
char yes,no;
int menu();
struct card
{
char name[10];
char password[20];
char start[2];
float account;
float score;
}CARD[100];
read_card()
{
FILE *fp;
int i;
if((fp=fopen("card.txt","r"))!=NULL);
{
i=0;
while(!feof(fp))
{
fscanf(fp,"%s %s %s %f %f",&CARD[i].name,&CARD[i].password,&CARD[i].start,&CARD[i].account,&CARD[i].score);
i++;
}
length=i;
}
fclose(fp);
}
write_card()
{
FILE *fp;
int i;
for(i=0;;i++)
{
if((fp=fopen("card.txt","w"))==NULL)
{
printf("写入文件错误!");
exit(0);
}
scanf("%s %s %s %f %f",&CARD[i].name,&CARD[i].password,&CARD[i].start,&CARD[i].account,&CARD[i].score);
fprintf(fp,"%s %s %s %f %f",CARD[i].name,CARD[i].password,CARD[i].start,CARD[i].account,CARD[i].score);
}
fclose(fp);
}
make_card()
{
int i;
char password1[20];
for(i=1; ;i++)
{
printf("请输入您的姓名:\n");
scanf("%s",CARD[i].name);
printf("请设置您的密码:\n");
scanf("%s",CARD[i].password);
printf("请确认您的密码:\n");
scanf("%s",&password1);
if(strcmp(CARD[i].password,password1)==0)
{
strcpy(CARD[i].password,password1);
CARD[i].account=0;
CARD[i].score=0;
length=length+1;
printf("制卡成功,您的购物卡为:\n");
printf("\t姓名\t是否激活\t余额\t积分\n");
printf("%s %s %f %f\n",CARD[i].name,CARD[i].start,CARD[i].account,CARD[i].score);
printf("按任意键返回主菜单\n");
menu();
}
else
{
printf("您两次输入的密码不同!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
}
start_card()
{
int i,n=0;
char password[20];
char name1[10];
printf("请输入您的姓名:\n");
scanf("%s",&name1);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name1)==0)&&(strcmp(CARD[i].password,password)==0))
{
strcpy(CARD[i].start,"yes");
n=i+i;
}
else n=n;
}
if(n>0)
{write_card();
printf("激活成功!");
printf("按任意键返回主菜单\n");
menu();}
else
{
printf("您输入的姓名或密码有误!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
get_card()
{
int i,m=0;
float n;
char password[20];
char name1[10];
printf("请输入您的名字:\n");
scanf("%s",&name1);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name1)==0)&&(strcmp(CARD[i].password,password)==0)&&(strcmp(CARD[i].start,"yes")==0))
m=i+1;
else m=m;
}
if(m>0)
{
printf("请输入您想消费的金额:\n");
scanf("%f",&n);
if(n<=CARD[i].account)
{
CARD[i].account=CARD[i].account-n;
CARD[i].score=CARD[i].score+n;
printf("您的余额为:\n");
printf("您的积分为:\n");
printf("消费成功!");
printf("按任意键返回主菜单\n");
menu();}
else
{
printf("您的余额不足!");
write_card();
printf("按任意键返回主菜单\n");
menu();
}
}
else
{
printf("你输入的姓名或密码有误或购物卡尚未激活!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
put_card()
{
int i,m=0;
float n;
char password[20];
char name1[10];
printf("请输入您的姓名:\n");
scanf("%s",&name1);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name1)==0)&&(strcmp(CARD[i].password,password)==0)&&(strcmp(CARD[i].start,"yes")==0)) m=i+1;
else m=m;
}
if(m>0)
{
write_card();
printf("请输入您想存的金额\n");
scanf("%f",&n);
CARD[i].account=CARD[i].account+n;
printf("你的余额为:\n");
printf("%f",CARD[i].account);
printf("存款成功!");
printf("按任意键返回主菜单\n");
menu();}
else
{printf("你输入的姓名或密码有误或购物卡尚未激活!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
display_card()
{
int i,m;
char password[20];
char name[10];
printf("请输入您的姓名:\n");
scanf("%s",&name);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name)==0)&&(strcmp(CARD[i].password,password)==0)&&(strcmp(CARD[i].start,"yes")==0)) { strcpy(CARD[i].start,"yes");m=i+1;}
else m=m;
}
if(m>0)
{
write_card();
printf("\t姓名 \t是否激活 \t余额 \t积分\n");
printf("%s %s %f %f",CARD[i].name,CARD[i].start,CARD[i].account,CARD[i].score);
printf("按任意键返回主菜单");
menu();
}
else
{
printf("你输入的姓名或密码有误或购物卡尚未激活!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
int menu()
{int i;
printf("\n\n");
printf("\t\t**************MENU**************\n");
printf("\t\t 1.制卡(申请购物卡)\n");
printf("\t\t 2.发卡(激活购物卡)\n");
printf("\t\t 3.消费\n");
printf("\t\t 4.存款\n");
printf("\t\t 5.报表(购物卡信息)\n");
printf("\t\t 6.退出\n");
printf("\t\t********************************\n");
printf("请选择您所需要的服务(1-6):");
scanf("%d",&i);
while(i<1||i>6)
{printf("您输入有误,请再输入!\n");
printf("请选择您所需要的服务(1-6):");
scanf("%d",&i);
}
switch(i)
{
case 1:make_card();break;
case 2:start_card();break;
case 3:get_card();break;
case 4:put_card();break;
case 5:display_card();break;
case 6:exit(0);
}
return(0);
}
void main()
{
printf("欢迎进入购物卡系统\n");
menu();
}[/size]
#include<stdlib.h>
#include<string.h>
int length=0;
char yes,no;
int menu();
struct card
{
char name[10];
char password[20];
char start[2];
float account;
float score;
}CARD[100];
read_card()
{
FILE *fp;
int i;
if((fp=fopen("card.txt","r"))!=NULL);
{
i=0;
while(!feof(fp))
{
fscanf(fp,"%s %s %s %f %f",&CARD[i].name,&CARD[i].password,&CARD[i].start,&CARD[i].account,&CARD[i].score);
i++;
}
length=i;
}
fclose(fp);
}
write_card()
{
FILE *fp;
int i;
for(i=0;;i++)
{
if((fp=fopen("card.txt","w"))==NULL)
{
printf("写入文件错误!");
exit(0);
}
scanf("%s %s %s %f %f",&CARD[i].name,&CARD[i].password,&CARD[i].start,&CARD[i].account,&CARD[i].score);
fprintf(fp,"%s %s %s %f %f",CARD[i].name,CARD[i].password,CARD[i].start,CARD[i].account,CARD[i].score);
}
fclose(fp);
}
make_card()
{
int i;
char password1[20];
for(i=1; ;i++)
{
printf("请输入您的姓名:\n");
scanf("%s",CARD[i].name);
printf("请设置您的密码:\n");
scanf("%s",CARD[i].password);
printf("请确认您的密码:\n");
scanf("%s",&password1);
if(strcmp(CARD[i].password,password1)==0)
{
strcpy(CARD[i].password,password1);
CARD[i].account=0;
CARD[i].score=0;
length=length+1;
printf("制卡成功,您的购物卡为:\n");
printf("\t姓名\t是否激活\t余额\t积分\n");
printf("%s %s %f %f\n",CARD[i].name,CARD[i].start,CARD[i].account,CARD[i].score);
printf("按任意键返回主菜单\n");
menu();
}
else
{
printf("您两次输入的密码不同!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
}
start_card()
{
int i,n=0;
char password[20];
char name1[10];
printf("请输入您的姓名:\n");
scanf("%s",&name1);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name1)==0)&&(strcmp(CARD[i].password,password)==0))
{
strcpy(CARD[i].start,"yes");
n=i+i;
}
else n=n;
}
if(n>0)
{write_card();
printf("激活成功!");
printf("按任意键返回主菜单\n");
menu();}
else
{
printf("您输入的姓名或密码有误!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
get_card()
{
int i,m=0;
float n;
char password[20];
char name1[10];
printf("请输入您的名字:\n");
scanf("%s",&name1);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name1)==0)&&(strcmp(CARD[i].password,password)==0)&&(strcmp(CARD[i].start,"yes")==0))
m=i+1;
else m=m;
}
if(m>0)
{
printf("请输入您想消费的金额:\n");
scanf("%f",&n);
if(n<=CARD[i].account)
{
CARD[i].account=CARD[i].account-n;
CARD[i].score=CARD[i].score+n;
printf("您的余额为:\n");
printf("您的积分为:\n");
printf("消费成功!");
printf("按任意键返回主菜单\n");
menu();}
else
{
printf("您的余额不足!");
write_card();
printf("按任意键返回主菜单\n");
menu();
}
}
else
{
printf("你输入的姓名或密码有误或购物卡尚未激活!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
put_card()
{
int i,m=0;
float n;
char password[20];
char name1[10];
printf("请输入您的姓名:\n");
scanf("%s",&name1);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name1)==0)&&(strcmp(CARD[i].password,password)==0)&&(strcmp(CARD[i].start,"yes")==0)) m=i+1;
else m=m;
}
if(m>0)
{
write_card();
printf("请输入您想存的金额\n");
scanf("%f",&n);
CARD[i].account=CARD[i].account+n;
printf("你的余额为:\n");
printf("%f",CARD[i].account);
printf("存款成功!");
printf("按任意键返回主菜单\n");
menu();}
else
{printf("你输入的姓名或密码有误或购物卡尚未激活!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
display_card()
{
int i,m;
char password[20];
char name[10];
printf("请输入您的姓名:\n");
scanf("%s",&name);
printf("请输入您的密码:\n");
scanf("%s",&password);
read_card();
for(i=0;i<=length;i++)
{
if((strcmp(CARD[i].name,name)==0)&&(strcmp(CARD[i].password,password)==0)&&(strcmp(CARD[i].start,"yes")==0)) { strcpy(CARD[i].start,"yes");m=i+1;}
else m=m;
}
if(m>0)
{
write_card();
printf("\t姓名 \t是否激活 \t余额 \t积分\n");
printf("%s %s %f %f",CARD[i].name,CARD[i].start,CARD[i].account,CARD[i].score);
printf("按任意键返回主菜单");
menu();
}
else
{
printf("你输入的姓名或密码有误或购物卡尚未激活!\n");
printf("按任意键返回主菜单\n");
menu();
}
}
int menu()
{int i;
printf("\n\n");
printf("\t\t**************MENU**************\n");
printf("\t\t 1.制卡(申请购物卡)\n");
printf("\t\t 2.发卡(激活购物卡)\n");
printf("\t\t 3.消费\n");
printf("\t\t 4.存款\n");
printf("\t\t 5.报表(购物卡信息)\n");
printf("\t\t 6.退出\n");
printf("\t\t********************************\n");
printf("请选择您所需要的服务(1-6):");
scanf("%d",&i);
while(i<1||i>6)
{printf("您输入有误,请再输入!\n");
printf("请选择您所需要的服务(1-6):");
scanf("%d",&i);
}
switch(i)
{
case 1:make_card();break;
case 2:start_card();break;
case 3:get_card();break;
case 4:put_card();break;
case 5:display_card();break;
case 6:exit(0);
}
return(0);
}
void main()
{
printf("欢迎进入购物卡系统\n");
menu();
}[/size]