南京邮电大学C语言实验报告六

实验六:结构体与文件实验

实验题目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 main()
{
	int b[13]={0,31,0,31,30,31,30,31,31,30,31,30,31};
	Date a[20];
	do
	{
		printf("Please input date(XXXX XX XX):\n");
		scanf("%4d%d%d",&(a->year),&(a->month),&(a->day));
		if(a->month==2)
		{
			if((a->year%4==0&&a->year%100!=0)||a->year%400==0)
				b[2]=29;
			else b[2]=28;
		}
	}while((a->month<1||a->month>12)||(b[a->month]<(a->day)||a->day<1));
	printf("%d-%d-%d",a->year,a->month,a->day);
	return 0;
	}

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

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

# include <stdio.h>
# include<stdlib.h>
# include<string.h>
# define N 20
int main()
{
	FILE * fp;
	char str[N];
	int lines=0;
	fp=fopen("D:\\f1.txt","w");  //以写的方式打开文件
	if(fp==NULL)  //如果打开文件失败
	{
		printf("file error\n");
		exit(1);
	}
	gets(str);
	while(strcmp(str,"#")!=0)  //如果读入的不是“#”串则写入文件
	{
		fputs(str,fp);  //将读入的字符串写入文件
		fputc('\n',fp);  //换行
		gets(str);  继续读入一串字符
	}
	rewind(fp);
	while(fgets(str,N,fp)!=NULL)
	{
		printf("%s",str);  //原样输出该字符
		lines++;
	}

	fclose(fp);  //关闭文件
	return 0;
}

实验题目(3)【见实验教材实验九的题目2】:某班有学生若干名(不超过40名),其信息的组织采用如下的结构体定义。编写程序exp9_2.c,完成要求的功能。

struct Student

{

        char ID[20];

        char name[30];

        int age;

        double score;

};

① 从键盘读入该班级学生的信息。

② 将所有的学生信息存入D:\Info.dat文件中、关闭该文件,建立文件定义函数CreateFile实现。

③ 另写一个函数ReadOut,将D:\Info.dat文件中的信息读入到内存,并依次输出到显示器上,该函数由main函数调用。

④编写函数Sort,实现按成绩由高到低将学生记录进行排序并输出排序后的结果。

⑤文件读写采用二进制读写(fread、fwrite)方式。实验解答:  ①请写出完整的源程序代码并做适当注释:

#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("%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("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("D:\\Info.dat","r+");
	if ( fp == 0 )                         /* 如果文件打开失败*/
	{	printf( "open binary file error\n" );
		exit(1);
	}
	for(i=0;i<n;i++)
		scanf("%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("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);
}
  • 36
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cookie爱吃小饼干

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

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

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

打赏作者

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

抵扣说明:

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

余额充值