part 1 验证性实验

验证性实验1

验证性实验2:已知文本数据文件file1.dat,从中读取数据,找出最高分和最低分学生信息,并输入在屏幕上。

运行结果如下图:

#include <stdio.h>
#include <stdlib.h> #define N 10
typedef struct student {
int num;
char name[];
int score;
}STU; int main() {
STU st, stmax, stmin;
int i;
FILE *fp; fp = fopen("file1.dat", "r");
if( !fp ) {
printf("fail to open file1.dat\n");
exit();
} stmax.score = ;
stmin.score = ;
while( !feof(fp) ) {//修改处,源代码为:for(i=0; i<N; i++)
fscanf(fp, "%d %s %d", &st.num, st.name, &st.score);
if(st.score > stmax.score)
stmax = st;
else if(st.score < stmin.score)
stmin = st;
} fclose(fp); printf("最高分学生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score);
printf("最低分学生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score); return ;
}

运行结果如图:

显然,对line29做了修改后,程序运行结果和之前一样。

验证性实验3:

实验3源代码:

#include <stdio.h>
#include <stdlib.h> #define N 10 typedef struct student {
int num;
char name[];
int score;
}STU; void sort(STU *pst, int n); int main() {
FILE *fin, *fout;
STU st[N];
int i;
fin = fopen("file1.dat", "r");
if( !fin ) {
printf("fail to open file1.dat\n");
exit();
}
for(i=; i<N; i++)
fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);
fclose(fin);
sort(st, N);
fout = fopen("file3.dat", "w");
if( !fout ) {
printf("fail to open file1.dat\n");
exit();
}
for(i=; i<N; i++) {
printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
}
fclose(fout);
return ;
} void sort(STU *pst, int n) {
STU *pi, *pj, t;
for(pi = pst; pi < pst+n-; pi++)
for(pj = pi+; pj < pst+n; pj++)
if(pi->score < pj->score) {
t = *pi;
*pi = *pj;
*pj = t;
} }

运行结果如图:

验证性实验4:

实验4源代码:

#include <stdio.h>
#include <stdlib.h>
#define N 10 typedef struct student {
int num;
char name[];
int score;
}STU; void sort(STU *pst, int n); int main() {
FILE *fin, *fout;
STU st[N];
int i;
fin = fopen("file1.dat", "r");
if( !fin ) {
printf("fail to open file1.dat\n");
exit();
}
for(i=; i<N; i++)
fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);
fclose(fin);
sort(st, N);
fout = fopen("file3.dat", "wb");
if( !fout ) {
printf("fail to open file1.dat\n");
exit();
}
for(i=; i<N; i++) {
printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
}
fclose(fout);
return ;
} void sort(STU *pst, int n) {
STU *pi, *pj, t;
for(pi = pst; pi < pst+n-; pi++)
for(pj = pi+; pj < pst+n; pj++)
if(pi->score < pj->score) {
t = *pi;
*pi = *pj;
*pj = t;
} }

运行结果如图:

比较实验3和实验4,比较文本文件和二进制文件的区别。

打开记事本,

实验3:

实验4:

所以,二进制文件和文本文件的区别之一是读取的软件不同。

文件文本像记事本这种编辑器就可以直接读写。而二进制文本需要特别的解码器。

part 2:编程练习

修改examinee.txt中的测试数据,给出程序的运行源码以及运行的结果截图。

在input()和output()函数中,请添加必要的注释,增加程序源码的可读性。

