C语言程序设计基础-结构体习题 下载本文

内容发布更新时间 : 2024/5/19 0:46:28星期一 下面是文章的全部内容请认真阅读。

1、把一个学生的信息(包括学号、姓名、性别、住址)放在一个结构体变量中,然后输出这个学生的信息。 #include int main()

{struct Student {

long int num; char name[20]; char sex;

char addr[20];

}a={10101,“Li Lin”,‘M’, “123 Beijing Road”};

printf(\ sex:%c\\naddress:%s\\n\ return 0; }

2、输入两个学生的学号、姓名和成绩,输出成绩较高学生的学号、姓名和成绩 #include int main()

{ struct Student {

int num;

char name[20]; float score;

}student1,student2;

scanf(\ scanf(“%d%s%f”,&student2.num,student2.name, &student2.score);

printf(\ if (student1.score>student2.score)

printf(\ else if (student1.score

printf(\ else

{printf(\ printf(\ }

return 0; }

3、有3个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果。 #include #include

struct Person { char name[20];

int count; }leader[3]={“Li”,0,“Zhang”,0,“Sun”,0}; int main()

{ int i,j; char leader_name[20]; for (i=1;i<=10;i++) { scanf(“%s”,leader_name); for(j=0;j<3;j++) if(strcmp(leader_name,

leader[j].name)==0) leader[j].count++; }

for(i=0;i<3;i++)

printf(\n“,leader[i].name,

leader[i].count); return 0; }

4、有n个学生的信息(包括学号、姓名、成绩),要求按照成绩的高低顺序输出各学生的信息。

#include struct Student

{ int num; char name[20]; float score; }; int main() { struct Student stu[5]={{10101,\ 86 },{10108,“Ling”, 73.5},{10110,“Fun”, 100 } }; struct Student temp; const int n = 5 ; int i,j,k;

printf(\ for(i=0;i

for(j=i+1;j

if(stu[j].score>stu[k].score) k=j; temp=stu[k]; stu[k]=stu[i];

stu[i]=temp;

}

for(i=0;i

printf(\

stu[i].num,stu[i].name,stu[i].score); printf(\ return 0; }

5、通过指向结构体变量的指针变量输出结构体变量中成员的信息。 #include #include int main()

{ struct Student { long num;

char name[20]; char sex; float score; };

struct Student stu_1; struct Student * p; p=&stu_1;

stu_1.num=10101;

strcpy(stu_1.name,“Li Lin”);

stu_1.sex='M‘; stu_1.score=89.5; printf(\n”,p->.num); printf(\ printf(\n”, p->.sex);

printf(”score:%5.1f\\n”,stu_1.score); return 0; }

6、有3个学生的信息,放在结构体数组中,要求输出全部学生的信息。 #include

struct Student { int num; char name[20]; char sex; int age; };

struct Student stu[3]={{10101,\Lin\Fun\Min\ int main()

{ struct Student *p;

printf(\ Name sex age\\n\ for(p=stu;p

printf(“] %-20s , M\\n”,p->num, p->name, p->sex, p->age); return 0; }

7、有n个结构体变量,内含学生学号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息(包括学号、姓名、3门课程成绩和平均成绩)。