程序代码:
#include <stdio.h>
#include "string.h"
#include <conio.h>
#include <stdlib.h>
typedef struct _bank{
char name[30];
long surplus;
struct _bank *next;
}bank;
bank *build(bank *);
void deposit(bank *);
void take_out(bank *);
void main_menu();
bank *find(bank *);
bank *cancellation(bank *,bank *);
int main(){
bank *head=NULL,*p;
char a;
main_menu();
a=getchar();
switch(a) {
case '4':head=build(head);break;
case '6':printf("==========谢谢使用,再见。========");_sleep(1500);exit(0);
case '2':
case '3':
case '1':
case '5':p=find(head);if(p==NULL){printf("===========操作失败,谢谢使用。==========");break;}
if(a=='1'){deposit(p);break;}
if(a=='2'){take_out(p);break;}
if(a=='3'){head=cancellation(head,p);break;}
if(a=='5')printf("==========账号信息=============\n帐号名:%s\n余额:%d",p->name,p->surplus);getch();break;
default:putchar('\a'); }
return 0;}
bank *build(bank *head){
bank *p,*q;
if((p=(bank *)malloc(sizeof(bank)))==NULL){
printf("========服务无效,请重试。======");
return NULL;}
printf("========请输入要建立的账号的姓名:");
fflush(stdin);
scanf("%s",p->name);
printf("=========请输入开户金额:");
fflush(stdin);
scanf("%l",p->surplus);
p->next=NULL;
if(head==NULL)return p;
else{q=head;while(q->next!=NULL)q=q->next;
q->next=p;return head;}}
bank *find(bank *head){
bank *p;
char a[30]={'\0'};
p=head;
printf("=============请输入你所要操作的账户名:");
fflush(stdin);
scanf("%s",a);
if(head==NULL)return NULL;
for(;strcmp(a,p->name)&&p!=NULL;)p=p->next;
if(p==NULL)return NULL;
else return p;}
void take_out(bank *p){
long a;
printf("============请输入取出的金额:");
fflush(stdin);
scanf("%l",&a);
if(a>p->surplus){printf("=============余额不足,谢谢使用。===============");return ;}
p->surplus=p->surplus-a;
return ;}
void deposit(bank *p){
long a;
printf("==============请输入要存入的金额:");
fflush(stdin);
scanf("%l",&a);
p->surplus=p->surplus+a;
return ;}
bank *cancellation(bank *head,bank *p){
bank *q;
q=head;
if(p==head){head=head->next;free(q);return head;}
while(q->next!=p)q=q->next;
q->next=p->next;
return head;}
void main_menu(){
system("CLS");
printf("^==================================================^\n");
printf("* 1、存款 2、取款 3、注销账户 *\n");
printf("^==================================================^\n");
printf("^==================================================^\n");
printf("* 4、开户 5、查询 6、退出==========*、n");
printf("请选择服务项目:");
}