• 学生成绩管理系统【c】


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    #define MAX 2000
    struct Student
    {
    int no;       /*学号*/
    char name[8]; /*姓名*/
    char sex;      /*性别*/
    char phone[8];/*联系电话*/
    int mark[4];   /*语文、数学、外语、考试平均成绩*/    
    };
    int total;         /*总人数*/
    char password[20];/*密码*/
    struct Student student[MAX];
    /**************************************************************************/
    void Init(); /*初始化,文件不存在,则建立,同时记录下文件中的记录数   */
    int get_menu_choice() ;/*接受菜单选择 */
    void menu() ;/*菜单响应 */
    void Show_menu(); /*显示菜单 */
    FILE *file_operate(char *mode); /*文件操作 */
    void Delete(FILE *fp); /*删除记录 */
    void Alter(FILE *fp); /*修改记录信息  */
    void Show(); /*显示打印所有的信息 */
    void Save(); /*备份信息到文件  */
    void Find(); /*查找记录的函数  */
    void Input(FILE *fp); /*向管理系统中增加记录  */
    int Inputoneperson(int i); /*向数组增加一条记录  */
    void set_psw();  /*设置密码  */
    int psw_check(); /*密码验证  */
    /*下面函数为了简化部分工作从上面函数中划分出来的功能  */
    /**************************************************************************/
    void exchange(int i,int j);/*交换两个人的所有信息  */
    void PrintTitle(); /*打印头信息  */
    void clear(); /*清屏 */
    void Showoneperson(int i); /*显示打印一个人的信息 */
    int CheckNumber(int no_temp); /*检查学号是否存在,存在返回序号,不存在返回-1 */
    void Inputfile(int i,FILE *fp); /*把下标为i 的记录写入文件 */
    void Readfile(int i,FILE *fp); /*读一条记录从文件 */
    int Sort_menu_choice();/*选择是按学号还是成绩排序*/
    void Sort_menu();/*排序菜单*/
    void Sort_save();/*排序后数据写入文件*/
    void Sort_no();/*选择是按学号排序*/
    void Sort_mark();/*选择是按成绩排序*/
    int get_menu_choice()/*接受菜单选择 */
    {
    int menu_ch;             /*菜单选项 */
    do
    {
    printf("Select the choice:");
    scanf("%d",&menu_ch);
    if((menu_ch<0)||(menu_ch>9))
    printf("error!");
    }
    while((menu_ch<0)||(menu_ch>9));
    return(menu_ch);
    }
    void Show_menu()/*显示菜单 */
    {


    printf("                欢迎使用学生成绩管理系统                     ");
    printf("************************************************************* ");
        printf("         1.显示数据         |         2.删除数据             ");
    printf("         3 查询数据         |         4.输入数据             ");
    printf("         5.修改数据         |         6.备份数据             ");
    printf("         7.设置口令         |         8.数据排序             ");
    printf("         0.退出                                               ");
        printf("************************************************************* ");
    }
    void clear()/*清屏 */
    {
    system("pause");
    system("cls");
    }
    void menu()/*菜单响应 */
    {
    while(1)
    {
    Show_menu();
    switch(get_menu_choice())
    {
    case 1:Show();clear();
    break;
    case 2:Delete(file_operate("wb"));clear();
    break;
    case 3:Find();clear();
    break;
    case 4:Input(file_operate("ab"));clear();
    break;
    case 5:Alter(file_operate("wb"));clear();
    break;
    case 6:Save();clear();
    break;
    case 7:set_psw();clear();
    break;
    case 8:Sort_menu();
    break;
    case 0:system("cls");
    printf("***************************** ");
    printf("       欢迎使用本程序!       ");
    printf("***************************** ");
    exit(0);
    }
    }
    }
    void Show()/*显示打印所有的信息 */
    {
    int i;
    printf("有%d个记录: ",total);
    PrintTitle();
    for(i=0;i<total;i++)
    Showoneperson(i);
    }
    void Showoneperson(int i) /*显示打印一个人的信息 */
    {
    printf(" %8d    %8s    %1c    %3d    %3d     %3d      %3d ",student[i].no,student[i].name,student[i].sex,student[i].mark[0],student[i].mark[1],student[i].mark[2],student[i].mark[3]);
    }
    void PrintTitle()/*打印头信息  */
    {
    printf("----------------------------------------------------------------- ");
    printf("  学号            姓名  性别   语文   数学   英语   平均成绩   ");
    printf("----------------------------------------------------------------- ");
    }
    void Init() /*初始化,文件不存在,则建立,同时记录下文件中的记录数   */
    {
    int i;
    FILE *fp;
    total=0;
    if((fp=fopen("student.txt","rb"))==NULL) 
    {
    fp=fopen("student.txt","ab");
    clear();
    menu(); 
    }
    else 
    {
    for(i=0;feof(fp)==0;i++)
    Readfile(i,fp);
    }
    total=i-1; 
    fclose(fp);
    }
    void Readfile(int i,FILE *fp)/*读一条记录从文件 */
    {
    int j;
    fscanf(fp,"%8d",&student[i].no);
    fscanf(fp,"%8s  ",&student[i].name);
    fscanf(fp,"%1c",&student[i].sex);
    for(j=0;j<4;j++)
    fscanf(fp,"%3d",&student[i].mark[j]);
    }
    int CheckNumber(int no_temp)/*检查学号是否存在,存在返回序号,不存在返回-1 */
    {
    int i,result;
    for(i=0;i<total;i++)
    {
    if(student[i].no==no_temp)
    {
    result=1;
    break;
    }
    }
    if(result==1)
    return i;
    else
    return -1;
    }
    int Inputoneperson(int i)/*向数组增加一条记录  */
    {
        int j,sum=0;
    int no_temp;
    do
    {
    printf("输入学号:(如10141301)");
    scanf("%d",&no_temp);
    if(no_temp<10141301)
    printf("error!");
    }
    while(no_temp<10141301);
    if(CheckNumber(no_temp)>0)
    {
    printf("Number repeatly! ");
    return 0;
    }
    else
    {
    student[i].no=no_temp;
    printf("Input name(lessthan 20 numbers):");
    scanf("%s",student[i].name);
    printf("Sex(M/W):");
    scanf("%s",&student[i].sex);
    printf(" 语文 数学 英语 ");
    for(j=0;j<3;j++)
    {
    scanf("%d",&student[i].mark[j]);
    sum=sum+student[i].mark[j];
    }
    student[i].mark[3]=sum/3;
    PrintTitle(); 
    Showoneperson(i);
    return 1;
    }
    }
    void Input(FILE *fp)/*向管理系统中增加记录  */
    {
    int i;
    i=total;
    if(Inputoneperson(i))
    {
    total++;
    Inputfile(i,fp);
    }
    fclose(fp);
    }
    void Inputfile(int i,FILE *fp) /*把下标为i 的记录写入文件 */
    {
    int j;
    fprintf(fp,"%8d",student[i].no);
    fprintf(fp,"%8s  ",student[i].name);
    fprintf(fp,"%1c ",student[i].sex);
    for(j=0;j<4;j++)
    fprintf(fp,"%3d ",student[i].mark[j]);
    }


    void exchange(int i,int j)/*交换两个人的所有信息  */
    {
    int k;
    int no_temp,mark_temp;
    char name_temp[20],sex_temp;
    no_temp=student[i].no;
    student[i].no=student[j].no;
    student[j].no=no_temp;
    for(k=0;k<4;k++)
    {
    mark_temp=student[i].mark[k];
    student[i].mark[k]=student[j].mark[k];
    student[j].mark[k]=mark_temp;
    }
    strcpy(name_temp,student[i].name);
    strcpy(student[i].name,student[j].name);
    strcpy(student[j].name,name_temp);
    sex_temp=student[i].sex;
    student[i].sex=student[j].sex;
    student[j].sex=sex_temp;
    }
    FILE *file_operate(char *mode)/*文件操作 */
    {
    char choice;
    FILE *fp;
    do
    {
    fflush(stdin);
    if((fp=fopen("student.txt",mode))==NULL)
    {
    puts("Fail to open the file!");
    puts("Try again!(Y/N)?");
    scanf("%c",&choice);
    }
    }
    while(choice=='y'||choice=='Y');
    if(choice=='n'||choice=='N')
    exit(1); 
    return(fp);
    }
    void Find()/*查找记录的函数  */
    {
    int i,no_temp;
    FILE *fp;
    fp=file_operate("rb");
    for(i=0;feof(fp)==0;i++)
    Readfile(i,fp);
    total=i-1; 
    fclose(fp);
    printf("Input the number that someone you want to find:(如10141303)");
    scanf("%d",&no_temp);
    i=CheckNumber(no_temp);
    if(i>=0)
    {
    PrintTitle();
    Showoneperson(i);
    }
    else
    {
    printf("Nobody is the number:%d ",no_temp);
    }
    }
    void Save()/*备份信息到文件  */
    {
    int i;
    char filename[10],ch;
    FILE *fp;
    printf("Name the new file:(less than ten bits)");
    scanf("%s",filename);
    if((fp=fopen(filename,"wb"))==NULL)
    {
    printf("Fail to build the file! ");
    exit(0);
    }
    ch=getchar();
    for(i=0;i<total;i++)
    Inputfile(i,fp);
    Show();
    fclose(fp);
    }
    void Delete(FILE *fp)/*删除记录 */
    {
    int i,j,k,no_temp,choice2;
    printf("Inpute the number someone you needed:(如10141301)");
    scanf("%d",&no_temp);
    i=CheckNumber(no_temp);
    if(i>=0)
    {
    PrintTitle();
    Showoneperson(i);
    printf("Sure to delete the person?(1,Y/2,N)");
    scanf("%d",&choice2);
    if(choice2==1)
    {
    for(j=i;j<total;j++)
    {
    strcpy(student[j].name,student[j+1].name);
    student[j].sex=student[j+1].sex;
    student[j].no=student[j+1].no;
    for(k=0;k<4;k++)
    {
    student[j].mark[k]=student[j+1].mark[k];
    }
    }
    total--;
    for(k=0;k<total;k++)
    Inputfile(k,fp);
    }
    }
    else
       printf("Nobody is the number:%d ",no_temp);
    }
    void Alter(FILE *fp)/*修改记录信息  */
    {
    int i,j,no_temp,sum=0;
    printf("Inpute the number somesoe you want to alter:(如10141301)");
    scanf("%d",&no_temp);
    i=CheckNumber(no_temp);
    if(i>=0)
    {
    student[i].no=no_temp;
    printf("Input name(less than 20 numbers):");
    scanf("%s",student[i].name);
    printf("Sex(M/W):");
    scanf("%c",&student[i].sex);
    printf(" 语文 数学 英语 ");
    for(j=0;j<3;j++)
    {
    scanf("%d",&student[i].mark[j]);
    sum=sum+student[i].mark[j];
    }
    student[i].mark[3]=sum/3;
    PrintTitle();
    Showoneperson(i);
    for(j=0;j<total;j++)
    Inputfile(j,fp);
    }
    else
    printf("Nobody is the number:%d ",no_temp);
    }
    int Sort_menu_choice()/*选择是按学号还是成绩排序*/
    {
    int choice;
    do
    {
    printf("输入排序方式(1 按学号  2 按平均成绩): ");
    scanf("%d",&choice);
    if((choice<0)||(choice>2))
    printf("error!");
    }
    while((choice<0)||(choice>2));
    return(choice);
    }
    void Sort_no()/*选择是按学号排序*/
    {
    int i,j;
    for(i=0;i<total-1;i++)
    {
    for(j=i+1;j<total;j++)
    {
    if(student[i].no>student[j].no)
    exchange(i,j);
    }
    }
    }
    void Sort_mark()/*选择是按成绩排序*/
    {
    int i,j;
    for(i=0;i<total-1;i++)
    {
    for(j=i+1;j<total;j++)
    {
    if(student[i].mark[3]<student[j].mark[3])
    exchange(i,j);
    }
    }
    }
    void Sort_menu()/*排序菜单*/
    {
    switch(Sort_menu_choice())
    {
    case 1:Sort_no();
      Sort_save();
      clear();
      break;
    case 2:Sort_mark();
      Sort_save();
      clear();
      break;
    }
    }
    void Sort_save()/*排序后数据写入文件*/
    {
    int i;
    FILE *fp;
    if((fp=fopen("student.txt","wb"))==NULL)
    {
    printf("Fail to save the file! ");
    exit(0);
    }
    for(i=0;i<total;i++)
    Inputfile(i,fp);
    Show();
    fclose(fp);
    }


    void set_psw()/*设置密码  */
    {
    char psw_set[20],psw_check[20],c;
    unsigned int i;
    FILE *fp;
    do
    {   printf("You must set password first! ");
    printf("Enter password:(lessthan 12 numbers and key'.'for end) ");
    for(i=0;(c=getch())!='.';i++)
    {
    putchar('*');
    psw_set[i]=c;
    }
    psw_set[i]='';
    printf(" ------------ ");
    printf("conform password:");
    for(i=0;(c=getch())!='.';i++)
    {
    putchar('*');
    psw_check[i]=c;
    }
    psw_check[i]='';
            printf(" ------------ ");
    if(strcmp(psw_set,psw_check)==0)
    {printf("Set password success!");
    strcpy(password,psw_set);
    }
    else
    printf("error! ");
    }
    while(strcmp(psw_set,psw_check)!=0);    
    clear();
    fp=fopen("password.txt","wb");
    fprintf(fp,"%s",password);
    fclose(fp);
    }


    int psw_check()/*密码验证  */
    {
    unsigned int i=1,j=1;
    FILE *fp;
    char pword[20],c;
    if((fp=fopen("password.txt","rb"))==NULL) 
    {
    fp=fopen("password.txt","a"); 
    set_psw();
    }
    fscanf(fp,"%s",password); 
    fclose(fp);
    do
    {
    printf(" Input password:key'.'for end(%d/three times)",j);
    for(j=0;(c=getch())!='.';j++)
    {
    putchar('*');
    pword[j]=c;
    }
    pword[j]='';
    i++;
    }
    while(strcmp(pword,password)!=0&&i<=3);
    if(i<=3)
    return 1;
    else
    {
    printf("You have tryed for three times,fail to open the file! ");
    return 0;
    }


    }


    void main()/*主函数*/
    {
    if(psw_check())
    {
    Init();
    clear();
    menu();
    }
    }
  • 相关阅读:
    Lighthead SiteCrawler
    svnbook
    SFTP in Python: Really Simple SSH
    WP Robot Wordpress plugin logo
    use curl to post data for work punch
    Setting up Hudson on port 80 on a Debian or Ubuntu machine
    Problem with PEXPECT in Python
    H2 Database Engine — h2database.com — Readability
    execnet: rapid multiPython deployment
    Maven问题总结 3 技术改变生活商业成就梦想 51CTO技术博客
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211444.html
Copyright © 2020-2023  润新知