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

版权声明:本文为博主原创文章。 https://blog.csdn.net/qq_41061352/article/details/79048656

问题描述:

现提供数据文件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.\n");
        system("pause");
        exit(1);
    }
    for(num=0;!feof(fp);num++)//read the file
    {
        fscanf(fp,"%[^,],%[^,],%[^,],%d,%s\n",&students[num].number,students[num].name,students[num].sex,&students[num].age,students[num].classes);
    }
    while(1)
    {
        system("cls");
        printf("               __________________________________________\n");
        printf("    ----------|Welcome to the student information system|-----------\n");
        printf("              ------------------------------------------\n");
        printf("    |mode1:Query student information by name or student number.     |\n");
        printf("    |mode2:Inquire about student information according to age range.|\n");
        printf("    |mode3:Check the student information according to the class.    |\n");
        printf("    |mode4:List all student information.                            |\n");
        printf("    |There are %d student's information in the system              |\n",num);
        printf("     ---------------------------------------------------------------\n");
        printf("    Please enter the query mode,enter is the return key.Enter 'q' to exit the program.\n");
        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\n",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\n");
            }

            else
            {
                for(num1=0,num3=0;num1<num;num1++)
                    {
                        if(!strcmp(temp,students[num1].number))
                        printf("%s,%s,%s,%d,%s\n",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\n");
            }
            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\n",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\n");
                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\n",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\n");
            system("pause");
        }
        else if (mode[0]==52)//mode4
        {
            system("cls");
            printf("name,number,sex,age,class\n");
            for(num1=0;num1<num;num1++)
            {
                printf("%s,%s,%s,%d,%s\n",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\n");
            system("pause");
        }
    }
    fclose(fp);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41061352/article/details/79048656