抄写作业1

抄写作业1_第1张图片

 

 

 

9.1

#include
int main()
{
    struct Student                            //声明结构体类型 struct Student 
    {
        long int num;                        //以下4行为结构体的成员 
        char name[20];
        char sex;
        char addr[20];
    }a={10101,"Li Lin",'M',"Beijing Road"};//定义结构体变量a并初始化 
    printf("NO.:%1d\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr);
    return 0; 
}
NO.:10101
name:Li Lin
sex:M
address:Beijing Road

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

  9.2

#include"stdio.h"
int main()
{
    struct Student            //声明结构体类型  struct Student
    {
        int num;
        char name[20];
        float score;
    }student1,student2;        //定义两个结构体变量 student1,student2
    scanf("%d%s%f",&student1.num,student1.name,&student1.score);    //输入学生1的数据 
    scanf("%d%s%f",&student2.num,student2.name,&student2.score);    //输入学生2的数据 
    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);
    }
    return 0;
}
7 B 15
8 M 55
The higher score is:
8 M  55.00

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

  9.3

#include"stdio.h" 
struct Person            //声明结构体类型 struct Person
{
    char name[20];        //候选人姓名  
    int count;            //候选人得票数 
}leader[3]={"B",0,"H",0,"N",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;
}
B
B
H
N
N
H
B
H
N
B

Result:
    B:4
    H:3
    N:3

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

  9.4

#include"stdio.h"
    struct Student            //声明结构体类型  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;i1;i++)
            {
                k=i;
            if(stu[j].score>stu[k].score)//进行成绩比较 
                k=j;
            temp=stu[k];
            stu[k]=stu[i];
            stu[i]=temp;
            }
            for(i=0;i)
            printf("%6d %8s %6.2f",stu[i].num,stu[i].name,stu[i].score);
            printf("\n");
            return 0;
}
The order is:
 10101    zhang  78.00 10103     wang  98.50 10106       li  86.00 10108     ling  73.50 10110      sun 100.00

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

  9.5

#include"stdio.h"
#include"string.h"
int main()
{
    struct Student
    {
        long num;
        char name[20];
        char sex;
        float score;
    };
    struct Student stu_1;//定义structstudent类型的变量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("No.:%1d\nname:%s\nsex:%c\nscore:5.1%f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
    printf("\nNo.:%1d\nname:%s\nsex:%c\nscore:5.1%f\n",(*p).num,(*p).name,stu_1.sex,(*p).score);    
    return 0;
}
No.:10101
name:li lin
sex:M
score:89.5

No.:10101
name:li lin
sex:M
score:89.5

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

  9.6

#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},{10104,"Wang Min",'F',20}};

int main()
{
    struct Student *p;
    printf("No.    name                sex age\n");
    for(p=stu;p3;p++)
    printf("%5d  %-20s%2c %4d\n",p->num,p->name,p->sex,p->age);
    return 0;
 } 
No.    name                sex age
10101  li lin               M   18
10102  Zhang Fang           M   19
10104  Wang Min             F   20

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

  9.7

#include   
#define N 3  
struct Student  
{  
    int num;  
    char name[20];  
    float score[3];  
    float aver;  
};  
int main()
{
    void input(struct Student stu[]);
    struct Student max(struct Student stu[]);
    void print(struct Student stu);
    struct Student stu[N],*p=stu;
    input(p);
    print(max(p));
    return 0; 
}
void input(struct Student stu[])
{
    int i;
    printf("请输入各学生的信息:学号、姓名、三门学科的成绩:\n");
    for(i=0;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=0;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平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2]);
}
请输入各学生的信息:学号、姓名、三门学科的成绩:
01 B 12 13 14

02 Y 15 16 16

03 L 18 28 20

成绩最高的学生是:
学号:3
姓名:L
三门课成绩: 18.0, 28.0, 20.0平均成绩:  22.00

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

  总结:经过这次例题抄写,发现这部分知识点的题型中结构题大都相似,可以举一反三,看起来并不难的题,实际敲起来也并不容易。

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