## 例题抄写

#include<stdio.h>
int main()
{
struct Student//声明一个结构体类型struct Student
{long int num;
char name[20];
char sex;
};
struct Student a={10101,"Li Lin",'M',"123 Beijing Road"};//定义结构体变量a并初始化
return 0;
}

NO.:10101
name:Li Lin
sex:M

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

#include<stdio.h>
#include<stdlib.h>
int main()
{struct Student//声明结构体类型
{int num;
char name[20];
float score;
};
struct Student student1={32,"weng",89};
struct Student student2={33,"ling",78};//定义两个结构体变量 ，输入两学生的数据
if(student1.score>student2.score)//使用if 语句判断
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;
}

32,weng,89.000000
--------------------------------
Process exited after 0.3633 seconds with return value 0

#include<stdio.h> #include<string.h>struct Person//声明结构体类型{char name[20]; int count;}leader[3]={"Li",0,"Zhang",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
Zhang
Li
Li
Sun
Zhang
Zhang
Zhang
Sun
Li

Result:
Li:4
Zhang:4
Sun:2

--------------------------------
Process exited after 59.23 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;  //定义结构体变量temp，用作交换时的临时变量
const int n=5;  //定义常变量n；
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     Sun100.00
10103    Wang 98.50
10106      Li 86.00
10101   Zhang 78.00
10108    Ling 73.50

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

#include<stdio.h>
#include<string.h>
int 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.5;
printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);  //输出结果
printf("\n");
printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",(*p).num,(*p).name,(*p).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.07086 seconds with return value 0

#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;p<stu+3;p++)
printf("%5d%-20s%2c%4d\n",p->num,p->name,p->sex,p->age);//输出结果
return 0;
}

No.    Name                sex age
10101Li Lin               M  18
10102Zhang Fang           M  19
10104Wang Min             F  20

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

#include<stdio.h>
#define N 3 //学生数为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 stud);
struct Student stu[N],*p=stu;  //定义结构体数组和指针
input(p);  //调用input函数
print(max(p));  //调用printf函数
return 0;
}
void input(struct Student stu[])//定义input函数
{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[])//定义max函数
{
int i,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函数
{printf("\n成绩最高的学生是:\n");
printf("学号:%d\n姓名：%s\n三门成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}

32 weng 65 64 34
33 guo 67 32 80
34 xie 78 97 75

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

posted on 2017-04-11 17:32  郭欣宇  阅读(122)  评论(0编辑  收藏  举报