C语言编程题,谢谢帮忙
Itusesthetextfiletoaccessthestudentinformation.ThestudentinformationcontainsstudentID...
It uses the text file to access the student information. The student information contains student ID and grade. Student ID can only be integer and up to 9-bit; grade can only be positive integer and its value range is between 0 and 100. 1. Interface There are three menu items in the main interface altogether. They are “1.Save Student Information”, “2. Read Student Information” and “0.Exit System”, respectively.
2. Text File Storage Format Save the student information into “studentInfo.txt” file under the project directory. The student information contains student ID and grade. Student ID and grade are separated with “##”. Each item of the student information occupies a line. The student information storage format is as below:
3. Functionalities (1) Save Student Information into Text File Input the menu item number “1”, prompt the user to input student ID and grade, save the input student ID and grade into “studentInfo.txt” file in append mode, and then output the main interface.
(2) Read Student Information from Text File Input the menu item number “2”, read the student information from “studentInfo.txt” file, display the student information with the form of the list, and then output the main interface.
(3) Exit Input the menu item number “0”, then, prompt the user “Exit System” and exit the program. (4) Error Prompts If the user input other numbers, prompt the user “The menu item that you input is wrong!”, then, output the main interface 展开
2. Text File Storage Format Save the student information into “studentInfo.txt” file under the project directory. The student information contains student ID and grade. Student ID and grade are separated with “##”. Each item of the student information occupies a line. The student information storage format is as below:
3. Functionalities (1) Save Student Information into Text File Input the menu item number “1”, prompt the user to input student ID and grade, save the input student ID and grade into “studentInfo.txt” file in append mode, and then output the main interface.
(2) Read Student Information from Text File Input the menu item number “2”, read the student information from “studentInfo.txt” file, display the student information with the form of the list, and then output the main interface.
(3) Exit Input the menu item number “0”, then, prompt the user “Exit System” and exit the program. (4) Error Prompts If the user input other numbers, prompt the user “The menu item that you input is wrong!”, then, output the main interface 展开
1个回答
展开全部
#include<stdio.h>
#define N 4096
int main(){
int a[N];
int b[N];
int m;
int i;
FILE * fp;
int num=0;
while(1){
printf("1.Save student information\n");
printf("2.Read student information\n");
printf("0.Exit system\n");
printf("Please input menu item:");
scanf("%d",&m);
switch (m){
case 1:
fp =fopen("studentInfo.txt","at");
printf("Please input student ID:");scanf("%d",&a[num]);
printf("Please input score:");scanf("%d",&b[num]);
fprintf(fp,"%d##%d\n",a[num],b[num]);num++;
printf("-----------------------------------\n");
fclose(fp);
break;
case 2:fp = fopen("studentInfo.txt","r");
if(fp){
num = 0;
while(fscanf(fp,"%d##%d",&a[num],&b[num]) != EOF){
num++;
}
fclose(fp);
printf("Student ID\tScore\n");
for(i=0;i<num;i++){
printf("%d\t%d\n",a[i],b[i]);
}
}
else{
printf("There is no student information at present\n");
}
printf("-----------------------------------\n");
break;
case 0:printf("Exit System\n");
getchar();
return 0;
break;
default:
printf("The menu item that you input is wrong!\n");
printf("-----------------------------------\n");
break;
};
}
return 0;
}
运行结果:
1.Save student information
2.Read student information
0.Exit system
Please input menu item:2
There is no student information at present
-----------------------------------
1.Save student information
2.Read student information
0.Exit system
Please input menu item:1
Please input student ID:123456789
Please input score:100
-----------------------------------
1.Save student information
2.Read student information
0.Exit system
Please input menu item:2
Student ID Score
123456789 100
-----------------------------------
1.Save student information
2.Read student information
0.Exit system
Please input menu item:1
Please input student ID:987654321
Please input score:100
-----------------------------------
1.Save student information
2.Read student information
0.Exit system
Please input menu item:2
Student ID Score
123456789 100
987654321 100
-----------------------------------
1.Save student information
2.Read student information
0.Exit system
Please input menu item:0
Exit System
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询