C语言实现银行管理系统

C语言编写的银行管理系统关键代码

//实现功能:开卡  查询内容 存钱 取钱 转账 修改密码服务
#include
#include
#include
  struct band{
  char cardname[20];
  char name[20];
  char mima[6];
  double money;
  struct band *next;
};
struct band *head,*tail,*temp;
void init(){
  head=NULL;
  tail=NULL;
  temp=NULL;
}
//写入文件
void write(){
	FILE *fp=fopen("2.txt","w");
	temp=head;
	while(temp!=NULL){
		fprintf(fp,"%s %s %s %lf\n",temp->cardname,temp->name,temp->mima,temp->money);
		temp=temp->next;
	}
	fclose(fp);
}
//读出文件 
void read(){
	int flag=0;
	FILE *fp=fopen("2.txt","r");
	init();
	while(1){
		temp=(struct band *)malloc(sizeof(struct band));
		flag=fscanf(fp,"%s %s %s %lf",temp->cardname,temp->name,temp->mima,&temp->money);
		if(flag==-1){
			break;
		}else{
			temp->next=NULL;
		    if(head==NULL){
			head=temp;
			tail=temp;
		   }else{
			tail->next=temp;
			tail =tail->next ;
		   } 
		}
	}
	fclose(fp);
}
//提示信息 
void printUsage(){
   printf("\n\t=======欢迎您使用中国银行=======\n");
   printf("\n\t=======开卡请按1========\n");
   printf("\n\t=======查询请按2========\n");
   printf("\n\t=======存钱请按3========\n");
   printf("\n\t=======取钱请按4========\n");
   printf("\n\t=======转账请按5========\n");
   printf("\n\t=======读文档请按6========\n");
   printf("\n\t=======修改密码请按7========\n");
   printf("\n\t=======结束请按0========\n");
   printf("\n\t=======请输入您的选择:========\n");
   printf("\n\t======(使用前请先使用读文档功能)========\n");
}
//开卡 
void createCard(){
	//开始进行结点 
    char a[20];
    printf("\n=======欢迎您使用开卡功能:========\n");
    printf("\n=======请输入您的卡号:========\n");
    scanf("%s",a);
   
    struct band *pd;
	pd=head;
    while(pd!=NULL){
    if(strcmp(a,pd->cardname)==0){
				printf("您输入的卡号已存在,请重新输入。\n");
				scanf("%s",a);
				pd=head;
			}else{
				pd=pd->next;
			}
}      
          if(pd==NULL){
				printf("可开卡!请再次输入:\n");
			}
  
    temp=(struct band *)malloc(sizeof(struct band)); 
    temp->next=NULL;
    scanf("%s",temp->cardname );
    printf("\n=======请输入您的姓名:========\n");
    scanf("%s",temp->name );
    printf("\n=======请输入您的密码:========\n");
    scanf("%s",temp->mima );
    printf("\n=======请输入您的余额:========\n");
    scanf("%lf",&temp->money );
    //节点已产生 ,加入到链表中
    if(head==NULL){
			head=temp;
			tail=temp;
		   }else{
			tail->next=temp;
			tail =tail->next ;
		   } 
    write(); 
}

 //查询功能 
