抄写例题作业1

例题9.1

#include<stdio.h>
#include<stdlib.h>
main()
{struct Student
{long int num;
char name[20];
char sex;
char addr[20];
}a={10101,"li lin",'M',"123 beijing Road"};
printf("NO.;%ld\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:123 beijing Road

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

  

例题9.2

#include<stdio.h>
#include<stdlib.h>
int main()
{struct Student
 {int num;
char name[20];
float score;
}; 
struct Student student1={10101,"wang",89};
struct Student student2={10103,"ling",98};
if(student1.score>student2.score)
printf("%d,%s,%f",student1.num,student1.name,student1.score);
else if(student1.score<student2.score)
printf("%d,%s,%f",student2.num,student2.name,student2.score);
else
{
printf("%d,%s,%f",student1.num,student1.name,student1.score);
printf("%d,%s,%f",student2.num,student2.name,student2.score);
}
return 0;
}
10103,ling,98.000000
--------------------------------
Process exited after 0.3591 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;
}
Li
Li
Sun
Zhang
Zhabg
Sun
Li
Sun
Zhang
Li

Result:
   li:0
zhang:0
  Sun:3

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

  例题9.4

#include<stdio.h>
#include<stdlib.h> 
struct Student//声明结构体类型 struct person 
{
int num;
char name[20];
float score;
};
int main()
{
struct Student stu[5]={{10101,"zhang",78},{10103,"wang",98},{10106,"li",86},{10108,"ling",73},{10110,"sun",100}};//定义结构体数组并初始化
struct Student temp;
const int n=5;
int i,j,k;//定义常变量n 
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;    //stu[k]和stu[i]元素互换 
}
for(i=0;i<n;i++)
printf("%d,%s,%d",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;
}
the order is:
10110,sun,010103,wang,010106,li,010101,zhang,010108,ling,0

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

  例题9.5

#include<stdio.h>
#include<stdlib.h> 
struct Student//声明结构体类型 struct person 
{
int num;
char name[20];
char sex;
int age;
};
struct Student stu[3]={{10101,"zhang",'M',78},{10103,"wang",'M',98},{10106,"li",'M',86}};//定义结构体数组并初始化 
main()
{struct Student *p;//定义指向struct student类型数据的指针变量p 
printf("NO.Name   sex age\n");
for(p=stu;p<stu+3;p++)
printf("%d,%s,%c,%d",p->num,p->name,p->sex,p->age);
return 0;
}
NO.Name   sex age
10101,zhang,M,7810103,wang,M,9810106,li,M,86
--------------------------------
Process exited after 0.4277 seconds with return value 0
请按任意键继续. . .

  例题9.6

#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:Mscore:0

--------------------------------
Process exited after 0.4299 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 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 < 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);  
}
请输入各学生的信息:学号、姓名、三门课成绩:
21 zh 36 37 38
22 ji 25 26 28
23 li 54 23 12

 成绩最高的学生:
学号:21
姓名:zh
三门课成绩: 36.0, 37.0, 38.0
平均成绩: 37.00

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

  总结:

代码敲得还是很慢,以后多加练习。

posted on 2017-04-10 16:41  入骨温  阅读(235)  评论(0)    收藏  举报

导航