zoukankan      html  css  js  c++  java
  • (C)学生成绩管理系统

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <unistd.h>
      5 
      6 struct Student
      7 {
      8     char id[4];
      9     char name[10];
     10     int score;
     11 };
     12 
     13 typedef struct User
     14 {
     15     char userName[20];
     16     char userPassword[10];
     17 }UserInfo;
     18 
     19 void inputNameAndPassword();
     20 void logon();
     21 void creatDataBace();
     22 void selectServe();
     23 void checkAchievement();
     24 void addAchievement();
     25 void delectAchievement();
     26 void changeAchievement();
     27 void sortAchievement();
     28 void start();
     29 
     30 int main(int argc, const char * argv[])
     31 {
     32     start();
     33     //清屏未实现
     34     system("clear");
     35         
     36     FILE *file;
     37     file=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt", "r+");
     38     struct Student aStudent;
     39     if (fread(&aStudent, sizeof(struct Student), 1, file)==0) {
     40         creatDataBace();
     41     }
     42     fclose(file);
     43     //清屏未实现
     44     
     45     selectServe();
     46     
     47     return 0;
     48 }
     49 
     50 void start()
     51 {
     52     int land;
     53     printf("1.注册
    2.登陆
    ");
     54     scanf("%d",&land);
     55     if (land==1) {
     56         logon();
     57     }else
     58     {
     59         if (land==2)
     60             inputNameAndPassword();
     61         else
     62         {
     63             printf("输入有误,请重新选择
    ");
     64             start();
     65         }
     66      }
     67 }
     68 
     69 void logon()
     70 {
     71     UserInfo userInput;
     72     printf("
    ------------------注册用户------------------");
     73     printf("
    请输入注册用户名:");
     74     scanf("%s",userInput.userName);
     75     printf("请输入密码:");
     76     scanf("%s",userInput.userPassword);
     77     FILE *fp;
     78     if((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/user_password.txt", "a+"))==NULL)
     79     {
     80         printf("注册失败!");
     81         exit(0);
     82     }
     83     if ((fwrite(&userInput, sizeof(UserInfo), 1, fp))!=1) {
     84         printf("注册失败!");
     85         exit(0);
     86     }
     87     fclose(fp);
     88     inputNameAndPassword();
     89 }
     90 
     91 void inputNameAndPassword()
     92 {
     93     UserInfo userInput,userBase[50];
     94     int i=0,count=0,chance=0;
     95     
     96     FILE *fp;
     97     if((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/user_password.txt", "r"))==NULL)
     98     {
     99         printf("登陆失败!");
    100         exit(0);
    101     }
    102     
    103     while(fread(&userBase[i], sizeof(UserInfo), 1, fp)!=0)
    104         i++;
    105     count=i;
    106     fclose(fp);
    107     
    108     printf("
    ------------------用户登陆------------------
    ");
    109     while (1)
    110     {
    111         printf("用户名:");
    112         scanf("%s",userInput.userName);
    113         printf("密码:");
    114         scanf("%s",userInput.userPassword); //密码用星号代替未实现
    115         
    116         for (i=0; i<count; i++) {
    117             if((strcmp(userInput.userName, userBase[i].userName)==0)&&(strcmp(userInput.userPassword, userBase[i].userPassword)==0))
    118                 return;
    119         }
    120         if (chance!=3)
    121             printf("
    用户名或密码输入错误,请重新输入(还有 %d 次机会):
    ",3-chance);
    122         chance++;
    123         if (chance>=4) {
    124             printf("
    尝试超出次数!退出程序。");
    125             exit(0);
    126         }
    127     }
    128 }
    129 
    130 void selectServe()
    131 {
    132     int serveNumber;
    133     printf("
    ------------------操作菜单------------------");
    134     printf("
    1.查询学生成绩
    2.添加学生成绩
    3.删除学生成绩
    4.修改学生成绩
    5.查看成绩排名
    ");
    135     printf("
    请选择您需要的操作(输入序号选择,输入其它退出程序):");
    136     scanf("%d",&serveNumber);
    137     switch (serveNumber) {
    138         case 1:
    139             checkAchievement();
    140             break;
    141         case 2:
    142             addAchievement();
    143             break;
    144         case 3:
    145             delectAchievement();
    146             break;
    147         case 4:
    148             changeAchievement();
    149             break;
    150         case 5:
    151             sortAchievement();
    152             break;
    153         default:
    154             break;
    155     }
    156     printf("
     ^^ 再见.....");
    157 }
    158 
    159 void creatDataBace()
    160 {
    161     FILE *fp;
    162     struct Student student[100];
    163     int flag=0;
    164     int i=0;
    165     int k=0;
    166     int count=0;
    167     int m=0;
    168     char ch;
    169     
    170     if((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt", "r"))==NULL)
    171     {
    172         printf("打开文件错误!");
    173         exit(0);
    174     }
    175     
    176     for (i=0;fread(&student[i], sizeof(struct Student), 1, fp)!=0;i++);
    177     count=i;
    178     fclose(fp);
    179     
    180     printf("
    ------------------初始化数据库------------------");
    181     i=0;
    182     while (1) {
    183         printf("
    请输入学生的基本信息:
    ");
    184         do
    185         {
    186             printf("第 %d 个学生的ID号:",i+1);
    187             scanf("%s",student[i].id);
    188             
    189             for (k=i-1; k>=0; k--) {
    190                 if(strcmp(student[i].id, student[k].id)==0)
    191                 {
    192                     printf("
    ID号已存在,请重新输入
    ");
    193                     flag=1;
    194                     break;
    195                 }
    196             }
    197             
    198             for (k=0; k<count; k++) {
    199                 if(strcmp(student[i].id, student[k].id)==0)
    200                 {
    201                     printf("
    ID号在整个数据库中已存在,请重新输入
    ");
    202                     flag=1;
    203                     break;
    204                 }
    205             }
    206         }while(flag);
    207         
    208         do
    209         {
    210             flag=0;
    211             printf("学生名称:");
    212             scanf("%s",student[i].name);
    213         }while (flag);
    214         
    215         do
    216         {
    217             flag=0;
    218             printf("学生成绩:");
    219             scanf("%d",&student[i].score);
    220             if (student[i].score>100||student[i].score<0) {
    221                 flag=1;
    222                 continue;
    223             }
    224         }while (flag);
    225         
    226         i++;
    227         m++;
    228         printf("
    是否继续创建学生成绩信息?输入'y'或'Y'继续,输入其它进入下一步:");
    229         getchar();
    230         scanf("%c",&ch);
    231         if (ch!='Y'&&ch!='y')
    232             break;
    233     }
    234     
    235     
    236     if((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt", "a"))==NULL)
    237     {
    238         printf("打开文件错误!");
    239         exit(0);
    240     }
    241     
    242     for (i=0; i<m; i++) {
    243         if (fwrite(&student[i], sizeof(struct Student), 1, fp)!=1)
    244             printf("数据保存失败");
    245     }
    246     
    247     fclose(fp);
    248 }
    249 
    250 void checkAchievement()
    251 {
    252     struct Student student;
    253     char id[4];
    254     FILE *fp;
    255     
    256     printf("
    ------------------查询学生成绩------------------
    ");
    257     printf("请输入需要查询的学生ID:");
    258     scanf("%s",id);
    259     while (1)
    260     {
    261         if ((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt","r"))==NULL)
    262         {
    263             printf("打开文件错误!");
    264             selectServe();
    265         }
    266         
    267         while (fread(&student, sizeof(struct Student), 1, fp)!=0) {
    268             if (strcmp(id,student.id)==0) {
    269                 printf("ID:%s  姓名:%s  分数:%d
    ",student.id,student.name,student.score);
    270                 break;
    271             }
    272         }
    273 
    274         if (strcmp(id,student.id)!=0)
    275             printf("没查找到ID为 %s 的学生信息。
    ",id);
    276         
    277         printf("
    请输入需要查询的学生ID(输入n返回操作菜单):");
    278         scanf("%s",id);
    279         if (strcmp(id,"n")==0||strcmp(id,"N")==0) {
    280             fclose(fp);
    281             selectServe();
    282         }
    283         fclose(fp);
    284     }
    285 }
    286 
    287 void addAchievement()
    288 {
    289     FILE *fp;
    290     struct Student student[100];
    291     int i=0;
    292     int count=0;
    293     char ch;
    294     
    295     if((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt", "r"))==NULL)
    296     {
    297         printf("打开文件错误!");
    298         selectServe();
    299     }
    300     
    301     for (i=0;fread(&student[i], sizeof(struct Student), 1, fp)!=0;i++);
    302     count=i;
    303     fclose(fp);
    304     
    305     if((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt", "a+"))==NULL)
    306     {
    307         printf("打开文件错误!");
    308         selectServe();
    309     }
    310     
    311     printf("
    ------------------添加学生成绩------------------");
    312     while (1)
    313     {
    314         int flag;
    315         do {
    316             flag=0;
    317             printf("
    请输入需要添加的学生ID:");
    318             scanf("%s",student[count].id);
    319             for (int k=0; k<count; k++) {
    320                 if(strcmp(student[count].id, student[k].id)==0)
    321                 {
    322                     printf("ID号在数据库中已存在,请重新输入。
    ");
    323                     flag=1;
    324                     break;
    325                 }
    326             }
    327         } while (flag);
    328         printf("请输入需要添加的学生姓名:");
    329         scanf("%s",student[count].name);
    330         printf("请输入需要添加的学生分数:");
    331         scanf("%d",&student[count].score);
    332         if (fwrite(&student[count], sizeof(struct Student), 1, fp)!=1) {
    333             printf("添加失败!");
    334         }
    335         printf("添加成功!
    ");
    336         printf("
    是否继续添加学生成绩信息?输入y继续,输入其它返回操作菜单:");
    337         getchar();
    338         scanf("%c",&ch);
    339         if (ch!='y'&&ch!='Y') {
    340             fclose(fp);
    341             selectServe();
    342         }
    343         count++;
    344     }
    345 }
    346 
    347 void delectAchievement()
    348 {
    349     char id[4];
    350     char sure;
    351     FILE *infile,*outfile;
    352     struct Student students[100];
    353     int i=0,flag=-1,count=0;
    354     
    355     
    356     printf("
    ------------------删除学生成绩------------------");
    357     
    358     if ((infile=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt","r"))==NULL)
    359     {
    360         printf("打开文件错误!");
    361         selectServe();
    362     }
    363     
    364     while (fread(&students[i], sizeof(struct Student), 1, infile)!=0) i++;
    365     count=i;
    366     fclose(infile);
    367     
    368     printf("
    请输入需要删除的学生ID:");
    369     scanf("%s",id);
    370     while (1)
    371     {
    372         for (i=0; i<count; i++) {
    373             if (strcmp(id,students[i].id)==0) {
    374                 flag=i;
    375                 break;
    376             }
    377         }
    378         
    379         if (flag==-1)
    380             printf("没查找到ID为 %s 的学生信息。",id);
    381         else
    382         {
    383             printf("确认要删除ID为 %s 的学生信息吗?(y确定 n取消)",id);
    384             getchar();
    385             scanf("%c",&sure);
    386             if (sure=='y')
    387             {
    388                 for (i=flag; i<count-1; i++) {
    389                     students[i]=students[i+1];
    390                 }
    391                 count--;
    392             
    393                 outfile=fopen("/Users/5000/Desktop/C:OC作业/Final Project/temp.txt","w+");
    394                 for (i=0; i<count; i++) {
    395                     if(fwrite(&students[i], sizeof(struct Student), 1, outfile)!=1)
    396                     {
    397                         printf("删除失败!");
    398                         selectServe();
    399                     }
    400                 }
    401                 fclose(outfile);
    402             }
    403             printf("删除成功!
    ");
    404         }
    405         
    406         printf("
    请输入需要删除的学生ID(输入n返回操作菜单):");
    407         scanf("%s",id);
    408         
    409         if (strcmp(id,"n")==0||strcmp(id,"N")==0) {
    410             /*其实用w打开原来文件就可以直接写入原来的文件,w打开会清空原来文件的内容*/
    411             unlink("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt");//#include <unistd.h>
    412             rename( "/Users/5000/Desktop/C:OC作业/Final Project/temp.txt","/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt");//#include <unistd.h>
    413             selectServe();
    414         }
    415     } 
    416 }
    417 
    418 void changeAchievement()
    419 {
    420     FILE *fp;
    421     char id[4],ch;
    422     int score=0,count=0,flag=1;
    423     int i=0;
    424     struct Student students[100];
    425     
    426     if ((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt","r"))==NULL)
    427     {
    428         printf("打开文件错误!");
    429         selectServe();
    430     }
    431     while (fread(&students[i], sizeof(struct Student), 1, fp)!=0) i++;
    432     count=i;
    433     fclose(fp);
    434     
    435     printf("
    ------------------学生成绩排名------------------
    ");
    436     while (1)
    437     {
    438         printf("请输入需要修改成绩的学生ID:");
    439         scanf("%s",id);
    440 
    441 
    442         for (i=0;i<count;i++) {
    443             if (strcmp(id,students[i].id)==0) {
    444                 printf("ID为 %s 的%s同学,分数为:%d
    ",students[i].id,students[i].name,students[i].score);
    445                 printf("更改%s同学的分数值为:",students[i].name);
    446                 scanf("%d",&score);
    447                 students[i].score=score;
    448                 flag=0;
    449                 break;
    450             }
    451         }
    452         
    453         if (flag)
    454             printf("没查找到ID为 %s 的学生信息。
    ",id);
    455         
    456         if ((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt","w+"))==NULL)
    457         {
    458             printf("打开文件错误!");
    459             selectServe();
    460         }
    461         for (i=0; i<count; i++) {
    462             if(fwrite(&students[i], sizeof(struct Student), 1, fp)!=1)
    463             {
    464                 printf("修改失败!");
    465                 selectServe();
    466             }
    467         }
    468         
    469         printf("
    是否继续查询学生成绩信息?输入'y'或'Y'继续,输入其它返回操作菜单:");
    470         getchar();
    471         scanf("%c",&ch);
    472         if (ch!='y'&&ch!='Y') {
    473             fclose(fp);
    474             selectServe();
    475         }
    476     }
    477 }
    478 
    479 void sortAchievement()
    480 {
    481     char ch;
    482     FILE *fp;
    483     struct Student students[100],temp;
    484     int i=0,j=0,count=0;
    485     
    486     if ((fp=fopen("/Users/5000/Desktop/C:OC作业/Final Project/achievement.txt","r"))==NULL)
    487     {
    488         printf("打开文件错误!");
    489         selectServe();
    490     }
    491     
    492     while (fread(&students[i], sizeof(struct Student), 1, fp)!=0) i++;
    493     fclose(fp);
    494     count=i;
    495     
    496     printf("
    ------------------学生成绩排名------------------
    ");
    497     for(i=0;i<count-1;i++)
    498         for(j=0;j<count-1-i;j++)
    499         {
    500             if (students[j].score<students[j+1].score) {
    501                 temp=students[j];
    502                 students[j]=students[j+1];
    503                 students[j+1]=temp;
    504             }
    505         }
    506     printf("ID    Name      Score
    "); //Id****Name******Score
    507     for (i=0; i<count; i++) {
    508         printf("%s",students[i].id);
    509         for (j=0; j<6-strlen(students[i].id); j++) 
    510             printf(" ");
    511         
    512         printf("%s",students[i].name);
    513         for (j=0; j<10-strlen(students[i].name); j++) 
    514             printf(" ");
    515             
    516         printf("%d
    ",students[i].score);
    517     }
    518     
    519     printf("
    输入任意键返回操作菜单:");
    520     getchar(); //接收选择操作4时输入的回车
    521     scanf("%c",&ch);
    522     selectServe();
    523     
    524 }


    运行截图:





  • 相关阅读:
    TensorFlow-Slim 简介+Demo
    AI 常见术语总结
    经典深度学习CNN总结
    YOLO V3 原理
    tf.app.run() 运行结束时,报错:SystemExit exception: no description
    VSCode中相对路径设置问题
    SSD算法原理
    机器学习-交叉熵原理
    tensorflow scope的作用
    水池进水和出水两个线程问题
  • 原文地址:https://www.cnblogs.com/mingfung-liu/p/3156780.html
Copyright © 2011-2022 走看看