Yojiroooo0705 2022-08-26 16:47 采纳率: 100%
浏览 110
已结题

有偿,问题不难 求超人解答

有偿!不难 通过文件读写实现用户注册,要识别用户id是否存在
现在只能识别第一行(第一个)用户


```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct UserData
{
    char userID[20];
    char userName[20];
    char userSex;
    char userNumber[20];
    char userPass[20];
    char userEmail[30];
    char userAddress[50];
    float usermoney;
}userdata;

void Regist()
{
    userdata a={0},b={0};
    char temp[20];
    char tempid[20];
    int q=0;
    int t=0;
    char tempmoney[50];
    char* tmoney;
    int n=0;
    FILE *pf;
    userdata local[20];
    pf=fopen("userdata.txt","r");
    if(pf==NULL)
    {
        printf("打开注册文件失败\n");
        return;
    }

    while(!feof(pf))
    {
        fscanf(pf,"%s %s %s %s %s %s %s %f",local[q].userID,local[q].userName,local[q].userSex,local[q].userNumber,local[q].userPass,local[q].userEmail,local[q].userAddress,&local[q].usermoney);
        q++;
    }

    printf("欢迎注册超市用户账号!( ´∀`)\n");
    printf("请输入你的用户ID:\n");
    scanf("%s",tempid);
    for ( t = 0 ; t < q ; t++)
    {
        if(strcmp(tempid,local[t].userID)==0)
        {
            printf("该ID已被用户注册,请输入新的用户ID");
            scanf("%s",tempid);
        }
        else
        {
            strcpy(a.userID,tempid);
            printf("您的ID为:%s",a.userID);
            break;
        }

    }
    



    printf("请输入姓名:\n");
    scanf("%s",a.userName);
    printf("请输入性别(f(女)/m(男)):\n");
    scanf("%c",&a.userSex);
    do
    {
        if(a.userSex != 'f' && a.userSex != 'm')
        {
            printf("性别输入错误!请重新输入!\n");
            scanf("%c",&a.userSex);
        }
    }while(a.userSex != 'f' && a.userSex != 'm');
    getchar();
    printf("请输入电话号码:\n");
    scanf("%s",a.userNumber);
    printf("请输入密码\n");
    scanf("%s",a.userPass);
    printf("请确认密码\n");
    scanf("%s",temp);
    do
    {
        if(strcmp(a.userPass,temp)==0)
        {

           printf("账号注册成功,请输入邮箱\n"); 
           fclose(pf);
           break;
        }
        else
        {
            printf("两次密码不匹配!请重新输入!\n");
            scanf("%s",a.userPass);
            printf("请确认密码\n");
            scanf("%s",temp);
        }
    }while(1);
    getchar();
    scanf("%s",a.userEmail);

    
    printf("请输入地址\n");
    scanf("%s",a.userAddress);
    printf("请输入您的余额:");
    scanf("%f",&a.usermoney);

    pf = fopen("userdata.txt","a");
    fwrite(&a, sizeof(struct UserData), 1, pf);
    fclose(pf);
    return;
}

 
  
int main()
{
    int input=-1; 
    do
    {
        printf("\t\t\t----------------------------------\n");
        printf("\t\t\t|           1.登录               |\n");
        printf("\t\t\t|           2.注册               |\n");
        printf("\t\t\t|           0.退出               |\n");
        printf("\t\t\t----------------------------------\n");
        printf("请选择您想进行的操作\n");
        scanf("%d",&input);
        switch(input)
        {
            case 2:Regist();break;
            case 0:puts("退出成功"); return 0;
        }
    }while(1);
 
 return 0;
}

