giannis34wudi 2022-12-26 23:33 采纳率: 100%
浏览 187
已结题

学生档案管理系统(c语言)(最好是原创)

该系统实现对学生档案的存储和管理。学生信息包括学号、姓名、性别和生日等信息。系统
可以对学生信息进行查询、添加、删除和备份。系统进入时还包括一个密码验证的过程,也
可以对密码进行管理。

img

  • 写回答

8条回答 默认 最新

  • 浪客 2022-12-27 13:52
    关注

    数组方式实现

    
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define STRLEN 20
    #define ARRSIZE 30
    
    typedef struct Student
    {
       char id[STRLEN];
       char name[STRLEN];
       char sex[4];
       char brithday[STRLEN];
    } Student;
    
    const char DataFile[] = "data.txt";
    const char PwdFile[] = "pwd.txt";
    
    int ReadFile(Student *stu, int n)
    {
       int cnt = 0;
       FILE *fp;
       fp = fopen(DataFile, "r");
       if (fp == NULL)
          return 0;
    
       while (!feof(fp) && cnt < n)
       {
          fread(stu + cnt, sizeof(Student), 1, fp);
          if (strlen(stu->id) == 0)
             break;
          cnt++;
       }
       fclose(fp);
       return cnt;
    }
    
    void SaveFile(Student *stu, int count)
    {
       FILE *fp;
       fp = fopen(DataFile, "w");
       if (fp == NULL)
          return;
    
       for (int i = 0; i < count; i++)
       {
          fwrite(stu + i, sizeof(Student), 1, fp);
       }
       fclose(fp);
    }
    
    // 输入口令
    void inputPWD(char *s, int n)
    {
       char ch;
       int i = 0;
       while (i < n - 1)
       {
          ch = getch();
          if (ch == '\r')
          {
             putchar('\n');
             break;
          }
          s[i++] = ch;
          putchar('*');
       }
       s[i] = '\0';
    }
    
    // 设置口令
    void setPWD()
    {
       char pwd1[STRLEN], pwd2[STRLEN];
       while (1)
       {
          printf("输入新密码:");
          inputPWD(pwd1, STRLEN);
          printf("再次输入新密码:");
          inputPWD(pwd2, STRLEN);
          if (strcmp(pwd1, pwd2) != 0)
             printf("两次输入密码不一致!\n");
          else
             break;
       }
       FILE *fp;
       fp = fopen(PwdFile, "w");
       fwrite(pwd1, STRLEN, 1, fp);
       fclose(fp);
       printf("密码修改成功!\n");
    }
    
    // 校验口令
    int CheckPWD(char *s) // 校验密码
    {
       char pwd[STRLEN] = {0};
       FILE *fp;
    
       fp = fopen(PwdFile, "r");
       if (fp == NULL) //
          return 1;
       fread(pwd, STRLEN, 1, fp);
       fclose(fp);
       return strcmp(s, pwd) == 0;
    }
    
    // 登录模块
    void Login()
    {
    
       char pwd[STRLEN];
       int r, i = 0;
       while (i < 3)
       {
          printf("\n\t   学生档案管理系统\n");
          printf("- - - - - - - - - - - - - - - - - - - \n");
          printf("\n请输入口令:");
          inputPWD(pwd, STRLEN);
          // puts(pwd);
          r = CheckPWD(pwd);
          if (!r)
             printf("口令错误,重新输入!\n");
          else
             break;
          i++;
       }
       if (i == 3)
       {
          printf("错误次数太多!\n");
          exit(1);
       }
    }
    
    void inputStu(Student *stu, int size, int *inx)
    {
       if (size == *inx)
       {
          printf("数组已满\n");
          return;
       }
       printf("输入学号:");
       scanf("%s", stu[*inx].id);
       printf("输入姓名:");
       scanf("%s", stu[*inx].name);
       printf("输入性别:");
       scanf("%s", stu[*inx].sex);
       printf("输入出生日期:");
       scanf("%s", stu[*inx].brithday);
       (*inx)++;
    }
    
    void Show(Student *stu, int count)
    {
       printf("%8s%8s%8s%10s\n", "学号", "姓名", "性别", "出生日期");
       for (int i = 0; i < count; i++)
       {
          printf("%8s%8s%8s%10s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].brithday);
       }
    }
    
    int FindbyName(Student *stu, int count, char *name)
    {
       for (int i = 0; i < count; i++)
       {
          if (strcmp(stu[i].name, name) == 0)
             return i;
       }
       return -1;
    }
    
    void Find(Student *stu, int count)
    {
       int i;
       char name[STRLEN];
       printf("输入要查询的姓名:");
       scanf("%s", name);
    
       i = FindbyName(stu, count, name);
       if (i == -1)
          printf("不存在!\n");
       else
          printf("%8s%8s%4s%8s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].brithday);
    }
    
    void Edit(Student *stu, int count)
    {
       int i;
       char name[STRLEN];
       printf("输入要修改的姓名:");
       scanf("%s", name);
       i = FindbyName(stu, count, name);
       if (i == -1)
       {
          printf("不存在!\n");
          return;
       }
       printf("%8s%8s%4s%8s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].brithday);
       inputStu(stu, ARRSIZE, &i);
       SaveFile(stu, count);
       printf("修改成功!\n");
    }
    
    void Del(Student *stu, int *count)
    {
       int i;
       char name[STRLEN];
       printf("输入要删除的姓名:");
       scanf("%s", name);
       i = FindbyName(stu, *count, name);
       if (i == -1)
       {
          printf("不存在!\n");
          return;
       }
       printf("%8s%8s%4s%8s\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].brithday);
       for (int j = i; j < *count - 1; j++)
       {
          stu[j] = stu[j + 1];
       }
       (*count)--;
       SaveFile(stu, *count);
       printf("删除成功!\n");
    }
    
    void Backup()
    {
       char bakname[100] = "backup.txt";
       FILE *fp1, *fp2;
       fp1 = fopen(DataFile, "r");
       fp2 = fopen(bakname, "w");
       char ch;
       while (!feof(fp1))
       {
          ch = fgetc(fp1);
          fputc(ch, fp2);
       }
       fclose(fp1);
       fclose(fp2);
       printf("备份完毕!\n");
    }
    
    int Menu()
    {
       int n;
       printf("\n\n\t   学生档案管理系统\n");
       printf("- - - - - - - - - - - - - - - - - - - \n");
       printf(" 1 输入数据         |    5 删除数据\n");
       printf(" 2 显示数据         |    6 备份数据\n");
       printf(" 3 查询数据         |    7 设置口令\n");
       printf(" 4 修改数据         |    0 退出\n");
       printf("- - - - - - - - - - - - - - - - - - - \n");
       printf("\n请选择:");
       fflush(stdin);
       scanf("%d", &n);
       return n;
    }
    
    int main()
    {
       Student stu[ARRSIZE] = {0};
       int sel, run = 1;
       Login();
       int count = ReadFile(stu, ARRSIZE);
       while (run)
       {
          sel = Menu();
          switch (sel)
          {
          case 1:
             inputStu(stu, ARRSIZE, &count);
             SaveFile(stu, count);
             break;
          case 2:
             Show(stu, count);
             break;
          case 3:
             Find(stu, count);
             break;
          case 4:
             Edit(stu, count);
             break;
          case 5:
             Del(stu, &count);
             break;
          case 6:
             Backup();
             break;
          case 7:
             setPWD();
             break;
          case 0:
             run = 0;
             break;
    
          default:
             printf("输入错误,重新输入!\n");
             break;
          }
       }
    
       return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 赞助了问题酬金15元 12月27日
  • 修改了问题 12月27日
  • 展开全部

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值