成都网站建设设计

将想法与焦点和您一起共享

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

文章目录
    • 一、 实验目的和要求
    • 二、实验环境(实验设备)
    • 三、实验原理及内容
      • 实验题目(1)【见实验教材实验八的题目3】:
        • ① 源程序代码如下:
        • ② 运行程序时,依次输入下面的几组年月日数据作为测试用例,观察程序的运行情况
      • 实验题目(2)【见实验教材实验九的题目1】:
      • 实验题目(3)【见实验教材实验九的题目2】:
        • ①请写出完整的源程序代码并做适当注释:
        • ② 运行程序,你从键盘输入的内容以及屏幕显示的结果如下:
    • 四、实验小结(包括问题和解决方法、心得体会、意见与建议、实验出错信息及解决方案等)
      • (一)实验中遇到的主要问题及解决方法
      • (二)实验心得
      • (三)意见与建议(没有可省略)

创新互联建站专注于临漳企业网站建设,响应式网站开发,商城建设。临漳网站建设公司,为临漳等地区提供建站服务。全流程按需制作网站,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务一、 实验目的和要求

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

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

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

三、实验原理及内容 实验题目(1)【见实验教材实验八的题目3】:

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

实验解答:

① 源程序代码如下:
#includestruct 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#includevoid 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#includestruct 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;ix=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;iint i,k,index;
	Stu temp;
	for(k=0;kindex=k;
		for(i=k+1;istu[index].score)
				index=i;
		if(index!=k)
		{	temp=stu[index];
			stu[index]=stu[k];
			stu[k]=temp;
		}
	}
	for(i=0;i
② 运行程序,你从键盘输入的内容以及屏幕显示的结果如下:
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
请按任意键继续. . .
四、实验小结(包括问题和解决方法、心得体会、意见与建议、实验出错信息及解决方案等) (一)实验中遇到的主要问题及解决方法

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

(二)实验心得

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

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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享文章:南京邮电大学高级语言程序设计实验六(结构体与文件实验)-创新互联
URL网址:http://chengdu.cdxwcx.cn/article/dhiehc.html