程序运行源码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int N = ; // 定义结构体类型struct student,并定义其别名为STU
typedef struct student
{
long int id;
char name[];
float objective; /*客观题得分*/
float subjective; /*操作题得分*/
float sum;
char level[];
}STU; // 函数声明
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n); int main()
{
STU stu[N]; printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n");
process(stu, N); printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n");
output(stu, N); return ;
} // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void input(STU s[], int n)
{
FILE *fin;
int i;
if((fin=fopen("examinee.txt","r"))==NULL)
{
printf("cannot open file");
exit();
}
for(i=;i<n;i++)
fscanf(fin,"%d %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); fclose(fin);
} // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
// 不仅输出到屏幕上,还写到文本文件result.txt中
void output(STU s[], int n)
{
FILE *fout;
int i;
if((fout=fopen("result.txt","w"))==NULL)
{
printf("cannot open file");
exit();
}
for(i=;i<n;i++)
{
printf("%5d %10s %5.1f %5.1f %5.1f %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
fprintf(fout,"%5d %10s %5.1f %5.1f %5.1f %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); }
fclose(fout);
}
// 对考生信息进行处理:计算总分,排序,确定等级
void process(STU s[], int n)
{
int i,j;
STU temp;
for(i=;i<n;i++)
{
s[i].sum=0.4*s[i].objective+0.6*s[i].subjective;
}
for(i=;i<n-;i++)
{
for(j=;j<n-i-;j++)
{
if(s[j].sum<s[j+].sum)
{
temp=s[j];
s[j]=s[j+];
s[j+]=temp;
}
}
}
for(i=;i<n;i++)
{
if(i<0.1*n)
strcpy(s[i].level,"优");
else if(i<0.5*n&&i>=0.1*n)
strcpy(s[i].level,"良");
else if(i>=0.5*n)
strcpy(s[i].level,"不合格");
}
}

运行结果:

实验总结和体会:

就是按部就班地照着实验指示完成,很多东西似懂非懂,云里雾里。

那个比较二进制文件和文本文件的题,自己并分析不出来(或者说不太会表述清楚),上网查了,然后再根据实验结果写的。

c语言实验7 文件的更多相关文章

  1. 2006: C语言实验——拍皮球

    2006: C语言实验——拍皮球 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 231  Solved: 162[Submit][Status][Web ...

  2. R语言- 实验报告 - 利用R语言脚本与Java相互调用

    一. 实训内容 利用R语言对Java项目程序进行调用,本实验包括利用R语言对java的.java文件进行编译和执行输出. 在Java中调用R语言程序.本实验通过eclipse编写Java程序的方式,调 ...

  3. c语言中的文件流

    一.打开和关闭文件 #include int main( void ) { FILE* pReadFile = fopen( "E:\\mytest.txt", "r&q ...

  4. C语言样式的文件操作函数

    使用C语言样式的文件操作函数,需要包含stdio.h头文件. 1.打开文件的函数: //oflag的取值为“w”或“r”,分别表示以写或读的方式打开 FILE* fd = fopen(filename ...

  5. c语言的头文件-不是c++类的头文件?

    下面的概述是参考的这篇文章:http://blog.csdn.net/bingxx11/article/details/7771437 c语言编程中也有,也需要头文件, 头文件不只是C++的类才需要! ...

  6. ytu 2002:C语言实验——单词统计(水题)

    C语言实验——单词统计 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 61  Solved: 34[Submit][Status][Web Board] ...

  7. ytu 1998:C语言实验——删除指定字符(水题)

    C语言实验——删除指定字符 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 327  Solved: 211[Submit][Status][Web Boa ...

  8. ytu 1985:C语言实验——保留字母(水题)

    C语言实验——保留字母 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 136  Solved: 59[Submit][Status][Web Board] ...

  9. ytu 2029: C语言实验——温度转换(水题)

    2029: C语言实验——温度转换 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 12  Solved: 10[Submit][Status][Web B ...

随机推荐

  1. 渲染路径-Deferred Lighting 延时光照

    http://blog.csdn.net/heyuchang666/article/details/51564954 注意: 最后3个步骤注意下 延时光照是有着最高保真度的光照和阴影的渲染路径.如果你 ...

  2. Unity3D脚本:更改脚本和类名,且不破坏现有脚本引用的方法

    Unity开发过程中,难免遇到需要修改类名(以及对应脚本名)的时候,但如果草率地在MonoDevelop里把类名直接改掉,会破坏现有场景以 及Project Assets中所有引用该脚本的链接,届时还 ...

  3. E: 软件包 ffmpeg 没有可供安装的候选者

    问题:在DSO安装依赖项ffmpeg时遇到“E: 软件包 ffmpeg 没有可供安装的候选者”这一问题. 解决:在Ubuntu上gstreamer0.10-ffmpeg属于额外的版权受限程序,gstr ...

  4. Gym - 101810D ACM International Collegiate Programming Contest (2018)

    bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...

  5. Codeforces Round #390 (Div. 2) B

    Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. ...

  6. 关于JVM的一些东西

    1.在JDK1.6(HotSpot虚拟机)及之前,运行时常量池(属于方法区的一部分)是永久代的,而在JDK1.7之后运行时常量池(里面用于存放编译期生成的各种字面量和符号引用,这部分内容将在类加载后进 ...

  7. 使用openssl 生成免费证书

    阅读目录 一:什么是openssl? 它的作用是?应用场景是什么? 二:使用openssl生成免费证书 回到顶部 一:什么是openssl? 它的作用是?应用场景是什么? 即百度百科说:openssl ...

  8. Python/WSGI 应用快速入门--转

    http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html 这个快速入门指南将会向你展示如何部署简单的 WSGI 应用和普 ...

  9. Nginx 开启目录浏览功能配置

    在server节点下添加 server { listen ; server_name default; #index index.php; # 目录浏览功能 autoindex on; # 显示文件大 ...

  10. 上白泽慧音——tarjian

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...