Jskey1876
2019-03-26 10:48
采纳率: 91.7%
浏览 276

在本人的已有C中添加或者修改

我想在这个程序中实现:将用户信息数据:如用户名,密码,账户金额引入txt文本,并在下一次实现功能时
,从txt中读取相关用户名,密码,金额。挂失数据暂不考虑,请避免用户名,密码,余额,不对应。txt文件名为data.txt

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int AccountCheck();   //用户名验证
int ReportLossVerification(int);   //挂失验证
int Code(int);     //密码函数
float Total=0;          //// 定义全局变量T,记录用户一天取款数的总和
void Withdrawal ();      //取款函数
void SaveMoney ();     //存款函数
void Inquire();      //查询函数
void SellingHousehodes();   //销户函数
void CreateNewAccount();       //开户函数
void Transfer();  //转账函数
void Loss();      //挂失函数
void Exit();      //退出函数
void CheckAllInfro();      //查询所有用户信息






char UserName[100][30];               //用户名
char CodeStr[100][30];               //密码
char LossName[100][30];                //挂失用户名


float RemainAmount[100];                 //存款余额
int UserNumber=0;                 //用户数
int LossNumber=0;                 //挂失用户数




void main()
{

    int order;          //命令


    while(order)
    {
        printf("******************************************************************\n");
        printf("||                    NM$L BANK ATM SYSTEM                      ||\n");
        printf("||                                                              ||\n");
        printf("||   1.Create new account. 4.Take money out.  8.Report Loss.    ||\n");
        printf("||   2.Save money.          5.Delete a current account.         ||\n");
        printf("||   3.Check current account. 6.Transfer money.   0.Exit.       ||\n");
        printf("||   7.Give all user information.                               ||\n");
        printf("||                                                              ||\n");
        printf(" ================================================================ \n");
        printf("Give your orders please:  ");
        scanf("%d",&order);
        printf("\n");
        switch(order)
        {
        case 1:                                        //开户
            CreateNewAccount ();
            break;
        case 2:                                        //存款
            SaveMoney();
            break;
        case 3:                                        //查询
            Inquire();
            break;
        case 4:                                        //取款
            Withdrawal();
            break;
        case 5:                                        //销户
            SellingHousehodes();
            break;
        case 6:                                        //转账
            Transfer();
            break;
        case 7:                                        //查询所有用户信息
            CheckAllInfro();
            break;
        case 8:                                        //挂失
            Loss();
            break;
        case 0:
             printf("欢迎您再次使用!\n");
             exit(0);
             break;
        }
    }


}




void Withdrawal()       //取款
{
    int i,m;
    float n;
    i=AccountCheck();
    m=ReportLossVerification(i);
    if(m==1)
        m=Code(i);
    while(m==1)
    {
        printf("How much do you want to take out: ");
        scanf("%f",&n);
        if(n>2000)
        {
            printf("Taken out less than 2000, per time!\n");
            Withdrawal();
                break;


        }
        Total=n+Total;
        if(Total>20000)
        {
            printf("Taken out per day should be less than 20000,Choose another day to take out.");
            break;
        }
        if(n<=RemainAmount[i])
        {
            printf("Take your money away.\n");
            RemainAmount[i]=RemainAmount[i]-n;
            break;
        }
        else
            printf("Remain is not enough,input another amount.\n");
    }
}




void SaveMoney()   //存款
{
    int i,m;
    float n;
    i=AccountCheck();
    m=ReportLossVerification(i);
    if(m==1)
        m=Code(i);
    while(m==1)
    {
        printf("How much doyou want to save: ");
        scanf("%f",&n);
        RemainAmount[i]=RemainAmount[i]+n;
        printf("Save successfully !");
        printf("Current remain is: %.2f\n",RemainAmount[i]);
        m=0;
    }
    m=1;
    main();
}




void Inquire()    //查询
{
    int i,m;
    i=AccountCheck();
    m=ReportLossVerification(i);
    if(m==1)
        m=Code(i);
}




