本人是C的初学者,刚把谭XX的那本书学完,老师布置了个小设计~~
要我们做个银行帐户管理系统,要求能新建(添加)帐户,查询帐户,取钱,存钱,修改密码这些功能~
我编译成功,运行的时候,新建第一个帐户,并执行查询什么别的功能都是OK,
但我新建立了第二个帐户,第三个的时候,执行别的功能时```有时就没的反应,请高手指点下
由于是新手,有很多毛病和改进的,也希望指点下
以下是我写的代码,不足之处,请多多包涵 本人在线等侯指导,谢谢
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct account)
int n=1; /*全局变量n,控制帐户编号的累加*/
struct account
{
int num; /帐户编号/
long password;
char name[15];
long money;
struct account *next;
};
struct account *creat(void) /*创建(添加)帐户*/
{
int flag;long first_password,second_password;
struct account *p1,*p2,*head;
p1=(struct account*)malloc(LEN);
printf("please input your name:\n");
scanf("%s",p1->name);
printf("input new password:\n");
scanf("%ld",&first_password);
printf("input password again:\n");
for(flag=0;flag<3;flag++) /*建立帐户时,输入密码会要求输入第二遍*/
{
scanf("%ld",&second_password);
if(first_password==second_password)
{
p1->password=first_password;break;
}
else printf("input error,please input again:");
}
if(flag〈3) /*flag值为3时,密码已经输入错误三次*/
{
printf("how much money do you want to save?");
scanf("%ld",&p1->money);
(p1->num)=n;n++; /*给帐户进行编号*/
p2=p1;
if(p1->num==1)
head=p1;
else p2->next=p1;
printf("%ld\n%s\n%ld\n%ld\n",p1->num,p1->name,p1->password,p1->money);
return(head);
}
}
void change(struct account *head) /*修改密码*/
{
struct account *p;int password,num,flag;long first_password,second_password;
p=head;
printf("please input number:\n");
scanf("%ld",&num);
while(p->num!=num)
{
p=p->next;
}
printf("please input password:\n");
scanf("%ld",&password);
if(password==(p->password))
{
printf("input new password:\n");
scanf("%ld",&first_password);
printf("input password again:\n");
for(flag=0;flag<3;flag++)
{
scanf("%ld",&second_password);
if(first_password==second_password)
{
p->password=first_password;break;
}
else printf("error,please input password again:");
}
printf("%ld",p->password);
}
else printf("password is wrong!");
return;
}
void find(struct account *head) /*查询帐户*/
{
int password,num;struct account *p;
printf("please input number:\n");
scanf("%ld",&num);
p=head;
while(p->num!=num)
{
p=p->next;
}
printf("please input password:\n");
scanf("%ld",&password);
if(password==(p->password))
{
printf("%s",p->name);
printf("\n%ld\n%ld",p->num,p->money);
}
else printf("error");
return;
}
void save(struct account *head) /*存钱*/
{
int password,num,money;struct account *p;
printf("please input number:\n");
scanf("%ld",&num);
p=head;
while(p->num!=num)
{
p=p->next;
}
printf("please input password:\n");
scanf("%ld",&password);
if(password==(p->password))
{
printf("how much money do you want to save?");
scanf("%ld",&money);
p->money=(p->money)+money;
printf("%ld",p->money);
}
else printf("error");
return;
}
void load(struct account *head) /*取钱*/
{
int money,num,password;struct account *p;
printf("please input number:\n");
scanf("%ld",&num);
p=head;
while(p->num!=num)
{
p=p->next;
}
printf("please input password:\n");
scanf("%ld",&password);
if(password==(p->password))
{
printf("how much money do you want to load?");
scanf("%ld",&money);
if(money>(p->money))
printf("your money is not enough!\n");
else
p->money=p->money-money;
printf("%ld",p->money);
}
else printf("your password is wrong!");
return;
}
void main()
{
struct account *head;
int x=100;head=0;
while(x!=0)
{
printf("1.find account 2.save money 3.load money 4.creat account 6.change password 0.exit\n");
scanf("%d",&x); /*输入序号,执行相应功能,如输入4,执行创建新帐户*/
switch(x)
{
case 1:find(head);break;
case 2:save(head);break;
case 3:load(head);break;
case 4:head=creat();break; /*必须先创建帐户,才能执行其他功能*/
case 5:change(head);break;
case 0:break;
default:printf("error\n");
}
}
}