weixin_50917758 2020-12-22 20:44 采纳率: 66.7%
浏览 13
已采纳

如何对我的openaccount的功能进行循环

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>


void showcode(void);
void openaccount(int sequence[10][15]);


struct Account{
        char name[100];
        char id[15];
        char address[200];
        char ccode[4];
        char bcode[3];
        char Dob[10];
        int balance;
        struct tm *Doo;
        char accountnumber[11];
};


void main(void){
        char option='\0', ch='\0', in='\0';        
        int count=0;
        int sequence[10][15];
        FILE *fp;
        if((fp=fopen("Sequence.txt","r"))==NULL){
                if((fp=fopen("Sequence.txt","w"))==NULL){
                        printf("Fail to open the file!\n");
                        exit(EXIT_FAILURE);
                }
                fwrite(sequence, 150*sizeof(int), 1, fp);
        }        
        else{
                fread(sequence, 150*sizeof(int), 1, fp);
        }
        fclose(fp);
        do{        
                
                printf("1) Show city code and branch code\n");
                printf("2) Open an account\n");
                printf("3) Show account details\n");
                printf("4) Show list of accounts\n");
                printf("5) Deposit in an account\n");
                printf("6) Withdraw from an account\n");
                printf("7) Transfer money\n");
                printf("8) Transaction details\n");
                printf("9) Close an account\n");
                printf("10)Quit\n");
                printf("\n");
                printf("Option: ");
                count=0;
                option='\0', ch='\0', in='\0';
                while((in=getchar())!='\n'){
                        count++;
                        option=ch;
                        ch=in;
                }
                if((count==1)&&(ch=='0')){
                        ch='a';
                }                
                if(count>2){
                        ch='a';
                }        
                if(count==2){                
                        if((option=='1')&&(ch=='0')){
                                ch='0';
                        }
                        else{
                                ch='a';
                        }
                }
                printf("\n");
                switch(ch){
                        case '1': showcode(); break;
                        case '2': openaccount(sequence); break;
                        case '3': printf("3\n"); break;
                        case '4': printf("4\n"); break;
                        case '5': printf("5\n"); break;
                        case '6': printf("6\n");break;
                        case '7': printf("7\n");break;
                        case '8': printf("8\n");break;
                        case '9': printf("9\n");break;
                        case '0': printf("break\n");break;
                        default: printf("Unknown option!\n");
                                 printf("\n"); 
                }
        }while(ch!='0');
        exit(0);
}

void showcode(void){
        FILE *fp;
        char name1[10], name2[10], name3[10];
        char code1[4], code2[4], code3[4];
        char buffer[20], citycode[4], choice;
        char *name, *code;
        int len, rmd;
        if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
                printf("Fail to open the file!\n");
                exit(EXIT_FAILURE);
        }
        printf("City\t\t\tcode\t\t\tCity\t\t\tcode\t\t\tCity\t\t\tcode\n");
        printf("_____________________________________________________________________________________________________________________________\n");        
        while(fgets(buffer, 20, fp)!=NULL){
                len=strlen(buffer);        
                buffer[len-1]='\0';
                code=strtok(buffer, "\t");
                name=strtok(NULL, "\t");
                if(strlen(code)==3){
                        rmd=(atoi(code))%3;
                        switch(rmd){
                                case 1:
                                        strcpy(name1, name);
                                        strcpy(code1, code);
                                        break;
                                case 2:
                                        strcpy(name2, name);
                                        strcpy(code2, code);
                                        break;
                                case 0: strcpy(name3, name);
                                        strcpy(code3, code);
                                        printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
                                        break;
                        }
                }
        }
        printf("%-10s\t\t%-10s\n", name1, code1);
        printf("\n");
        
        fseek(fp, 0, SEEK_SET);
        printf("Enter the city code: ");        
        scanf("%s", citycode);
        getchar();
        printf("\n");
        if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
                printf("Fail to open the file!\n");
                exit(EXIT_FAILURE);
        }
        while(fgets(buffer, 20, fp)!=NULL){
                len=strlen(buffer);        
                buffer[len-1]='\0';
                code=strtok(buffer, "\t");
                name=strtok(NULL, "\t");                
                if((strcmp(code, citycode))==0){
                        break;
                }
        }
        printf("Branch name\t\tcode\t\t\tBranch name\t\tcode\t\t\tBranch name\t\tcode\n");
        printf("_____________________________________________________________________________________________________________________________\n");        
        while(fgets(buffer, 20, fp)!=NULL){                        
                len=strlen(buffer);        
                buffer[len-1]='\0';
                code=strtok(buffer, "\t");
                name=strtok(NULL, "\t");
                if(strlen(code)==3){
                        break;
                }
                rmd=(atoi(code))%3;
                switch(rmd){
                        case 1:
                                strcpy(name1, name);
                                strcpy(code1, code);
                                break;
                        case 2:
                                strcpy(name2, name);
                                strcpy(code2, code);
                                break;
                        case 0: strcpy(name3, name);
                                strcpy(code3, code);
                                printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
                                break;
                }
        }
        switch(rmd){
                case 1:
                        printf("%-10s\t\t%-10s\n", name1, code1);
                        break;                        
                case 2:
                        printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2);
                        break;
                case 0:
                        break;
        }
        fclose(fp);
        printf("\n");
        printf("Do you want to see more city and branch codes (Y/N): ");
        scanf("%c", &choice);
        getchar();
        if(choice=='Y'){
                                
                showcode();
        }
        printf("\n");
}
                        

