C语言 编程

作者&投稿:布怪 (若有异议请与网页底部的电邮联系)

花了一个小时,帮你写了一个,可以在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:
");

   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("
Then to read the file message:
");/*读文件*/

     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:
",j);

printf("Name:%s,     Height:%f cm,      Weight:%f kg 

",stup[j]->name,stup[j]->height,stup[j]->weight);

printf("The student %d has the most weight:
",k);

printf("Name:%s,     Height:%f cm,      Weight:%f kg 
",stup[k]->name,stup[k]->height,stup[k]->weight);

  fclose(fp);/*关闭文件*/

  return 0;

}

再给你一个运行的截图看下:



#include<stdio.h>
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);
}
}

楼主可以先编写一下,之后我给你修改一下,看一下你的问题所在,我感觉比直接给你好

c语言编程~

LZ没把上次运行的程序关掉,造成了连接错误,把上次运行的程序关掉就行了

我考,中午看见了和这4个一样的题。。。
1,下面程序段将输出 computer,请填空。
i<Strlen(charc)
if(i<7) continue;
2,,strcmp(str[0],str[1])<0?str[0]:str[1]
s
3, 在以下程序,数组 a 中存放一个递增数列。输入一个整数 x,并将它插入到数组 a 中,使该数组仍为一个递增数列。请选择正确的答案。
D (因为要插入另外一个x 所以必须要11)
A(跳出循环A)
D(向前遍历)
D(插入x)