void query(){
    printf("\n=======欢迎您使用查询功能:========\n");
    char a[20];
    char b[20];
    char c[6];
    int i; 
    temp=head;//temp从头开始 
    printf("\n=======请输入您的卡号:========\n");
    scanf("%s",a);	
    while(1){
    	
       if(strcmp(a,temp->cardname)==0){
    	printf("您输入的卡号正确,请输入您的姓名!\n");
			scanf("%s",b);
			if(strcmp(b,temp->name )==0){
				printf("您输入的姓名正确,请输入您的密码(您有三次机会)!\n");//密码三次机会验证 
				for(i=1;i<=3;i++){
					scanf("%s",c);
				if(strcmp(c,temp->mima )==0){
					printf("您输入的密码正确!\n");
					printf("您的余额是%lf\n",temp->money );
					break;
				}
				else{
					printf("您输入的密码不正确!\n");
					
				}
			}
				if(i>3){
					printf("您的密码错误!"); 
					break; 
				}
				
			}
			else{
				printf("您输入的姓名不正确!\n");
				break;
			}
			break;
	}else{
		temp=temp->next ;
	}
	if(temp==NULL){
		 	printf("您输入的卡号不正确!\n");
		 	break;
		}
}
}
//存钱 
void save(){

     printf("\n=======欢迎您使用存钱功能:========\n");
    char a[20];
    char b[20];
    char c[6];
    double d;
    int i;
    temp=head;
    printf("\n=======请输入您的卡号:========\n");
    scanf("%s",a);
   while(1){
       if(strcmp(a,temp->cardname)==0){
    	printf("您输入的卡号正确,请输入您的姓名!\n");
			scanf("%s",b);
			if(strcmp(b,temp->name )==0){
				printf("您输入的姓名正确,请输入您的密码(您有三次机会)!\n");//三次机会 
				for(i=1;i<=3;i++){
					scanf("%s",c);
					if(strcmp(c,temp->mima )==0){
					printf("您输入的密码正确!\n");
					printf("您的余额是%lf\n",temp->money );
					printf("您要存的钱数为:");
					scanf("%lf",&d);
					while(d<=0)
					{
						printf("您的输入有误!请重新输入。"); 
						scanf("%lf",&d); 
					} 
					temp->money =temp->money +d;
					printf("您此时的余额为:%lf",temp->money );
					write();
							break;
				}else{
					printf("您的密码错误,请重新输入。您还有%d次机会。\n",3-i); 
				}
			}
				if(i>3){
					printf("您输入的密码不正确!\n");
					break;
				}
			}
			else{
				printf("您输入的姓名不正确!\n");
				break;
			}
			break; 
	}else{
		temp=temp->next ;
	}
	if(temp==NULL){
		 	printf("您输入的卡号不正确!\n");
		 	break;
		}

}   
}
//取钱 
void withdraw(){
     printf("\n=======欢迎您使用取钱功能:========\n");
    char a[20];
    char b[20];
    char c[6];
    double d;
    int i; 
    temp=head;
    printf("\n=======请输入您的卡号:========\n");
    	scanf("%s",a);
    while(1){
       if(strcmp(a,temp->cardname)==0){
    	printf("您输入的卡号正确,请输入您的姓名!\n");
			scanf("%s",b);
			if(strcmp(b,temp->name )==0){
				printf("您输入的姓名正确,请输入您的密码(您有三次机会)!\n");
				for(i=1;i<=3;i++){
					scanf("%s",c);
				if(strcmp(c,temp->mima )==0){
					printf("您输入的密码正确!\n");
					printf("您的余额是%lf\n",temp->money );
					printf("您要取的钱数为:");
					scanf("%lf",&d);
					if(d>temp->money ){
						printf("您的余额不足!请重新输入:");
						scanf("%lf",&d); 
					}
					temp->money =temp->money -d;
					printf("您此时的余额为:%lf",temp->money );
					write();
					break;
				}
				else{
					printf("您输入的密码错误!您还有%d次机会。\n",3-i);
				}
				}
				if(i>3){
					printf("您输入的密码不正确!\n");
					break;
				}
			}
			else{
				printf("您输入的姓名不正确!\n");
				break;
		}
		break;
	}else{
		temp=temp->next ;
	}
	if(temp==NULL){
		 	printf("您输入的卡号不正确!\n");
		 	break;
		}
}
}
//转账 
void zhuanzhang(){
	 printf("\n=======欢迎您使用转账功能:========\n");
    char a[20];
    char b[20];
    char c[6];
    char e[20];
    double d;
    int i;
    temp=head;
    struct band *pd; 
	printf("欢迎使用转账服务!\n");
	printf("请输入您的卡号:");
	scanf("%s",a); 
	while(1){
	 	if(strcmp(a,temp->name)==0){
	 		printf("您输入的卡号正确,请输入您的姓名!\n");
	 		scanf("%s",b);
	 		if(strcmp(b,temp->cardname)==0){
			 
	 			printf("您输入的姓名正确,请输入密码(您有三次机会)!\n");
	 			for(i=1;i<=3;i++){
	 				scanf("%s",c);
	 			if(strcmp(c,temp->mima)==0){
	 			printf("您输入的密码正确,请输入要转账人的账号!\n");
	 			scanf("%s",e);
	 		    pd=head;
	 		    while(1){
	 			if(strcmp(e,pd->cardname)==0){
	 				printf("您转账人的卡号在开卡当中!\n");
	 				printf("请输入您要转入的钱数(退出请按-1)!\n");
	 				scanf("%lf",&d);
	 				if(d>temp->money ){
	 					printf("您的余额不足,请重新输入。"); 
	 				
					 }else{
					 	temp->money=temp->money -d;
					 	pd->money =pd->money +d;
					 	printf("您此时的余额为:%lf元",temp->money);
					 	break;
					 }
	 				if(d==-1){
	 	             break;
                    }
	 					}else{
				    pd=pd->next ;	 	 
				}
				if(pd==NULL){
					printf("您输入的转账人的卡号不在我们开的卡当中!\n");
					break;
				}
			}
			break; 
		}else{
		printf("您的密码不正确!您还有%d次机会。",3-i); 
		}
				 } 
	 	if(i>3){
					printf("您输入的密码不正确!\n");
					break;
				}		
       }else{
			printf("您的姓名错误!"); 
			break;
    }
	break; 
	}
	else{
       	temp=temp->next ;
	   }
	   if(temp==NULL){
	   	printf("您的卡号不存在!"); 
	   	break;
	   }   
}
   write();

}
//修改密码 
void change(){
	printf("\n=======欢迎您使用修改密码功能:========\n");
    char a[20];
    char b[20];
    char c[6];
    int i; 
    temp=head;
    printf("\n=======请输入您的卡号:========\n");
    scanf("%s",a);	
    while(1){
    	
       if(strcmp(a,temp->cardname)==0){
    	printf("您输入的卡号正确,请输入您的姓名!\n");
			scanf("%s",b);
			if(strcmp(b,temp->name )==0){
				printf("您输入的姓名正确,请输入您的密码!(您有三次机会)\n");
				for(i=1;i<=3;i++){
					scanf("%s",c);
				if(strcmp(c,temp->mima )==0){
					printf("您输入的密码正确!\n");
					printf("请输入新的密码:");
					scanf("%s",temp->mima );
					printf("\n您的新密码为:%s",temp->mima ); 
					break; 
				}
				else{
					printf("您输入的密码不正确!您还有%d次机会\n",3-i);
				}
				}
				if(i>3){
					printf("您输入的密码不正确!\n");
					break;
				}
			}
			else{
				printf("您输入的姓名不正确!\n");
				break;
			}
			break;
	}else{
		temp=temp->next ;
	}
	if(temp==NULL){
		 	printf("您输入的卡号不正确!\n");
		 	break;
		}
}
write();
} 
void main(){
    int op;
    while(1){
    printUsage();
    scanf("%d",&op);
    if(op==0){
     printf("\n========感谢您的使用,欢迎下次光临!=========\n");
     break;
    }
   switch(op){
   	case 1:
   		createCard();
   		break;
   	case 2:
   		query();	
   		break;
   	case 3:
   		save();
   		break;
   	case 4:
   		withdraw();
   		break;
   	case 5:
   		zhuanzhang();
   		break;	
   	case 6:
   		read();
   		printf("文件读取成功!"); 
   		break;
   	case 7:
   		change();
   		break;
   	default:
   		printf("您的输入有误!"); 
   }
   }
   
}



你可能感兴趣的:(c语言)