TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/*
- 5. 用scanf_s函数从键盘读入 5个学生数据(包括:学生名、学号、三门课程的分数),
- 然后求出平均分数。用 fprintf函数输出所有信息到磁盘文件stud.rec中,
- 再用 fscanf_s函数从 stud.rec中读入这些数据并在显示屏上显示出来。
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Student9_3_5
- {
- char name[20];
- int num;
- float chinese;
- float math;
- float english;
- }Stu;
- /*
- 保存学生信息
- */
- void savaStuInfo95(Stu * stu,int n)
- {
- Stu temp;
- FILE *fp = NULL;
- fopen_s(&fp, "stud.rec","wb");
- if (fp)
- {
- float avg;
- for (int i = 0; i < n; i++)
- {
- temp = stu;
- avg = (temp.chinese + temp.math + temp.english) / 3;
- fprintf(fp,"num=%d,chinese=%f,math=%f,english=%f,avg=%f",temp.num,
- temp.chinese,temp.math,temp.english,avg);
- }
- fclose(fp);
- }
- else
- {
- printf("open file failed!");
- }
- }
- /*
- 读取学生信息
- */
- void readStuInfo95()
- {
- FILE *fp = NULL;
- Stu stu;
- float avg;
- fopen_s(&fp, "stud.rec","rb");
- if (fp)
- {
- while(fscanf_s(fp,"num=%d,chinese=%f,math=%f,english=%f,avg=%f",&(stu.num),
- &(stu.chinese),&(stu.math),&(stu.english),&avg) != EOF)
- {
- printf("num = %d chinese = %f math = %f english = %f avg = %f\n",stu.num,
- stu.chinese,stu.math,stu.english,avg);
- }
- fclose(fp);
- }
- else
- {
- printf("open file failed!");
- }
- }
- void main()
- {
- const int N = 5;
- Stu stu[N];
- for (int i = 0; i< N; i++)
- {
- printf("请输入第%d个学生姓名:\n",i + 1);
- scanf_s("%s",&stu.name);
- printf("请输入第%d个学生学号:\n",i + 1);
- scanf_s("%d",&stu.num);
- printf("请输入第%d个学生语文成绩:\n",i + 1);
- scanf_s("%f",&stu.chinese);
- printf("请输入第%d个学生数学成绩:\n",i + 1);
- scanf_s("%f",&stu.math);
- printf("请输入第%d个学生英语成绩:\n",i + 1);
- scanf_s("%f",&stu.english);
- }
- savaStuInfo95(stu,N);
- readStuInfo95();
- system("pause");
- }</pre><br><br></pre></pre>
|
|