<5>c语言实现ATM存取款系统(文件、排序)
来源:互联网 发布:什么是淘宝长尾词 编辑:程序博客网 时间:2024/05/22 16:38
#include<stdio.h>#include<String.h>#include<math.h>#include<stdlib.h>#include<windows.h>#define N 100typedef struct info {char account[21];char user[21];long dsit;long pw;struct info *next;}Info;void login();void regist();int menu();int refer();int deposit();int withdraw();int transfer();int resetPW();int judgePW(long ps);int search(char *s,int psw);int searchAC(char *s);void OutFile();void InFile();void Sort();void Print();Info *pt, *head = NULL, *tail, *p0;main(){int choice = -1;OutFile();while(1){printf(" 欢迎使用银行存取款系统 \n");printf("\n ================================ \n");printf(" 1.登录 \n");printf(" 2.注册 \n");printf(" 3.退出 \n");printf("\n ================================ \n");printf("\t* 请输入您的选择:");scanf("%d", &choice);switch(choice){case 1:system("cls"); login();break;case 2:system("cls");regist();break;case 3: Sort(); Print();InFile();exit(0);break;default:printf("输入有误,请重输...\n");break;}}}/////////////////登录//////////////////////////////////void login(){int flag;char my_act[100];long password;int X = -1;printf("\t\t=================****登录****=================\n\n");printf("\t\t* 输入账户: ");scanf("%s", my_act);printf("\n");printf("\t\t* 输入密码: ");scanf("%ld", &password);flag = search(my_act,password);if(flag == 1) {printf("\t\t登录成功,正在跳转页面...\n");p0 = pt;Sleep(2000);system("cls");while(1){X = menu();//成功进入if(X == 1){system("cls");menu();}elsereturn;}}else if(flag == 0) {printf("\t\t账号或密码错误,请重新输入...\n");Sleep(2000);system("cls");return;}}int search(char *s,int psw) {if(head == NULL) {return 0;}pt = head;while(1) {if(strcmp(pt->account, s)==0&&pt->pw==psw) {return 1;}if(pt->next == NULL) break;pt = pt->next;}return 0;}int searchAC(char *s) {if(head == NULL) {return 0;}pt = head;while(1) {if(strcmp(pt->account, s)==0) {return 1;}if(pt->next == NULL) break;pt = pt->next;}return 0;}////////////////////////注册///////////////////////////void regist(){printf("\t\t=================****注册****=================\n\n");struct info *p1;p1 = (Info *)malloc(sizeof(Info));p1->next = NULL;int flag;printf("\t\t请输入您的账户....\n");printf("\t\t");scanf("%s", p1->account);printf("\t\t请输入用户名......\n");printf("\t\t");scanf("%s", p1->user);printf("\t\t输入您的密码......\n");printf("\t\t");scanf("%ld", &p1->pw);flag = search(p1->account,p1->pw);if(flag == 1) {printf("\t\t账户已存在,请重新注册......\n");Sleep(2000);system("cls");return;}else if(flag == 0) {printf("\t\t注册成功\n");//flagN = N; //标记当前注册账户是链表中的第几个p1->dsit = 0;if(head == NULL) {head = p1;tail = p1;//n ++;Sleep(2000);system("cls");return;}else {tail->next = p1;tail = p1;//n ++;Sleep(2000);system("cls");return;}}}////////////主页面/////////////////////////////////////int menu(){printf("\t\t=================****主页面****=================\n\n");printf("\t\t====================****************=================\n\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\n");int choice = -1;int Y = -1;printf("\t\t *");printf("我的选择:");scanf("%d", &choice);while(1){switch(choice){case 1:system("cls");Y = refer();break;case 2:system("cls");Y = deposit();break;case 3:system("cls");Y = withdraw();break;case 4:system("cls");Y = transfer();break;case 5:system("cls");Y = resetPW();break;case 6:system("cls");return 0;default:printf("输入有误,请重输...\n");return 1;}switch(Y){case 0:printf("出现错误!\n");return 0;break;case 1: /*正常情况menu返回1重新进入menu*/return 1;break;case 2:Y = deposit();break;case 3:Y = withdraw();break;case 4:Y = transfer();break;case 5:Y = resetPW();break;}}}//////////////////////查询/////////////////////////////int refer(){printf("\t\t=================****查询****=================\n\n");printf("\t\t* 您的账户余额为:\n");printf("\t\t* %ld\n", p0->dsit);Sleep(3000);system("cls");return 1;}//////////////////////////取款//////////////////////////int deposit(){long rMoney;printf("\t\t=================****存款****=================\n\n");printf("\t\t* 输入您要存入的钱数(整百):\n\n");printf("\t\t* ");scanf("%ld", &rMoney);if(rMoney%100 != 0){printf("\t\t* 出现错误,请重试...\n\n");return 2;}p0->dsit += rMoney;printf("\t\t* 存钱成功...\n\n");Sleep(2000);system("cls");return 1;}//////////////////////////取款/////////////////////////int withdraw(){long omoney;printf("\t\t=================****取款****=================\n\n");printf("\t\t* 输入您要取出的钱数...\n\n");printf("\t\t* ");scanf("%ld", &omoney);if(omoney%100 != 0){printf("\t\t* 出现错误...\n\n");Sleep(2000);system("cls");return 3;}if(omoney > p0->dsit){printf("\t\t* 余额不足...\n\n");system("cls");return 1;}p0->dsit -= omoney;printf("\t\t* 取钱成功...\n\n");Sleep(2000);system("cls");return 1;}////////////////转账////////////////////////////////int transfer(){struct info *pFlag;char ob[20];long money;int flag;printf("\t\t=================****转账****=================\n\n");printf("\t\t* 请输入您要转账的账户...\n");printf("\t\t* ");scanf("%s", ob);flag = searchAC(ob);pFlag = pt;if(flag == 1) {printf("\t\t* 输入要转出的钱数(整百):\n");printf("\t\t* ");scanf("%ld", &money);if(money%100 != 0){printf("\t\t *");printf("\t\t出现错误,请重试...\n");Sleep(2000);system("cls");return 1;}if(money > p0->dsit){printf("\t\t* 余额不足.....\n");return 1;}p0->dsit -= money;pFlag->dsit += money;Sleep(2000);printf("\t\t* 转账成功...");Sleep(2000);system("cls");return 1;}else if(flag == 0) {printf("\t\t* 对不起,您输入的账户不存在...\n");printf("*****************请稍等*********************");Sleep(2000);system("cls");return 1;}}///////////////////////修改密码////////////////////////////////////////////int resetPW(){long password1, password2;long newpassword;printf("\t\t=================****修改密码****=================\n\n");printf("\t\t* 请输入原始密码...\n\n");printf("\t\t* ");scanf("%ld", &password1);printf("\t\t* 请再次输入...\n\n");printf("\t\t* ");scanf("%ld", &password2);if(p0->pw == password1 && p0->pw == password2){printf("\t\t* 输入新密码...\n\n");printf("\t\t* ");scanf("%ld", &newpassword);if(judgePW(newpassword) == 1){p0->pw = newpassword;printf("\t\t* 密码重置成功...\n\n");Sleep(2000);system("cls");return 1;}else{printf("\t\t* 输入的密码不符合规则,请重试...\n");Sleep(2000);system("cls");return 5;}}else{printf("\t\t* 密码错误...\n");Sleep(2000);system("cls");return 1;}return 1;}//////////////////////////密码判断////////////////////////////////////////int judgePW(long ps){int n, k;for(k=0; k<6; k++){n = ps%10;ps = ps/10;if(n < 0 || n > 9){return 2;}}return 1;}///////////////////////文件读取////////////////////////////////////////////void OutFile(){FILE *fp;Info *p; fp = fopen("record.txt","rb+");if(!fp) {printf("ERROR!");return;} while(1){ p = (Info *)malloc(sizeof(Info)); fread(p,sizeof(Info),1,fp); if(p->next==NULL){ tail->next=p; tail=p; break;}else{if(head == NULL){p->next = NULL;head = tail = p;}else {p->next=NULL;tail->next=p;tail=p;} } } fclose(fp);}//////////////////////文件读入////////////////////////////////////////////void InFile(){FILE *ft;Info *p;p = head; while(p->next != NULL){ ft = fopen("record.txt","wb+"); fwrite(p, sizeof(Info), 1, ft); p = p->next; } fwrite(p, sizeof(Info), 1, ft); fclose(ft);}//////////////////////排序/////////////////////////////////////////////void Sort(){ Info *p, *pt, *p1, *p2, *p0, *px; /*链表冒泡法排序*/ while(1) { p0 = head->next; /*包含头结点的比较为特殊情况,先进行比较*/ if(head->pw > p0->pw) { head->next = p0->next; p0->next= head; head = p0; }//此时p0等于head //print(head); pt = head; while(1) {//p0为前置节点,p1,p2为要比较的节点 p0 = pt; p1 = p0->next; p2 = p1->next; if(p1->pw > p2->pw) { p1->next = p2->next; p0->next = p2; p2->next = p1; if(p1->next==NULL || p1==px) { px = p2; break; } } else { if(p2->next==NULL || p2==px) { px = p1; break; } } pt = pt->next; } /*px的作用:每进行一次循环比较,就将一个最大的数 推到最后,而px就记录这个最大数的前面一个数,排序完后 px == head->next*/ if(px == head->next) break; }}///////////////////////账号查询////////////////////////////////////////void Print(){Info *p;p=head;while(1){printf("%ld ", p->pw);if(p->next == NULL) break;p=p->next;}}