c语言实验报告6_56课时

本文介绍了三个实验:一、使用结构体定义学生信息并实现输入、排序和输出功能;二、读写文本文件并提取字母及ASCII码;三、处理学生信息录入、保存与读取,按成绩排序。实验中涉及了结构数组、文件操作和基本算法应用。
摘要由CSDN通过智能技术生成

实验报告六

文件与结构数组实验

实验题目(1)编写程序exp6_1.c,定义一个存储学生信息的结构体类型 struct Student(该类型因为要被多个函数使用,因此定义在所有函数之前),学生信息包括学号、姓名、3门课成绩,平均成绩等。本题除了主函数以外,还需要定义以下3个函数,各函数原型及功能说明如下: ① 定义输入函数void Input(struct Student* pa,int n);实现输入n个学生的学号、姓名、3门课成绩,并求出每个学生的平均成绩。 ② 定义排序函数void Sort(struct Student * pa,int n);实现按平均分对结构数组排序。 ③ 定义输出函数void Output(const struct Student* pa,int n);实现输出结构体数组的所有元素。 ④ 主函数中首先定义一个结构体数组;接着调用Input函数输入若干个学生记录(学生人数不能超过结构体数组的元素个数);在主函数中直接对结构体数组的元素进行判断,只要有一门课的成绩低于60分,则输出该学生的完整信息;然后在主函数中调用排序函数按平均分排序,最后调用输出函数输出按平均分排序后的各学生记录。 ⑤ 用调试器观察结构变量中成员的变化情况,正确使用F10和F11进行单步跟踪。

源代码:

`#include<stdio.h>
#include<stdlib.h>
struct Student
{
	char ID[20];//学号
	char name[20];//姓名
	int score[3]; //成绩
	double ave;//平均分
};
void Input(struct Student* pa, int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		scanf_s("%s%s%d%d%d", (pa + i)->ID, (pa + i)->name, &pa[i].score[0], &pa[i].score[1], &pa[i].score[2]);
		pa[i].ave = (double)(pa[i].score[0] + pa[i].score[1] + pa[i].score[2]) / 3;//定义计算平均分
	}
}
void Sort(struct Student *pa, int n)
{
	int i, j;
	struct Student t;
	for(i = 0; i < n -1;i++)
		for(j = i + 1; j < n; j++)
			if (pa[i].ave < pa[j].ave)//比较大小
			{
				t = pa[i];
				pa[i] = pa[j];
				pa[j] = t;
			}
}
void Output(const struct Student* pa, int n)
{
	int i;
	for (i = 0; i < n; i++)
		printf("%12s%12s%5d%5d%5d%7.2f\n", pa[i].ID, pa[i].name, pa[i].score[0], pa[i].score[1],pa[i].score[2], pa[i].ave);
}

int main()
{
	struct Student s[3];
	Input(s, 3);
	Sort(s, 3);
	Output(s, 3);
	return 0;
}`

实验题目(2)编写程序exp6_2.c,对文本文件D:\f1.txt作如下处理(D:\f1.txt的内容事先用记事本或其他工具编辑建立)。通过程序: ① 读入文件D:\f1.txt所有的内容,并原样输出到显示器上。 ② 将其中所有的字母及其对应的ASCII码输出到另外一个文件D:\f2.txt中。

源代码:

`#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() 
{
FILE *fp,*fq;
char ch;
fp=fopen_s("c:\\f1.txt","r+");//以读/写的方式打开一个文本文件
if (fp==NULL)
{
printf("File f1 open error\n");
exit(1);
}
fq=fopen_s("c:\\anyo\\f2.txt","w+");//以读/写的方式建立一个新的文本文件
if (fq==NULL)
{
printf("File f2 open error\n");
exit(1);
}
while ((ch=fgetc(fp))!=EOF)
{
putchar(ch);
if (isalpha(ch))
{
fputc(ch,fq);
fprintf(fq,"%x",ch);
} 
}

fclose(fp);
fclose(fq); 
return 0;
}    `

实验题目(3)某班有学生若干名(不超过50名),每个学生的信息采用如下的结构体定义。编写程序完成要求的功能。 struct Student { char ID[20]; char name[30]; int age; double score; }; ① 从键盘读入该班级所有学生的信息。 ② 将所有的学生信息存入D:\Info.dat文件中、关闭该文件。 ③ 另写一个函数ReadOut,将D:\Info.dat文件中的信息读入到内存并原样显示。 ④ 编写函数Sort实现按成绩由高到低将学生记录进行排序。

源代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Student
{
	char ID[20];
	char name[30];
	int age;
	double score;
}Student;
void CreateFile();
void WriteFile(Student st[], int n);
void ReadOutFile(Student st[]);
void Sort(Student st[], int n);
void Printf(Student st[], int n);
int main()
{
	Student st[40];
	int n;
	printf("How many scores you want to input?\n");
	scanf_s("%d", &n);
	CreateFile();
	printf("Plaese input scores:\nID\tName\tAge\tScore\n");
	WriteFile(st, n);
	ReadOutFile(st);
	printf("\nYou input:\n");
	Printf(st, n);
	Sort(st, n);
	printf("\nAfter sorting:\n");
	Printf(st, n);
	return 0;
}
void CreateFile()   //创建文件                                
{
	FILE* fp;
	fp = fopen_s("D:\\Info.dat", "wb");
	if (fp == 0)                         //如果文件创建失败
	{
		printf("Creat file error\n");
		exit(1);
	}
	fclose(fp);
}
void WriteFile(Student st[], int n)//写入数据
{
	int i;
	FILE* fp;
	fp = fopen_s("D:\\Info.dat", "r+");
	if (fp == 0)                         //如果文件打开失败
	{
		printf("open binary file error\n");
		exit(1);
	}
	for (i = 0; i < n; i++)
		scanf_s("%s%s%d%lf", st[i].ID, st[i].name, &st[i].age, &st[i].score);
	fwrite(st, sizeof(Student), n, fp);
	fclose(fp);
}
void ReadOutFile(Student st[])//读取数据
{
	FILE* fp;
	int i = 0;
	fp = fopen_s("D:\\Info.dat", "rb");
	if (fp == 0)                         //如果文件打开失败
	{
		printf("open file error\n");
		exit(1);
	}
	fread(&st[i], sizeof(Student), 1, fp);	       //从文件中读出下表记录
	while (!feof(fp))                     //当文件未结束时循环
	{
		i++;                                //下标加以备读入
		fread(&st[i], sizeof(Student), 1, fp);	  // 读一条记录 
	}
	fclose(fp);      //关闭文件
}
void Sort(Student stu[], int n)//排序
{
	int i, a, j;
	Student temp;
	for (i = 0; i < n - 1; i++)
	{
		a = i;
		for (j = i + 1; j < n; j++)
			if (stu[a].score < stu[j].score)  //比较两元素的大小
				a = j;
		if (a != i)
		{
			temp = stu[i];
			stu[i] = stu[a];
			stu[a] = temp;
		}
	}
}
void Printf(Student st[], int n)//输出数据
{
	int i;
	for (i = 0; i < n; i++)
		printf("%s\t%s\t%d\t%lf\n", st[i].ID, st[i].name, st[i].age, st[i].score);

实验总结:虽然能运行出来,但警告有点多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值