//例题9.1//
#include<stdio.h>
int main()
{struct Student
{long int num; //定义长整形num//
char name[20]; //定义字符数组name,记住:当没有初始化时,字符数组中数不能空//
char sex;
char addr[20];
}a={10101,"Li Lin",'M',"123 Beijing Road"}; //给定义的数都初始化,注意:字符初始化要有单引号,字符串初始化要有双引号//
printf("NO.:%ld
name:%s
sex:%c
address:%s
",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.4356 seconds with return value 0 请按任意键继续. . .
//例题9.2//
#include<stdio.h>
int main()
{struct student
{ int num;
char name[20];
float score;
}student1,student2;
scanf("%d%s%f",&student1.num,&student1.name,&student1.score); //输入学生1,2的各项值,再输入中一定得加&//
scanf("%d%s%f",&student2.num,&student2.name,&student2.score);
printf("The higher score is:
");
if(student1.score>student2.score) //用双重if语句来判断学生们的大小比较//
printf("%d %s %6.2f
",student1.num,student1.name,student1.score);
else if(student1.score<student2.score)
printf("%d %s %6.2f
",student2.num,student2.name,student2.score);
else
{ printf("%d %s %6.2f
",student1.num,student1.name,student1.score);
printf("%d %s %6.2f
",student2.num,student2.name,student2.score);
}
return 0;
}
10101 ww 88.5 10103 qq 44.1 The higher score is: 10101 ww 88.50 -------------------------------- Process exited after 16.54 seconds with return value 0 请按任意键继续. . .
//例题9.3//
#include<string.h>
#include<stdio.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++) //用包含for循环选出投票数//
if(strcmp(leader_name,leader[j].name)==0)leader[j].count++;
}
printf("
Result:
");
for(i=0;i<3;i++)
printf("%5s:%d
",leader[i].name,leader[i].count);
return 0;
}
Li Zhang Li Li Sun Zhang Zhang Zhang Sun Li Result: Li:4 Zhang:4 Sun:2 -------------------------------- Process exited after 62.49 seconds with return value 0 请按任意键继续. . .
//例题9.4//
#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:
");
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
",stu[i].num,stu[i].name,stu[i].score);
printf("
");
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.1495 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;
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.:%ld
name:%s
sex:%c
score:%5.1f
",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("
");
printf("No.:%ld
name:%s
sex:%c
score:%5.1f
",(*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.1335 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
");
for(p=stu;p<stu+3;p++)
printf("%5d %-20s %2c %4d
",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.1049 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 stud);
struct Student stu[N],*p=stu;
input(p);
print(max(p));
return 0;
}
void input(struct Student stu[])
{int i;
printf("请输入各学生的信息:学号、姓名、三门课成绩:
");
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,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("学号:%d
姓名:%s
三门成绩:%5.1f,%5.1f,%5.1f
平均成绩:%6.2f
",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}
请输入各学生的信息:学号、姓名、三门课成绩: 22 ww 44 77 88 33 qq 44 77 55 44 ee 66 33 11 成绩最高的学生是: 学号:22 姓名:ww 三门成绩: 44.0, 77.0, 88.0 平均成绩: 69.67 -------------------------------- Process exited after 24.56 seconds with return value 0 请按任意键继续. . .
总结:C语言是极易弄错误的一门课,因此需要我们细心查看,要注意标点括号,要查阅出错时程序给的错误提示,并思考出现这种错误的原因,然后逐 层修改。