南京邮电大学高级语言程序设计实验六(结构体与文件实验)

一、 实验目的和要求

(1)掌握结构体类型以及结构体变量的定义与使用。
(2)综合运用结构体、数组、指针等知识,解决相关问题。
(3)会正确定义FILE*指针,掌握文件操作从打开、读写到关闭的完整过程。
(4)理解文本文件和二进制文件的区别和不同的读写方式。

二、实验环境(实验设备)

硬件: 微型计算机
软件: Windows 操作系统、Microsoft Visual Studio 2010

三、实验原理及内容

实验题目(1)【见实验教材实验八的题目3】:

编写程序exp8_3.c,验证用户输入的日期格式是否正确,如果不正确,则提示重新输入,直到重新输入正确为止。(提示:需要定义一个表示日期的结构体类型struct Date,包括年、月、日信息,并用typedef重新定义新类型名Date;检查日期是否有效,定义为函数int checkDate(Date date))。

实验解答:

① 源程序代码如下:
#include <stdio.h>
struct Date
{
	int year;
	int month; 
	int day;
};
typedef struct Date Date;
int checkData(Date date)
{
	if (date.year < 1900 || date.year>2018)
	{
		return 0;
	}
	if (date.month < 1 || date.month>12)
	{
		return 0;
	}
	if (date.month == 1 ||
		date.month == 3 ||
		date.month == 5 ||
		date.month == 7 ||
		date.month == 8 ||
		date.month == 10 ||
		date.month == 12  )
	{
		if (date.day < 1 || date.day > 31)
		{
			return 0;
		}
	}
	else if (date.month == 4 ||
			date.month == 6 ||
			date.month == 9 ||
			date.month == 11)
	{
		if (date.day < 1 || date.day > 30)
		{
			return 0;
		}
	}
	else
	{
		if (date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0)
		{
			if (date.day < 1 || date.day > 29)
			{
				return 0;
			}
		}
		else
		{
			if (date.day < 1 || date.day > 28)
			{
				return 0;
			}
		}
	}
	return 1;
}

int main()
{
	Date date; int flag;
	do
	{
		printf("please input date!\n");
		scanf_s("%d%d%d", &date.year, &date.month, &date.day);
		flag = checkData(date);
		if (flag)
		{
			printf("The date is valid!\n");
		}
		else
		{
			printf("Invalid date!\n");
		}
	} while (!flag);
	return 0;
}
② 运行程序时,依次输入下面的几组年月日数据作为测试用例,观察程序的运行情况
测试用例	输入的原始数据	需要重新输入或你的输出结果

用例1	2002  4  31	please input date!
2002  4  31
Invalid date!
please input date!
用例2	1809  12  3	please input date!
1809  12  3
Invalid date!
please input date!
用例3	2020  5  4	please input date!
2020  5  4
Invalid date!
please input date!
用例4	2000  2  29	please input date!
2000  2  29
The date is valid!
用例5	1908  14  23	please input date!
1908  14  23
Invalid date!
please input date!
用例6	2003  11  -8	please input date!
2003  11  -8
Invalid date!
please input date!
用例7	1996  2  31	please input date!
1996  2  31
Invalid date!
please input date!
用例8	1996  5  19	please input date!
1996  5  19
The date is valid!

实验题目(2)【见实验教材实验九的题目1】:

编写程序exp9_1.c ,从键盘读入一系列字符并以“#”结束,将读入的字符(不包括#号)存入文本文件D:\f1.txt中,再从该文件读取内容,并在显示器上原样显示。

实验解答: 写出完整的源程序代码并做适当注释:

#include<stdio.h>
#include<stdlib.h>
void writeFile(int ch, FILE* fp);
void readFile(int ch, FILE* fp);
int main()
{
	FILE* fp; 
	char ch = 0;
	fp = fopen("D:\\f1.txt", "w+");
	if (fp == NULL)   
	{
		printf("file error\n");
		exit(1);
	}
	writeFile(ch, fp);
	rewind(fp);
	readFile(ch, fp);
	fclose(fp);
	return 0;
}
void writeFile(int ch, FILE* fp)      
{
	printf("Please enter a string of characters ending with #:\n");
	ch = getchar();
	while (ch != '#')
	{
		fputc(ch, fp);
		ch = getchar();
	}
}
void readFile(int ch, FILE* fp)       
{
	while ((ch = fgetc(fp)) != EOF)
	{
		putchar(ch);
	}
	printf("\n");
}

