/* 创建时间:20180101
创建人:fangweijun([email protected])
功能:通过指向结构体变量的指针变量输出结构体变量中成员的信息
位置:C程序设计(第四版)2010年6月第四版 303页例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\nname:%s\nsex:%c\nscore:%5.1f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("\nNo.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",(* p).num,(* p).name,(* p).sex,(* p).score);
return 0;
}
/*Dev-c++输出结果:
NO.:10101
name:Li Lin
sex:M
score: 89.5
No.:10101
name:Li Lin
sex:M
score: 89.5
*/
/* 创建时间:20180101
创建人:fangweijun([email protected])
功能:有3个学生的信息,放在结构数组中,要求输出全部学生的信息
位置:C程序设计(第四版)2010年6月第四版 304页例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\n");
for(p=stu;p<stu+3;p++)
printf("%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age);
return 0;
}
/*Dev-c++输出结果:
No.Name sex age
10101 Li Lin M 18
10102 Zhang Fang M 19
10104 Wang Min F 20
*/
/* 创建时间:20180101
创建人:fangweijun([email protected])
功能:有n个结构体变量,内含徐盛学号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息(包括学号、姓名、3门课程成绩和平均成绩)
分别用三个函数来实现不同的功能:
1.用input函数来输入数据和求各学生平均成绩
2.用max函数来找平均成绩最高的学生
3.用print函数来输出成绩最高学生的信息
位置:C程序设计(第四版)2010年6月第四版 306页例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,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姓名:%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);
}
/*Dev-c++输出结果:
请输入各学生的信息:学号、姓名、三门课成绩:
10101 Li 78 89 98
10103 Wang 98.5 87 69
10106 Sun 88 76 89
成绩最高的学生是:
学号:10101
姓名:Li
三门课成绩: 78.0, 89.0, 98.0
平均成绩: 88.33
*/
/* 创建时间:20180101
创建人:fangweijun([email protected])
功能:建立一个简单链表如图9.9所示,它由3个学生数据的结点组成,要求输出各结点中的数据(静态链表)
位置:C程序设计(第四版)2010年6月第四版 310页例9.8
*/
#include<stdio.h>
struct Student
{
int num;
float score;
struct Student * next;
};
int main()
{
//静态链表
struct Student a,b,c,* head,* p;//定义struct Student类型
a.num=10101;
a.score=89.5;
b.num=10103;
b.score=90;
c.num=10107;
c.score=85;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
return 0;
}
/*Dev-c++输出结果:
10101 89.5
10103 90.0
10107 85.0
*/
/* 创建时间:20180101
创建人:fangweijun([email protected])
功能:写一个函数建立一个有3名学生数据的单向动态链表(动态链表)
位置:C程序设计(第四版)2010年6月第四版 311页例9.9
*/
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student * next;
};
int n;
//动态链表
struct Student * creat(void)
{
struct Student * head;
struct Student * p1,* p2;
n=0;//结点数
p1=p2=(struct Student *)malloc(LEN);//malloc()返回的是void *类型,可以显式强制转换,系统也可以隐式转换
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;//无链表
while(p1->num!=0)//规定num不能为0,只能用来结束链表创建
{
n=n+1;
C程序设计 第四版(谭浩强)-学习笔记-第十二天
最新推荐文章于 2023-08-02 20:32:46 发布