void openaccount(int sequence[10][15]){
        struct Account *account=(struct Account*)malloc(sizeof(struct Account));        
        int blc,i,j,number,len; 
        char ch, orig[5], seq[5];
        FILE *fp, *ptr;
        time_t t;
        printf("Enter the name of the account holder: ");
        while((ch=getchar())!='\n'){
                account->name[i]=ch;
                i++;                
        }
        printf("Enter the Id number of the account holder: ");
        scanf("%s", account->id);
        getchar();        
        printf("Enter the address of the account holder: ");
        i=0;        
        while((ch=getchar())!='\n'){
                account->address[i]=ch;
                i++;                
        }
        printf("Enter city code: ");
        scanf("%s", account->ccode);
        getchar();
        printf("Enter branch code: ");
        scanf("%s", account->bcode);
        getchar();
        printf("Enter the Dob of the account holder: ");
        scanf("%s", account->Dob);
        getchar();
        while(blc<1000){
                printf("Enter the initial balance of the account (not less that 1000 RMB): ");
                scanf("%d", &blc);
                if(blc<1000){
                        printf("Initial deposit can't be less than 1000 RMB.\n");
                }
        }
        account->balance=blc;
        time(&t);
        account->Doo=localtime(&t);
        if((fp=fopen("Account.txt", "a"))==NULL){
                printf("Fail to open the file!\n");
                exit(EXIT_FAILURE);
        }
        i=atoi(account->ccode);
        j=atoi(account->bcode);        
        number=(sequence[i][j])+1;
        sequence[i][j]+=1;
        if((ptr=fopen("Sequence.txt", "w"))==NULL){
                printf("Fail to open the file!\n");
                exit(EXIT_FAILURE);
        }
        fwrite(sequence, 150*sizeof(int), 1, ptr);
        fclose(ptr);
        sprintf(orig, "%d", number);
        len=strlen(orig);
        i=0;
        for(j=0;j<5;j++){
                if(j>=(5-len)){
                        seq[j]=orig[i];
                        i++;
                }
                else{
                        seq[j]='0';
                }
        }
        strcpy(account->accountnumber, account->ccode);
        strcat(account->accountnumber, account->bcode);
        strcat(account->accountnumber, seq);
        account->accountnumber[10]='\0';
        printf("The account number is: %s\n", account->accountnumber);
        fwrite(account, sizeof(struct Account), 1, fp);
        free(account);
        fclose(fp);
        printf("\n");
        printf("Do you want to open more accounts?(Y/N): ");
        scanf("%c", &ch);
        getchar();
        if(ch=='Y'){
                printf("\n");                
                openaccount(sequence);
        }
        printf("\n");
}

我需要用c语言写一个每个功能最后都可以询问用户是否需要重复该功能的银行小程序,在我的showcode功能里最后面可以用的逻辑为什么在openaccount里就用不了了

  • 写回答

7条回答 默认 最新

  • sinJack 2020-12-23 09:44
    关注

    如果在openaccount方法中控制不了。就在switch case中去控制。将下面代码

    case '2': openaccount(sequence); break;

    改为:

    case '2':

    char ch;

    do{

    openaccount(sequence);

    printf("Do you want to open more accounts?(Y/N): ");

    scanf("%c", &ch);

    }while (ch=='Y');

    break;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端