```

  • 写回答

5条回答 默认 最新

  • qzjhjxj 2022-08-26 17:32
    关注

    这是我以前写的,和你要求的功能相同的代码,有对识别用户id是否存在,同时密码输入里有对输入密码(六位[数字+字符])位数的检查及数字与字符位数的要求,供参考:

    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    typedef struct The_users
    {
        char id[100];
        char password[20];
        char phone[12];
        char mailbox[100];
        struct The_users* next;
    }users;
    void add_users(char* s = NULL)
    {
        FILE* fp;
        errno_t err;
        int count = 0, i = 0, flg = 0, flag = 0;
        char  name_id[100], password[20], phone[12], mailbox[100];
     
        if ((err = fopen_s(&fp, "users.txt", "a")) != 0)
            printf("文件打开失败!\n");
        else {
            if (s == NULL){
                printf("新建用户名:");
                scanf_s("%s", name_id, (int)sizeof(name_id));
            }
            else
                strcpy(name_id, s);
            do {
                printf("请输入密码(六位[数字+字符]):");
                scanf_s("%s", password, (int)sizeof(password));
                count = strlen(password);
                for (i = 0, flg = 0, flag = 0; i < count; i++)
                    if (isdigit(password[i]))
                        flg++;
                    else
                        flag++;
            } while (count != 6 || flg == 0 || flag == 0);
            printf("请输入电话:");
            scanf_s("%s", phone, (int)sizeof(phone));
            printf("请输入邮箱:");
            scanf_s("%s", mailbox, (int)sizeof(mailbox));
            fprintf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox);
            fclose(fp);
            printf("恭喜,您的账号注册成功!\n");
        }
    }
    void registers()
    {
        char  name_id[100], password[20], phone[12], mailbox[100];
        users* head, * tail, * p;
        FILE* fp;
        errno_t err;
        printf("欢迎来到注册界面!\n");
        if ((err = fopen_s(&fp, "users.txt", "r")) != 0){
            add_users();
        }
        else {
            head = NULL, tail = NULL, p = NULL;
            while (1)
            {
                if (fscanf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox) != 4) break;
                p = (users*)malloc(sizeof(struct The_users));
                p->next = NULL;
                strcpy(p->id, name_id); strcpy(p->password, password);
                strcpy(p->phone, phone); strcpy(p->mailbox, mailbox);
                if (head == NULL)
                    head = p;
                else
                    tail->next = p;
                tail = p;
            }
            fclose(fp);
            do {
                printf("新建用户名:");
                scanf("%s", name_id);
                tail = head;
                while (tail != NULL && strcmp(tail->id, name_id) != 0)
                {
                    tail = tail->next;
                }
                if (tail == NULL)
                    add_users(name_id);
                else
                    printf("该用户名已经被注册,请重新输入!\n");
            } while (tail != NULL);
        }
        return;
    }
    int login()
    {
        char  name_id[100], password[20], phone[12], mailbox[100];
        users* head, * tail, * p;
        errno_t err;
        FILE* fp;
        int i = 0, flg = 0;
        if ((err = fopen_s(&fp, "users.txt", "r")) != 0){
            printf("无注册用户,请先注册用户。\n");
        }
        else {
            printf("欢迎来到登录界面\n");
            head = NULL, tail = NULL, p = NULL;
            while (1)
            {
                if (fscanf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox) != 4) break;
                p = (users*)malloc(sizeof(struct The_users));
                p->next = NULL;
                strcpy(p->id, name_id);  strcpy(p->password, password);
                strcpy(p->phone, phone); strcpy(p->mailbox, mailbox);
                if (head == NULL)
                    head = p;
                else
                    tail->next = p;
                tail = p;
            }
            fclose(fp);
            printf("请输入用户名:");
            scanf_s("%s", name_id, (int)sizeof(name_id));
            tail = head;
            while (tail != NULL && strcmp(tail->id, name_id) != 0)
            {
                tail = tail->next;
            }
            if (tail != NULL)
            {
                do {
                    printf("密 码:");
                    scanf_s("%s", password, (int)sizeof(password));
                    if (strcmp(tail->password, password) == 0)
                    {
                        printf("登录成功!\n");
                        return 1;
                    }
                    else {
                        i++;
                        if (i != 3)
                            printf("输入密码错误,还有%d次机会,请重新输入!\n", 3 - i);
                        else {
                            printf("输入密码错误,3次机会已用完,退出登录!\n");
                            break;
                        }
                    }
                } while (strcmp(tail->password, password) != 0);
            }
            else
                printf("该用户名不存在!\n");
        }
        return 0;
    }
    int main()
    {
        int n;
        while (1)
        {
            printf("***********菜单*************\n");
            printf("**   1:登录              **\n");
            printf("**   2:注册              **\n");
            printf("**   3:退出              **\n");
            printf("***************************\n");
            printf("请输入1-3编号:");
            scanf_s("%d", &n);
            if (n == 2)
                registers();
            else if (n == 3)
                break;
            else if (n == 1)
                login();
        }
        return 0;
    }
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 9月4日
  • 已采纳回答 8月27日
  • 创建了问题 8月26日

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?