当前位置:网站首页>Simple C language exercise: access to student data
Simple C language exercise: access to student data
2022-04-22 07:56:00 【halftion】
Problem description :
Data files are now provided students.txt( The data of this topic is randomly generated by the program , Untrue data ), Inside is the student's information , The sample data is as follows :
full name , Student number , Gender , Age , class
Zhang San ,2017002871, male ,17, big data 1702
Li Si ,2017001765, Woman ,18, Software Engineering 1712
path ,2016009876, male ,20, big data 1701
kid ,2014002715, male ,19, Computer 1402
Wang Ludan ,2017009479, Woman ,22, big data 1702
Zhang San ,2015009321, male ,22, Xinji 1502
requirement :
a) Read in students.txt Store the data in the memory , The student information is required to be stored in the form of structure array .
b) Query the student information in the memory according to the user's input , You need to support the following three query modes , You need to provide a menu for users to select query mode :
i. Query mode 1: Query student information according to name or student number
ii. Query mode 2: Query student information according to age range
iii. Query mode 3: Query student information according to class
3. I/o sample :
a) Query mode 1
Examples 1:
Input : Zhang San
Output :
Zhang San ,2017002871, male ,17, big data 1702
Zhang San ,2015009321, male ,22, Xinji 1502
Examples 2:
Input : 2017009876
Output :
path ,2016009876, male ,20, big data 1701
b) Query mode 2
Examples 1:
Input : 18 20
Output :
Li Si ,2017001765, Woman ,18, Software Engineering 1712
path ,2016009876, male ,20, big data 1701
kid ,2014002715, male ,19, Computer 1402
c) Query mode 3
Examples 1:
Input : big data 1702
Output :
Zhang San ,2017002871, male ,17, big data 1702
Wang Ludan ,2017009479, Woman ,22, big data 1702
The problem is simple c Language practice , It is mainly to practice the use of structures and the reading of files , And when reading %[^] Use
The program code is as follows :
#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;
}
版权声明
本文为[halftion]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220624413619.html
边栏推荐
- Unity mask click the lower UI game to start guiding Click
- 【TCP/IP 五 ICMP】
- C-9结构体:计算输入日期是该年的第几天
- STM32 peripherals [iv] RCC
- 多线程(线程通信【生产者与消费者】)
- Circular explanation and various small details
- Callable结合CountDownLatch实战应用
- VC call control
- Unity mask reverse mask implementation
- Autoware displays user interface details
猜你喜欢

QT动态翻译中英文语言

Run program ~ customize similar CMD commands to open non system software

OBD external test equipment initialization judgment protocol type (SAE J1939 / iso15765 / iso27145)

观察者模式--ApplicationContext

UNITY遮罩点击下层UI游戏开始引导点击

【TCP/IP 一 概述】
![Rt-thread [一] 创建工程](/img/cd/9a2a9c7381caaf150b3700e348e38a.png)
Rt-thread [一] 创建工程

一种简易的ESP32图文Web服务器的实现方式

树莓派配置清单(入门参考)
![STM32 peripherals [iv] RCC](/img/e1/e81d1e6017bebc87690227a7aa5f99.png)
STM32 peripherals [iv] RCC
随机推荐
Inverse Polish quaternion
Stm32外设篇 [三] 串口 RS232 RS485
Principle of single chip microcomputer [1] five diagrams that must be mastered by single chip microcomputer well
Read wwh-obd (iso27145 protocol) data stream
Circular explanation and various small details
【TCP/IP 一 概述】
C#程序初解析 及编写注意事项
OBD external test equipment initialization judgment protocol type (SAE J1939 / iso15765 / iso27145)
基於卷積神經網絡LeNet-5模型的mnist手寫數字識別
读取SAE J1939协议数据流
Autoware显示用户界面细节
ActiveX控件使用总结
树莓派:访问Bitlocker To Go加密过的磁盘
C#自制一个简单的树莓派IP寻找工具
MySQL 中文字段排序问题(根据中文拼音排序)
JWT realizes the whole process of login authentication, password encryption and token verification (with source code)
【C - 属性】
Unity mask reverse mask implementation
C#中分支的使用
【TCP/IP 五 ICMP】