(完整 word 版)C 语言程序设计基础-结构体习题
1、把一个学生的信息(包括学号、姓名、性别、住址)放在一个结构体变量中,然后输出这个学生的信息.
#include 〈stdio.h〉
int main()
{struct Student
{
long int num;
char name[20];
char sex;
char addr[20];
}a={10101,“Li Lin”,‘M’, “123 Beijing Road”};
printf(”NO。:%ldname:%s sex:%caddress:%s”,a.num,a.name,a。sex,a。addr);
return 0;
}
2、输入两个学生的学号、姓名和成绩,输出成绩较高学生的学号、姓名和成绩
#include 〈stdio。h〉
int main()
{ struct Student
{
int num;
char name[20];
float score;
}student1,student2;
scanf("%d%s%f",&student1.num,student1.name, &student1。score);
scanf(“%d%s%f”,&student2.num,student2.name, &student2.score);
printf(”The higher score is:\n");
if (student1.score〉student2。score)
printf("%d %s %6.2f”,student1.num,student1.name, student1.score);
else if (student1.score<student2.score)
printf("%d %s %6。2f”,student2。num,student2。name, student2。score);
else
{printf(”%d %s %6。2f”,student1。num,student1。name, student1.score);
printf(”%d %s %6.2f”,student2.num,student2。name, student2.score);
}
return 0;
}
3、有 3 个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后
输出各人得票结果。
#include <string.h〉
#include <stdio。h>
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++)