• 简单c语言练习:学生数据库的制作


    问题描述:

    现提供数据文件students.txt(本题目数据由程序随机生成,非真实数据),里面存放的是学生的信息,数据样例如下:

    姓名,学号,性别,年龄,班级

    张三,2017002871,,17,大数据1702

    李四,2017001765,,18,软件工程1712

    小路,2016009876,,20,大数据1701

    大张伟,2014002715,,19,计算机1402

    王璐丹,2017009479,,22,大数据1702

    张三,2015009321,,22,信计1502

     

    要求:

    a)      读入students.txt里的数据存到内存,要求学生信息以结构体数组的方式存储。

    b)     根据用户的输入在内存里查询学生信息,需要支持以下三种查询模式,需要提供给用户选择查询模式的菜单:

     

    i.       查询模式1:根据姓名或学号查询学生信息

    ii.      查询模式2:根据年龄范围查询学生信息

    iii.    查询模式3:根据班级查询学生信息

     

    3.输入输出样例:

    a)      查询模式1

    样例1

    输入张三

    输出

    张三,2017002871,,17,大数据1702

    张三,2015009321,,22,信计1502

    样例2

    输入 2017009876

    输出

    小路,2016009876,,20,大数据1701

    b)     查询模式2     

    样例1

    输入 18 20

    输出

    李四,2017001765,,18,软件工程1712

    小路,2016009876,,20,大数据1701

    大张伟,2014002715,,19,计算机1402

    c)     查询模式3

    样例1

    输入大数据1702

    输出

    张三,2017002871,,17,大数据1702

    王璐丹,2017009479,,22,大数据1702

    这道题是简单的c语言练习,主要是练习结构体的使用以及文件的读取,以及读取时%[^]的使用

    程序代码如下:

    #include <stdio.h>
    #include <string.h>
    struct student
    {
        char name[30];
        char number[30];
        char sex[10];
        int age;
        char classes[30];
    };
    int main()
    {
        int num,num2,num3,num4;
        long int num1;
        FILE *fp;
        char temp[30],mode[30];
        struct student students[1000];
        if((fp=fopen("./students.txt","r"))==NULL)//open the file
        {
            printf("can not open file students.txt,please check if the file is in the same path.
    ");
            system("pause");
            exit(1);
        }
        for(num=0;!feof(fp);num++)//read the file
        {
            fscanf(fp,"%[^,],%[^,],%[^,],%d,%s
    ",&students[num].number,students[num].name,students[num].sex,&students[num].age,students[num].classes);
        }
        while(1)
        {
            system("cls");
            printf("               __________________________________________
    ");
            printf("    ----------|Welcome to the student information system|-----------
    ");
            printf("              ------------------------------------------
    ");
            printf("    |mode1:Query student information by name or student number.     |
    ");
            printf("    |mode2:Inquire about student information according to age range.|
    ");
            printf("    |mode3:Check the student information according to the class.    |
    ");
            printf("    |mode4:List all student information.                            |
    ");
            printf("    |There are %d student's information in the system              |
    ",num);
            printf("     ---------------------------------------------------------------
    ");
            printf("    Please enter the query mode,enter is the return key.Enter 'q' to exit the program.
    ");
            printf("    mode:");
            scanf("%s",&mode);//the welcome screen
            fflush(stdin);
            if(mode[0]==49)//mode1
            {
                system("cls");
                printf("please input the student's name or number:");
                scanf("%29s",temp);
                fflush(stdin);
                if(temp[0]<0)
                {
                    for(num1=0,num3=0;num1<num;num1++)
                        {
                            if(!strcmp(temp,students[num1].name))
                            printf("%s,%s,%s,%d,%s
    ",students[num1].name,students[num1].number,students[num1].sex,students[num1].age,students[num1].classes),num3++;
                        }
                    if(num3==0) printf("can not find this name
    ");
                }
    
                else
                {
                    for(num1=0,num3=0;num1<num;num1++)
                        {
                            if(!strcmp(temp,students[num1].number))
                            printf("%s,%s,%s,%d,%s
    ",students[num1].name,students[num1].number,students[num1].sex,students[num1].age,students[num1].classes),num3++;
                        }
                    if(num3==0) printf("can not find this number
    ");
                }
                system("pause");
            }
            else if(mode[0]==50)//mode2
            {
                system("cls");
                printf("please input the age range you need tu query:");
                scanf("%d %d",&num2,&num3);
                getchar();
                for(num1=0,num4=0;num1<num;num1++)
                    {
                        if(students[num1].age>=num2&&students[num1].age<=num3)
                        printf("%s,%s,%s,%d,%s
    ",students[num1].name,students[num1].number,students[num1].sex,students[num1].age,students[num1].classes),num4++;
                    }
                    if(num4==0) printf("can not find a term that meet the crietra
    ");
                    getch();
            }
            else if(mode[0]==51)//mode3
            {
                system("cls");
                printf("please input the class you want to find:");
                scanf("%s",temp);
                getchar();
                for(num1=0,num3=0;num1<num;num1++)
                        {
                            if(!strcmp(temp,students[num1].classes))
                            printf("%s,%s,%s,%d,%s
    ",students[num1].name,students[num1].number,students[num1].sex,students[num1].age,students[num1].classes),num3++;
                        }
                if(num3==0) printf("the class wan not found
    ");
                system("pause");
            }
            else if (mode[0]==52)//mode4
            {
                system("cls");
                printf("name,number,sex,age,class
    ");
                for(num1=0;num1<num;num1++)
                {
                    printf("%s,%s,%s,%d,%s
    ",students[num1].name,students[num1].number,students[num1].sex,students[num1].age,students[num1].classes);
                }
                system("pause");
            }
            else if(mode[0]=='q') break;//exit
            else//input error
            {
                printf("can not find this mode
    ");
                system("pause");
            }
        }
        fclose(fp);
        return 0;
    }
    


  • 相关阅读:
    文件下载(Servlet/Struts2)
    Spring学习(一)---依赖注入和控制反转
    MyBatis学习(三)---MyBatis和Spring整合
    MyBatis学习(二)---数据表之间关联
    MyBatis学习(一)---配置文件,Mapper接口和动态SQL
    转载:常见端口介绍
    CentOS7 yum提示:another app is currently holding the yum lock;waiting for it to exit
    批量删除文件,只保留目录下最新的几个文件,其他均删除
    转载:SQL Server 如何设置数据库的默认初始大小和自动增长大小
    阿里云ECS使用秘钥或者密码登录
  • 原文地址:https://www.cnblogs.com/halftion/p/9470359.html
Copyright © 2020-2023  润新知