例题9.1

 

#include<stdio.h>
struct STD{
    int num;
    char name[20];
    char sex;
    char addr[20];
}; 
int main(){
    struct STD student={10,"sanlvzi",'M',"daqing"};
    printf("num:%d\nname:%s\nsex:%c\naddr:%s\n",student.num,student.name,student.sex,student.addr);
    return 0;
}
.

 

num:10
name:sanlvzi
sex:M
addr:daqing

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

  出现的错误:字符串和字符输出的时候%s个%c用错了,编译的时候没有错误,但是执行的时候直接关闭了程序。

例题9.2

#include<stdio.h>
struct stu{
    int num;
    char name[20];
    int score;
}; 
int main(){
     struct stu student1;
     struct stu student2;
     scanf("%d%s%d",&student1.num,&student1.name,&student1.score);
     scanf("%d%s%d",&student2.num,&student2.name,&student2.score);
     if(student1.score>student2.score)
     printf("%d%s%d\n",student1.num,student1.name,student1.score);
     else if(student1.score<student2.score)
     printf("%d%s%d\n",student2.num,student2.name,student2.score);
     else
     {
     printf("%d  %s  %d\n",student1.num,student1.name,student1.score);
     printf("%d  %s  %d\n",student2.num,student2.name,student2.score);
     }
     return 0;
}

 

98 liu 99
93 mark 99
98  liu  99
93  mark  99

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

  

例题9.3

#include<stdio.h>
#include<string.h>
struct per{
    char name[20];
    int count;
}leader[3]={"liu",0,"duan",0,"yien",0};//利用数组定义结构体变量 
int main(){
    int j,i; 
    char leader_name[20];
    for(i=0;i<10;i++){
        scanf("%s",leader_name);
        for(j=0;j<3;j++)
        if(strcmp(leader_name,leader[j].name)==0)//C/C++函数,比较两个字符串,只能比较字符串,不能比较数字等其他形式的参数。
        leader[j].count++;
    }
    printf("\nresult:\n");
    for(i=0;i<3;i++)
    printf("%s:%d\n",leader[i].name,leader[i].count);
    return 0;
}

 

liu
liu
liu
duan
duan
duan
duan
yien
yien
liu

result:
liu:4
duan:4
yien:2

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

  出现的错误:运用strcmp等等的函数总会记混。

例题9.4

#include<stdio.h>
struct stu{
    int num;
    char name[20];
    int score;
};
int main(){
    struct stu student[4]={1,"liu",99,2,"nin",88,3,"sun",77,4,"yue",66};
    struct stu temp;
    const int n=4;//const是一个C语言的关键字,它限定一个变量不允许被改变
    int i,j,k;
    for(i=0;i<n-1;i++)//利用冒泡法排序 
    {
        k=i;
        for(j=i+1;j<n;j++){
            if(student[i].score>student[k].score)
            k=j;
            temp=student[k];student[k]=student[i];student[i]=temp;//把成绩高的换到前面 
        }
        for(i=0;i<n;i++)
           printf("%d   %s  %d\n",student[i].num,student[i].name,student[i].score);
    }
    return 0;
}

 

1   liu  99
2   nin  88
3   sun  77
4   yue  66

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

  出现的错误:冒泡法循环用时候总会把for循环使用错。

例题9.5

#include<stdio.h>
struct student{
    int num;
    char name[20];
    char sex;
    int score;
};
int main(){
    struct student stu={10,"mark",'M',100};//定义变量stu并赋值 
    struct student *p;//定义指向struct student 类型数据的指针变量p 
    p=&stu;//p指向 stu 
    printf("用stu打印:\n");
    printf("%d  %s  %c  %d\n",stu.num,stu.name,stu.sex,stu.score); 
    printf("用指针打印:\n");
    printf("%d  %s  %c  %d\n",(*p).num,(*p).name,(*p).sex,(*p).score);
    return 0;
}

 

用stu打印:
10  mark  M  100
用指针打印:
10  mark  M  100

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

  

例题9.6

#include<stdio.h>
struct student{
    int num;
    char name[20];
    int age;
}; 
struct student stu[3]={1,"liu",20,2,"yie",25,3,"ena",25};
int main(){
    struct student *p;//定义一个struct student类型的指针 
    p=&stu[0];//指向数组首元素 
    for(p=stu;p<(stu+3);p++)
    printf("%d  %s  %d\n",(*p).num,(*p).name,(*p).age);
    return 0;
}

 

 liu  20
 yie  25
 ena  25

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

  

例题9.7

#include<stdio.h>  
#define N 3  
  
struct Student  
{  
    int num;  
    char name[20];  
    float score[3];  
    float aver;  
};  
  
int main()  
{  
    void input(struct Student stu[]);  //定义一个struct类型的input函数,作用是从终端输入struct Student里面的内容。并且计算每个学生的各科平均成绩
    struct Student max (struct Student stu[]);  //定义struct类型的max函数,用来比较平均成绩。成绩高的返回
    void print(struct Student stu);  //调用print函数输出成绩高的学生信息
    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 < N;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;  
    int m = 0;;  
    for(i = 0; i < N;i ++)  
    {  
        if(stu[i].aver > stu[m].aver)  
        {  
            m = i;  
        }  
    }  
    return stu[m];  
}  
  
void print(struct Student stud)  
{  
    printf("\n 成绩最高的学生:\n");  
    printf("学号:%d\n",stud.num);  
    printf("姓名:%s\n",stud.name);  
    printf("三门课成绩:%5.1f,%5.1f,%5.1f\n",stud.score[0],stud.score[1],stud.score[2]);  
    printf("平均成绩:%6.2f\n",stud.aver);  
}

 

 

请输入各学生的信息:学号、姓名、三门课成绩:
11 li 78 89 98
12 wa 95.5 84 66
16 sa 59.3 66 89.9

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

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

  

  总结:这个程序是将三名学生的信息输入,求出个学生的平均数,找出最大值 ,并输出成绩最高的信息我为找出成绩最高的同学,将所有同学的平均数放到一个数组中,通过for循环将最大值赋值给变量f再通过for循环找出最大值是哪个同学。我是照书写的,自己也写了,但是出现了编程保存类型的错误,编译不会出现阻止,但是会出现警告。

  心得:通过最后一个例题才知道自己有好多不足。比如调用函数的时候定义的和调用的时候往往没考虑类型。抓住一个就直接调用。以至于程序一直在提示错误。以后会多加练习调用函数。

posted on 2017-04-09 20:18  lnmark  阅读(269)  评论(0编辑  收藏  举报