void CreateNewAccount()   //开户
{
    int i,n=UserNumber,j=0;
    float m;
    char User[32],mima1[32],mima2[32];
    printf("Input your user name please: ");
    getchar();
    scanf("%s",User);
    for(i=0;i<n+1;i++)
    {
        if(strcmp(User,UserName[i])==0)
        {
            printf("Account already exit.\n");
            break;
        }
        else
        {
            strcpy(UserName[UserNumber],User);
            while(j!=1)
            {
                printf("Input code please: ");
                scanf("%s",mima1);
                printf("Now checking code again: ");
                scanf("%s",mima2);
                if(strcmp(mima1,mima2)==0)
                {
                    printf("Code successfully set.\n");
                    printf("Input your save amount : ");
                    scanf("%f",&m);
                    printf("OK,New account created.\n");
                    strcpy(CodeStr[UserNumber],mima1);
                    RemainAmount[UserNumber]=m;
                    j=1;
                }
                else
                    printf("Two different inputs, try again.\n ");
            }
            UserNumber++;
            break;
        }
    }
}




void SellingHousehodes()    //销户
{
    int i,n,m;
    i=AccountCheck();
    m=ReportLossVerification(i);
    if(m==1)
        m=Code(i);
    while(m==1)
    {
        printf("Make sure to delete ?\n   <1>Sure    <2>Not   \n");
        scanf("%d",&n);
        if(n==1)
        {
            for(;i<UserNumber;i++)
            {
                strcpy(UserName[i],UserName[i+1]);
                strcpy(CodeStr[i],CodeStr[i+1]);
                RemainAmount[i]=RemainAmount[i+1];
            }
        }
    UserNumber--;
    printf("Successfully deleted !\n");
    m=0;
    }
}


void Transfer()      //转账
{
    int i,n,j,s=1;
    float k;
    char User[32];
    i=AccountCheck();
    n=ReportLossVerification(i);
    if(n==1)
        n=Code(i);
    while(n==1)
    {
        printf("Input the target account :");
        scanf("%s",User);
        for(j=0;j<UserNumber;j++)
        {
            if(strcmp(UserName[j],User)==0)
            {
                n=2;
                break;
            }
        }
        if(j==UserNumber)
            printf("Target account not found ,input again:\n");
    }
    while(n==2)
    {
        printf("Input transfer amount:");
        scanf("%f",&k);
        if(k<=RemainAmount[i])
        {
            RemainAmount[i]=RemainAmount[i]-k;
            RemainAmount[j]=RemainAmount[j]+k;
            printf("Transfer successfully !\nCurrent remain amount is : %.2f\n",RemainAmount[i]);
            break;
        }
        else
            printf("Remain not enough,Input again:\n");
    }
}




void Loss()      //挂失
{
    int n,i,m;
    i=AccountCheck();
    m=ReportLossVerification(i);
    if(m==1)
        m=Code(i);
    if(m==1)
    {
        printf("Sure to report lost ?\n   <1>Sure   <2>Not   \n");
        scanf("%d",&n);
        if(n==1)
        {
            strcpy(LossName[LossNumber],UserName[i]);
            LossNumber++;
            printf("%s Reported successfully !\n",UserName[i]);
        }
    }
}




void Exit()
{


}


void CheckAllInfro()     //查询所有用户信息
{
    int i;
    for(i=0;i<UserNumber;i++)
        printf("Account name:%15s     Remain:%.2f\n",UserName[i],RemainAmount[i]);
}


int AccountCheck()     //用户名验证
{
    int m=1,i;
    char User[32];
    while(m==1)
    {
        printf("Input your Account name: ");
        scanf("%s",User);
        for(i=0;i<UserNumber;i++)
            if(strcmp(UserName[i],User)==0)
            {
                m=0;
                break;
            }
        if(i==UserNumber)
            printf("Account not found ,Make sure to try again: \n");
    }
    return i;
}


int ReportLossVerification(int i)             //挂失验证
{
    int j,n;
    for(j=0;j<LossNumber;j++)
    {
        if(strcmp(UserName[i],LossName[j])==0)
        {
            printf("This account already reported lost ,Delete lost report than try again!\n");
            n=0;
            break;
        }
    }
    if(j==LossNumber)
        n=1;
    return n;
}




