抄写例题作业

9.1
把一个学生的信息(包括学号、姓名、性别、住址)放在一个结构体变量中,然后输出这个学生的信息。
复制代码
#include
int main()
{
    struct student
    {
        long int num;
        char name[20];
        char sex;
        char addr[20];
    }a = { 10101, "Lin", 'M', "123 Beijing Road" };
    aprintf("NO.:%1d\nname:%s\nsex:%c\naddress:%s\n", a.num, a.name, a.sex, a.addr);
    return 0;
}
/*NO.:10101
name:Lin
sex:M
address:123 Beijing Road
请按任意键继续. . .
*/

复制代码

在写此程序是并没有遇到什么问题!

9.2

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


复制代码
#include 
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\n", student1.num, student1.name, student1.score);
    else if (student1.score < student2.score)
        printf("%d  %s  %6.2f\n", student2.num, student2.name, student2.score);
    else
    {
        printf("%d  %s  %6.2f\n", student1.num, student1.name, student1.score);
        printf("%d  %s  %6.2f\n", student2.num, student2.name, student2.score);
    }
}
/*10101 Wang 89
10103 Lin 90
The higher score is:
10103  Lin   90.00
请按任意键继续. . .
*/

9.3

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


复制代码
#include 
struct person
{
    char name[20];
    int count;
} leader[3] = {"li",0, "zhan",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);
}
/*li
li
sun
zhan
zhan
sun
li
sun
zhan
li

Result:
li:4
zhan:3
sun:3
请按任意键继续. . .
*/

复制代码

9.4

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


复制代码
#include 
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;
    int i, j;
    printf("The order is :\n");
    for (i = 0; i < 4; i++)
       for (j = 0; j < 4 - i; j++)
           if (stu[j].score>stu[j + 1].score){
                temp = stu[j]; 
                stu[j] = stu[j + 1]; 
                stu[j + 1] = temp;
            }
    for (i = 0; i < 5; i++)
        printf("%6d%8s%6.2f\n", stu[i].num, stu[i].name, stu[i].score);
    printf("\n");
    return 0;
}
/*The order is :
 10108    ling 73.50
 10101   zhang 78.00
 10106      li 86.00
 10103    wang 98.50
 10110     sun100.00
 请按任意键继续. . .
*/        

复制代码

9.5

通过指向结构体变量的指针变量来输出结构体中成员的信息。


复制代码
#include 
#include <string.h>
int main()
{
    struct student
    {
        long num;
        char name[20];
        char sex;
        float score;
    };
    struct student stu1;
    struct student *p;
    p = &stu1;
    stu1.num = 10101;
    strcpy(stu1.name, "Li Lin"); 
    stu1.sex = 'M';
    stu1.score = 89.5;
    printf("No.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score);
    printf("\nNo.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n", (*p).num, (*p).name, (*p).sex, (*p).score);
}
/*No.:10101
name:Li Lin
sex:M
score: 89.5

No.:10101
name:Li Lin
sex:M
score: 89.5
请按任意键继续. . .

*/

复制代码

9.6

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


复制代码
#include 
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
请按任意键继续. . .
*/

复制代码

9.7

复制代码
#include
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
请按任意键继续. . .
*/
总结:通过这次作业让我跟进一步的了解了指针和结构体,知道了自己的不足之处,也使我的编程速度有所提升。

 

你可能感兴趣的:(抄写例题作业)