## 例题

#include<stdio.h>
int main()
{
struct student{//声明结构体类型
int num;//结构体成员
char name[20];
char sex;
}student_1={19,"Kakyoin",‘m’,"Harbin"};//定义结构体变量初始化
}
学号:19

--------------------------------
Process exited after 0.004355 seconds with return value 44

#include<stdio.h>
int main()
{
struct student{
int num;
char name[20];
float score;
}stu1={19,"Kakyoin",77},stu2={28,"Allams",90};
printf("成绩较高的是:\n");
if(stu1.score>stu2.score)
printf("学号：%d\n姓名:%s\n分数:%6.2f",stu1.num,stu1.name,stu1.score);
else if(stu1.score<stu2.score)
printf("学号：%d\n姓名:%s\n分数:%6.2f",stu2.num,stu2.name,stu2.score);
else
{
printf("学号：%d\n姓名:%s\n分数:%6.2f",stu1.num,stu1.name,stu1.score);
printf("学号：%d\n姓名:%s\n分数:%6.2f",stu2.num,stu2.name,stu2.score);
}
}
成绩较高的是:

--------------------------------
Process exited after 0.003391 seconds with return value 32

#include<stdio.h>
#include<string.h>
struct Perspon
{
char name[20];
int count;
int main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=0;j<3;j++)
}
printf("\nResult:\n");
for(i=0;i<3;i++)
return 0;
}  
Li
Li
Sun
Zhang
Li
Sun
Sun
Zhang
Li
Li

Result:
Li:5
Zhang:2
Sun:3

--------------------------------
Process exited after 33.29 seconds with return value 0

#include<stdio.h>
struct stu{
int num;
char name[20];
int score;
};
int main(){
struct stu student[4]={1,"Li",78,2,"Ze",68,3,"Hua",97,4,"Wei",96};
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   Li  78
2   Ze  68
3   Hua  97
4   Wei  96

--------------------------------
Process exited after 0.00389 seconds with return value 0

#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.00477 seconds with return value 0

#include<stdio.h>
struct student{
int num;
char name[20];
int age;
};
struct student stu[3]={1,"Li",20,2,"Ze",25,3,"Hua",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;
}
1  Li  20
2  Ze  25
3  Hua  25

--------------------------------
Process exited after 0.00326 seconds with return value 0

#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);
}
请输入各学生的信息：学号、姓名、三门课成绩：
1 Li 36 89 77
2 Ze 77 98 90
3 Hua 87 99 23

成绩最高的学生:

--------------------------------
Process exited after 852.9 seconds with return value 0

posted on 2017-04-11 19:33  Kakyoin  阅读(344)  评论(0编辑  收藏  举报