int Code(int i)     //密码函数
{
    char mima[32];
    while(1)
    {
        printf("Input your passwords: ");
        scanf("%s",mima);
        if(strcmp(CodeStr[i],mima)==0)
        {
            printf("Log in successfully !\n");
            printf("Account :%s          Remain : %.2f \n",UserName[i],RemainAmount[i]);
            break;
        }
        else
            printf("Wrong code ,try again.\n");
    }
    return 1;
}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 白色一大坨 2019-03-26 13:49
    已采纳

    直接用吧,txt文件你建一个放在程序目录,改掉你循环判断条件为true,用order不合适

    
    void openfile()
    {
        FILE *fp;
        int len;
        char *ptr;
        char *p;
        if ((fp = fopen("data.txt", "r")) == NULL)
        {
            printf("cannot open infile\n");
        }
    
        char buf[256] = { 0 };
        int i = 0;
        while (fgets(buf, 1024, fp) != NULL)//读取数据
        {
            len = strlen(buf);
            buf[len - 1] = '\0';  /*去掉换行符*/
            ptr = strtok(buf, "\t");//拆分字符串
            strcpy(UserName[i], ptr);
            ptr = strtok(NULL, "\t");
            strcpy(CodeStr[i], ptr);
            ptr = strtok(NULL, "\t");
            RemainAmount[i] = atof(ptr);
            i++;
        }
    
         UserNumber = i;
        fclose(fp);
    }
    
    void savefile()
    {
        FILE *fp;
        char buf[256] = { 0 };
        if ((fp = fopen("data.txt", "w")) == NULL)
        {
            printf("cannot open infile\n");
        }
    
        for (int i = 0; i < UserNumber;i++)
        {
            sprintf(buf, "%s\t%s\t%.2f\n", UserName[i], CodeStr[i], RemainAmount[i]);
            fputs(buf, fp);
        }
    }
    
    
    void main()
    {
    
        int order = 0;          //命令
    
        openfile();
        while (true)
        {
            printf("******************************************************************\n");
            printf("||                    NM$L BANK ATM SYSTEM                      ||\n");
            printf("||                                                              ||\n");
            printf("||   1.Create new account. 4.Take money out.  8.Report Loss.    ||\n");
            printf("||   2.Save money.          5.Delete a current account.         ||\n");
            printf("||   3.Check current account. 6.Transfer money.   0.Exit.       ||\n");
            printf("||   7.Give all user information.                               ||\n");
            printf("||                                                              ||\n");
            printf(" ================================================================ \n");
            printf("Give your orders please:  ");
            scanf("%d", &order);
            printf("\n");
            switch (order)
            {
            case 1:                                        //开户
                CreateNewAccount();
                break;
            case 2:                                        //存款
                SaveMoney();
                break;
            case 3:                                        //查询
                Inquire();
                break;
            case 4:                                        //取款
                Withdrawal();
                break;
            case 5:                                        //销户
                SellingHousehodes();
                break;
            case 6:                                        //转账
                Transfer();
                break;
            case 7:                                        //查询所有用户信息
                CheckAllInfro();
                break;
            case 8:                                        //挂失
                Loss();
                break;
            case 0:
                printf("欢迎您再次使用!\n");
                savefile();
                exit(0);
                break;
            }
        }
    
    
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • a81836620 2019-03-26 13:58

    在你的代码基础上加了两个函数,分别是程序开始是读取文本 的readFile 以及退出时的writeFile.读写已经格式对齐。考虑到显示问题,字符数组只显示了10个长度,金额 只显示了5个长度。有需要可以自己稍微改下

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int AccountCheck();   //用户名验证
    int ReportLossVerification(int);   //挂失验证
    int Code(int);     //密码函数
    float Total=0;          //// 定义全局变量T,记录用户一天取款数的总和
    void Withdrawal ();      //取款函数
    void SaveMoney ();     //存款函数
    void Inquire();      //查询函数
    void SellingHousehodes();   //销户函数
    void CreateNewAccount();       //开户函数
    void Transfer();  //转账函数
    void Loss();      //挂失函数
    void Exit();      //退出函数
    void CheckAllInfro();      //查询所有用户信息
    void readFile();   // 从文本读入账户
    void writeFile();  // 把账户写入文本
    
    
    char UserName[100][30];               //用户名
    char CodeStr[100][30];               //密码
    char LossName[100][30];                //挂失用户名
    
    
    float RemainAmount[100];                 //存款余额
    int UserNumber=0;                 //用户数
    int LossNumber=0;                 //挂失用户数
    
    
    
    
    int main()
    {
    
        int order;          //命令
        /*
        读入txt数据
        */
       readFile();
    
    
        while(order)
        {
            printf("******************************************************************\n");
            printf("||                    NM$L BANK ATM SYSTEM                      ||\n");
            printf("||                                                              ||\n");
            printf("||   1.Create new account. 4.Take money out.  8.Report Loss.    ||\n");
            printf("||   2.Save money.          5.Delete a current account.         ||\n");
            printf("||   3.Check current account. 6.Transfer money.   0.Exit.       ||\n");
            printf("||   7.Give all user information.                               ||\n");
            printf("||                                                              ||\n");
            printf(" ================================================================ \n");
            printf("Give your orders please:  ");
            scanf("%d",&order);
            printf("\n");
            switch(order)
            {
            case 1:                                        //开户
                CreateNewAccount ();
                break;
            case 2:                                        //存款
                SaveMoney();
                break;
            case 3:                                        //查询
                Inquire();
                break;
            case 4:                                        //取款
                Withdrawal();
                break;
            case 5:                                        //销户
                SellingHousehodes();
                break;
            case 6:                                        //转账
                Transfer();
                break;
            case 7:                                        //查询所有用户信息
                CheckAllInfro();
                break;
            case 8:                                        //挂失
                Loss();
                break;
            case 0:
                writeFile();
                printf("欢迎您再次使用!\n");
                exit(0);
                break;
            }
        }
        return 0;
    }
    
    
    void readFile(){  // 从文本读入账户
        int i;
        FILE *fpRead=fopen("data.txt","r"); 
        for(i=0;!feof(fpRead);i++)  
        {  
            fscanf(fpRead,"%s",UserName[i]);
            fscanf(fpRead,"%s",CodeStr[i]);
            fscanf(fpRead,"%f",&RemainAmount[i]);
            //fscanf(fpRead,"%f",&RemainAmount[i]);
            printf("%10s  %10s  %5f\n",UserName[i],CodeStr[i],RemainAmount[i]);  
        } 
        UserNumber = i;
        fclose(fpRead);
    }   
    void writeFile(){  // 把账户写入文本
        FILE *fpWrite=fopen("data.txt","w"); 
        printf("%d\n",UserNumber);
        for(int i=0;i<UserNumber;i++){
            printf("%10s %10s %5f\n",UserName[i],CodeStr[i],RemainAmount[i]);
            fprintf(fpWrite,"%10s%10s\n%5f",UserName[i],CodeStr[i],RemainAmount[i]);  
        }
        fclose(fpWrite);  
    }
    
    
    
    void Withdrawal()       //取款
    {
        int i,m;
        float n;
        i=AccountCheck();
        m=ReportLossVerification(i);
        if(m==1)
            m=Code(i);
        while(m==1)
        {
            printf("How much do you want to take out: ");
            scanf("%f",&n);
            if(n>2000)
            {
                printf("Taken out less than 2000, per time!\n");
                Withdrawal();
                    break;
    
    
            }
            Total=n+Total;
            if(Total>20000)
            {
                printf("Taken out per day should be less than 20000,Choose another day to take out.");
                break;
            }
            if(n<=RemainAmount[i])
            {
                printf("Take your money away.\n");
                RemainAmount[i]=RemainAmount[i]-n;
                break;
            }
            else
                printf("Remain is not enough,input another amount.\n");
        }
    }
    
    
    
    
    void SaveMoney()   //存款
    {
        int i,m;
        float n;
        i=AccountCheck();
        m=ReportLossVerification(i);
        if(m==1)
            m=Code(i);
        while(m==1)
        {
            printf("How much doyou want to save: ");
            scanf("%f",&n);
            RemainAmount[i]=RemainAmount[i]+n;
            printf("Save successfully !");
            printf("Current remain is: %.2f\n",RemainAmount[i]);
            m=0;
        }
        m=1;
        main();
    }
    
    
    
    
    void Inquire()    //查询
    {
        int i,m;
        i=AccountCheck();
        m=ReportLossVerification(i);
        if(m==1)
            m=Code(i);
    }
    
    
    
    
    void CreateNewAccount()   //开户
    {
        int i,n=UserNumber,j=0;
        float m;
        char User[32],mima1[32],mima2[32];
        printf("Input your user name please: ");
        getchar();
        scanf("%s",User);
        for(i=0;i<n+1;i++)
        {
            if(strcmp(User,UserName[i])==0)
            {
                printf("Account already exit.\n");
                break;
            }
            else
            {
                strcpy(UserName[UserNumber],User);
                while(j!=1)
                {
                    printf("Input code please: ");
                    scanf("%s",mima1);
                    printf("Now checking code again: ");
                    scanf("%s",mima2);
                    if(strcmp(mima1,mima2)==0)
                    {
                        printf("Code successfully set.\n");
                        printf("Input your save amount : ");
                        scanf("%f",&m);
                        printf("OK,New account created.\n");
                        strcpy(CodeStr[UserNumber],mima1);
                        RemainAmount[UserNumber]=m;
                        j=1;
                    }
                    else
                        printf("Two different inputs, try again.\n ");
                }
                UserNumber++;
                break;
            }
        }
    }
    
    
    
    
    void SellingHousehodes()    //销户
    {
        int i,n,m;
        i=AccountCheck();
        m=ReportLossVerification(i);
        if(m==1)
            m=Code(i);
        while(m==1)
        {
            printf("Make sure to delete ?\n   <1>Sure    <2>Not   \n");
            scanf("%d",&n);
            if(n==1)
            {
                for(;i<UserNumber;i++)
                {
                    strcpy(UserName[i],UserName[i+1]);
                    strcpy(CodeStr[i],CodeStr[i+1]);
                    RemainAmount[i]=RemainAmount[i+1];
                }
            }
        UserNumber--;
        printf("Successfully deleted !\n");
        m=0;
        }
    }
    
    
    void Transfer()      //转账
    {
        int i,n,j,s=1;
        float k;
        char User[32];
        i=AccountCheck();
        n=ReportLossVerification(i);
        if(n==1)
            n=Code(i);
        while(n==1)
        {
            printf("Input the target account :");
            scanf("%s",User);
            for(j=0;j<UserNumber;j++)
            {
                if(strcmp(UserName[j],User)==0)
                {
                    n=2;
                    break;
                }
            }
            if(j==UserNumber)
                printf("Target account not found ,input again:\n");
        }
        while(n==2)
        {
            printf("Input transfer amount:");
            scanf("%f",&k);
            if(k<=RemainAmount[i])
            {
                RemainAmount[i]=RemainAmount[i]-k;
                RemainAmount[j]=RemainAmount[j]+k;
                printf("Transfer successfully !\nCurrent remain amount is : %.2f\n",RemainAmount[i]);
                break;
            }
            else
                printf("Remain not enough,Input again:\n");
        }
    }
    
    
    
    
    void Loss()      //挂失
    {
        int n,i,m;
        i=AccountCheck();
        m=ReportLossVerification(i);
        if(m==1)
            m=Code(i);
        if(m==1)
        {
            printf("Sure to report lost ?\n   <1>Sure   <2>Not   \n");
            scanf("%d",&n);
            if(n==1)
            {
                strcpy(LossName[LossNumber],UserName[i]);
                LossNumber++;
                printf("%s Reported successfully !\n",UserName[i]);
            }
        }
    }
    
    
    
    
    void Exit()
    {
    
    
    }
    
    
    void CheckAllInfro()     //查询所有用户信息
    {
        int i;
        for(i=0;i<UserNumber;i++)
            printf("Account name:%15s     Remain:%.2f\n",UserName[i],RemainAmount[i]);
    }
    
    
    int AccountCheck()     //用户名验证
    {
        int m=1,i;
        char User[32];
        while(m==1)
        {
            printf("Input your Account name: ");
            scanf("%s",User);
            for(i=0;i<UserNumber;i++)
                if(strcmp(UserName[i],User)==0)
                {
                    m=0;
                    break;
                }
            if(i==UserNumber)
                printf("Account not found ,Make sure to try again: \n");
        }
        return i;
    }
    
    
    int ReportLossVerification(int i)             //挂失验证
    {
        int j,n;
        for(j=0;j<LossNumber;j++)
        {
            if(strcmp(UserName[i],LossName[j])==0)
            {
                printf("This account already reported lost ,Delete lost report than try again!\n");
                n=0;
                break;
            }
        }
        if(j==LossNumber)
            n=1;
        return n;
    }
    
    
    
    
    int Code(int i)     //密码函数
    {
        char mima[32];
        while(1)
        {
            printf("Input your passwords: ");
            scanf("%s",mima);
            if(strcmp(CodeStr[i],mima)==0)
            {
                printf("Log in successfully !\n");
                printf("Account :%s          Remain : %.2f \n",UserName[i],RemainAmount[i]);
                break;
            }
            else
                printf("Wrong code ,try again.\n");
        }
        return 1;
    }
    
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题