实验题目(3)【见实验教材实验九的题目2】:

某班有学生若干名(不超过40名),其信息的组织采用如下的结构体定义。编写程序exp9_2.c,完成要求的功能。

struct Student
{
	char ID[20];
	char name[30];
	int age;
	double score;
};
  1. 从键盘读入该班级学生的信息。
  2. 将所有的学生信息存入D:\Info.dat文件中、关闭该文件,建立文件定义函数CreateFile实现。
  3. 另写一个函数ReadOut,将D:\Info.dat文件中的信息读入到内存,并依次输出到显示器上,该函数由main函数调用。
  4. 编写函数Sort,实现按成绩由高到低将学生记录进行排序并输出排序后的结果。
  5. 文件读写采用二进制读写(fread、fwrite)方式。

实验解答:

①请写出完整的源程序代码并做适当注释:
#include<stdio.h>
#include<stdlib.h>
struct Student
{
	char ID[20];
	char name[30];
	int age;
	double score;
};
typedef struct Student Stu;
#define N 50
void CreateFile(Stu [],int n,FILE *fp);
void ReadOut(Stu [],int n,FILE *fp);
void Sort(Stu [],int len);
int main()
{
	int n,i,x;
	Stu stu[N];
	FILE *fp=NULL;                       
	do
	{
		printf("Please input the number of students:\n");
	    scanf("%d",&n);
	}while(n<1||n>40);
	for(i=0;i<n;i++)
	{
		x=i+1;
		printf("%d(ID name age score):\n",x);
		scanf("%s%s%d%lf",stu[i].ID,stu[i].name,&stu[i].age,&stu[i].score);
	}
	CreateFile(stu,n,fp);
	printf("before being sorted:\n");
	ReadOut(stu,n,fp);
	printf("after being sorted:\n");
    Sort(stu,n);
	return 0;
}
void CreateFile(Stu stu[],int n,FILE *fp)
{
	fp=fopen("D:\\Info.dat","wb+");
	if(fp==0)                       //文?件t打䨰开a后¨®需¨¨判D断?是º?否¤?正y确¨¡¤
	{
		printf("file error\n");
		exit(1);
	}
	fwrite(stu,sizeof(Stu),n,fp);
	fclose(fp);
}
void ReadOut(Stu stu[],int n,FILE *fp)
{
	int i=0;
	fp=fopen("D:\\Info.dat","rb");
	if(fp==0)                       //文?件t打䨰开a后¨®需¨¨判D断?是º?否¤?正y确¨¡¤
	{
		printf("file error\n");
		exit(1);
	}
	fread(&stu[i],sizeof(Stu),n,fp);
	for(i=0;i<n;i++)
		printf("%s %s %d %.2f\n",stu[i].ID,stu[i].name,stu[i].age,stu[i].score);
	fclose(fp);
}
void Sort(Stu stu[],int len)
{
	int i,k,index;
	Stu temp;
	for(k=0;k<len-1;k++)
	{
		index=k;
		for(i=k+1;i<len;i++)
			if(stu[i].score>stu[index].score)
				index=i;
		if(index!=k)
		{
			temp=stu[index];
			stu[index]=stu[k];
			stu[k]=temp;
		}
	}
	for(i=0;i<len;i++)
		printf("%s %s %d %.2f\n",stu[i].ID,stu[i].name,stu[i].age,stu[i].score);
}
② 运行程序,你从键盘输入的内容以及屏幕显示的结果如下:
Please input the number of students:

3
1(ID name age score):
101 a 19 90
2(ID name age score):
102 b 18 99
3(ID name age score):
103 y 20 78
before being sorted:
101 a 19 90.00
102 b 18 99.00
103 y 20 78.00
after being sorted:
102 b 18 99.00
101 a 19 90.00
103 y 20 78.00
请按任意键继续. . .

四、实验小结(包括问题和解决方法、心得体会、意见与建议、实验出错信息及解决方案等)

(一)实验中遇到的主要问题及解决方法

验证日起合法性的判断条件,太繁琐。看书上的例题后找到可以将月份的天数存入数组。

(二)实验心得

对于文件的操作还应该继续熟悉与拓展。
多上机调试。

(三)意见与建议(没有可省略)

  • 21
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦是远方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值