C语言 编程
编写一个程序 从键盘上输入五个学生的身高体重及姓名 将其存放在一个结构数组中 将该结构数组中的内容写入一个二进制文件 stu.dat中
2
从上题的文件中 读出五个学生的身高体重与姓名 并存放在一个结构数组中 输出 期中身高最高 好个体重最重的同学的信息(姓名 身高 体重) 展开
花了一个小时,帮你写了一个,可以在tc2.0或者vc6.0下直接运行,试试吧
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include<stdlib.h>
typedef struct stu{
char name[20];
float height;/*单位cm*/
float weight;/*单位kg*/
}Student;
int main()
{
int i,j,k;
char name[20];
float h,w;
Student *stu_p = NULL;
Student *stup[5];
FILE *fp = NULL;
if((fp = fopen("c:/stu.dat","w+b"))==NULL)/*打开文件*/
{
printf("Canit open file");
return -1;
}
printf("Please input 5 students' name,height and weight:\n");
for(i=0;i<5;i++)/*输入数据*/
{
for(j=0;j<20;j++)
name[j] = 0;
if(stu_p!=NULL)
free(stu_p);
stu_p = (Student *)malloc(sizeof(Student));/*申请一块内存放数据*/
printf("input student %d name: ",i+1);
scanf("%19s",name);
strcpy(stu_p->name,name);/*姓名*/
printf("input student %d height: ",i+1);
scanf("%f",&h);
stu_p->height = h;/*身高*/
printf("input student %d weight: ",i+1);
scanf("%f",&w);
stu_p->weight = w;/*体重*/
/*写入文件*/
fwrite(stu_p,sizeof(Student),1,fp);
}
fclose(fp);/*关闭文件*/
printf("\nThen to read the file message:\n");/*读文件*/
if((fp = fopen("c:/stu.dat","r+b"))==NULL)/*打开文件*/
{
printf("Canit open file");
exit(-1);
}
for( i=0;i<5;i++)
{
stup[i] = (Student *)malloc(sizeof(Student));
fread(stup[i],sizeof(Student),1,fp);
}
/*然后选身高最高 和 体重最大的学生*/
h = stup[0]->height;/*选身高*/
j = 0;
for(i=1;i<5;i++)
{
if(h<stup[i]->height)
{
h = stup[i]->height;
j = i;
}
}
w = stup[0]->weight;/*选体重*/
k = 0;
for(i=1;i<5;i++)
{
if(w<stup[i]->weight)
{
w = stup[i]->weight;
k= i;
}
}
/*输出结果*/
printf("The student %d has the most height:\n",j);
printf("Name:%s, Height:%f cm, Weight:%f kg \n\n",stup[j]->name,stup[j]->height,stup[j]->weight);
printf("The student %d has the most weight:\n",k);
printf("Name:%s, Height:%f cm, Weight:%f kg \n",stup[k]->name,stup[k]->height,stup[k]->weight);
fclose(fp);/*关闭文件*/
return 0;
}
再给你一个运行的截图看下:
struct student
{char name[20];
float heigh;
float weight;
}s[5];
void main()
{
int i;
struct student *p1,*p2;/*定义两个结构体指针*/
FILE *fp;
printf("plaese input students'information:\n");
for(i=0;i<5;i++) /*依次输入五个学生信息*/
scanf("%s%f%f",s[i].name,&s[i].heigh,&s[i].weight);
fp=fopen("/home/ccx/mydocument/stu.dat","wb+");/*为读写建立一个二进制文件stu.dat*/
for(i=0;i<5;i++)
if(fwrite(&s[i],sizeof(struct student),1,fp)!=1)
printf("file write error!\n");
fp=fopen("/home/ccx/mydocument/stu.dat","rb+");/*打开该二进制文件*/
for(i=0,p1=s,p2=s;i<5;i++)
{fread(&s[i],sizeof(struct student),1,fp);/*依次读取信息*/
if((p1+i)->heigh>p1->heigh) p1=p1+i;/*若后一个学生身高比前一个高,互换*/
if((p2+i)->weight>p2->weight) p2=p2+i;/*若后一个学生体重比前一个重,互换*/
}
printf("身高最高的是:%s %f %f\n",p1->name,p1->heigh,p1->weight);/*输出最高者*/
printf("体重最重的是:%s %f %f\n",p2->name,p2->heigh,p2->weight);/*输出最重者*/
fclose(fp);
}
运行结果:
plaese input students'information:
li 165 58
hu 190 65
she 178 64.5
he 174 62
wang 178 67
身高最高的是:hu 190.000000 65.000000
体重最重的是:wang 178.000000 67.000000
#include<stdio.h>
#define FILENAME "C:\\stu.dat"
struct tag_std
{
unsigned int height;
unsigned int weight;
char name[12];
};
typedef struct tag_std STD;
int main()
{
FILE *fp;
STD student[5];
int i;
printf("Please input five students' information\n"
"student's name , height and weight:\n");
for(i=0;i<5;i++)
{
printf("Student[%d]:",i+1);
scanf("%s%d%d",student[i].name,&student[i].height,&student[i].weight);
}
fp = fopen(FILENAME,"wb");
if(fp == NULL)
{
printf("ERROR in opening file! Pass any key to exit.");
getchar();
exit(0);
}
for(i=0;i<5;i++)
{
fprintf(fp,"%s %d %d\n",student[i].name,student[i].height,student[i].weight);
}
}
----------------------------------------------------------------------------------------------------------------------
第二题:
#include<stdio.h>
#define FILENAME "C:\\stu.dat"
struct tag_std
{
int height;
int weight;
char name[12];
};
typedef struct tag_std STD;
int main()
{
FILE *fp;
STD std[5];
int i;
fp = fopen(FILENAME,"rb");
if(fp == NULL)
{
printf("ERROR in opening file! Pass any key to exit.");
getchar();
exit(0);
}
for(i=0;i<5;i++)
{
fscanf(fp,"%s %d %d",std[i].name,&std[i].height,&std[i].weight);
}
for(i=0;i<5;i++)
{
printf("Student[%d]:",i+1);
printf("%s %d %d\n",std[i].name,std[i].height,std[i].weight);
}
}