头上长草了哥 2019-03-26 10:48 采纳率: 100%
浏览 279
已采纳

在本人的已有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;
            }
        }
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决