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

#include <stdio.h>
int main()
{
    struct Student 
    {
        long int num;
        char name[20];
        char sex;
        char addr[20];
    } a = { 10101, "Li Lin", 'M', "123 Beijng Road" };
    printf("NO.:%1d\nname:%s\nsex:%c\naddress:%s\n", a.num ,a.name,a.sex,a.addr);
    return 0;
}
 num:10101
 name:LiLin
 sex:M
 addr:123 bei jing road
 
 --------------------------------
 Process exited after 0.2346 seconds with return value 50
 请按任意键继续. . .
总结一·
此题先构造结构体类型,然后在赋初值,最后输出,比较简单,只要会构造结构体即可。

例题二 输入2个学生的学号,姓名和成绩,输出成绩较高的学生的学号,姓名和成绩。

 

#include<stdio.h>
  struct stu
  {
      int num;
      char name[30];
      float score;
  };
  int main()
  {
     struct stu student1,student2;
     scanf("%d%s%f",&student1.num,student1.name,&student1.score);
     scanf("%d%s%f",&student2.num,student2.name,&student2.score);
     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);
     }
}

101 li 100
102 er 89
101 li 100.00
--------------------------------
Process exited after 177.3 seconds with return value 15
请按任意键继续. . .

总结二·本题主要关于结构体变量,成员名,还有涉及到以前的最值函数,scanf函数。

注意结构体变量的正确用法及对成员的赋值。

此题有一些难度,但熟知上课知识点较容易。

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

#include<string.h>
#include<stdio.h>
struct Person//声明结构体类型 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++;
 }
 printf("\nResult:\n");
 for(i=0;i<3;i++)
 printf("%5s:%d\n",leader[i].name,leader[i].count);
 return 0;
  }

li
li
sun
zhang
zhang
sun
li
sun
zhang
li

Result:
li:4
zhang:3
Sun:0

--------------------------------
Process exited after 36.81 seconds with return value 0
请按任意键继续. . .

总结三、本题主要关于定义和引用结构体数组。所以我们首先要定义结构体数组,中途运用了if函数、for循环来使函数简洁。

注定义结构体数组一般形式是先结构体名在声明一个结构体类型后对其初始化。

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

#include<stdio.h>
#include<stdio.h>
struct student
{
int num;
char name[20];
float score;
};
int main()
{
struct student Stu[5]={{10101,"Zhang",78},
{10103,"Wang",98.5},
{10106,"Li",86},
{10108,"Ling",73.5},
{10110,"Sun",100}};
struct student temp;
const int n=5;
int i,j,k;
printf("The order is:\n");
for(i=0;i<n-1;i++)
{ k=i;
for(j=i+1;j<n;j++)
if(Stu[j].score>Stu[k].score)
k=j;
temp=Stu[k];Stu[k]=Stu[i];Stu[i]=temp;
}
for(i=0;i<n;i++)
printf("%6d %8s %6.2f\n",Stu[i].num,Stu[i].name,Stu[i].score);
printf("\n");
return 0;
}

The order is:
10110 Sun 100.00
10103 Wang 98.50
10106 Li 86.00
10101 Zhang 78.00
10108 Ling 73.50


--------------------------------
Process exited after 0.1874 seconds with return value 0
请按任意键继续. . .

总结四、此题采用按照以前的知识--选择法 与上一题来比多了一个选择法,但同样重视结构体数组的初始化,

更鲜明的表示结构体类型的好处---不必人为的指定一个个成员互换。

例题五 通过指向结构体变量的指针变量来输出结构体中成员的信息。
#include<stdio.h>
#include<string.h>
main()
{struct Student
{long num;
char name[20];
char sex;
float score;
};
struct Student stu_1;//定义struct student类型的变量stu_1
struct Student *p;//定义指向struct student类型数据的指针变量p
p=&stu_1;//p指向stu—1
stu_1.num=10101;//对结构体变量的成员赋值
strcpy(stu_1.name,"li lin");//用字符串复制函数给stu_1.name赋值
stu_1.sex='M';
stu_1.score=89;
printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",(*p).num,(*p).name,(*p).sex,(*p).score);
return 0;
}

NO.:10101

name:li lin
sex:M
score:0
NO.:10101
name:li lin
sex:M
score:0
 
--------------------------------
Process exited after 2.478 seconds with return value 0
请按任意键继续. . .
总结五、本题着重表示结构体指针,怎么通过指针赋值及访问成员。
注 (*p).num=p->num 注意运算符的用法。
例题六  有3个学生的信息,放在结构体数组中,要求输出全部学生的信息。
#include <stdio.h>
struct student
{
    int num;
    char name[20];
    char sex;
    int age;
};
struct student stu[3] = { { 10101, "Li Lin", 'M', 18 }, { 10102, "Zhang Fang", 'M', 19 }, { 10103, "Wang Min", 'F', 20 } };
int main()
{
    struct student *p;
    p = stu;
    printf("No.   Name                sex age\n");
    for (; p < stu + 3; p++)
        printf("%5d %-20s%2c%4d\n", p->num, p->name, p->sex, p->age);
}

No. Name sex age
10101 Li Lin M 18
10102 Zhang Fang M 19
10103 Wang Min F 20

--------------------------------
Process exited after 0.7347 seconds with return value 4206752
请按任意键继续. . .

总结六、此题用结构体指针处理,与前几道题思路差不多,但要注意使p指向结构体数组的首元素及其有关信息,并运用循环使其在输出。

例题七     有n个结构体变量,内含学生学号,姓名和3门课程成绩。要求输出平均成绩最高的学生的信息(包括学号,姓名,3门课程成绩和平均成绩)。
#include<stdio.h>
struct student
{
    int num;
    char name[20];
    float score[3];
    float aver;
};
void input(struct student stu[])
{
    int i;
    printf("请输入各学生的信息:学号,姓名,三门课成绩:\n");
    for (i = 0; i < 3; i++){
        scanf("%d%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
        stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
    }
}
struct student max(struct student stu[])
{
    int i, m = 0;
    for (i = 1; i<3; i++)
        if (stu[i].aver>stu[m].aver) m = i;
    return stu[m];
}
void print(struct student stud)
{
    printf("\n成绩最高的学生是:\n");
    printf("学号:%d\n姓名: %s\n三门课成绩: %5.1f ,%5.1f,%5.1f\n平均成绩:%5.1f\n", stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2], stud.aver);
}
int main()
{
    //void input (struct student stu[]);
    //struct student max(struct student stu[]);
    //void print(struct student stu);
    struct student stu[3], *p = stu;
    input(p);
    print(max(p));
    return 0;
}

请输入各学生的信息:学号,姓名,三门课成绩:
10101 li 78 89 98
10103 wang 98.5 87 69
10106 sun 88 76.5 89

成绩最高的学生是:
学号:10101
姓名: li
三门课成绩: 78.0 , 89.0, 98.0
平均成绩: 88.3

--------------------------------
Process exited after 68.58 seconds with return value 0
请按任意键继续. . .

 总结七、用结构体变量及变量的指针做函数参数。

要运用input max printf 三个函数来编译,但三个函数调用情况不同,他们的实参形参不同,要区分开,才能运行出来

 
posted on 2017-04-09 22:52  李胡仪  阅读(384)  评论(0编辑  收藏  举报