教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

电1C语言实验报告(9)

来源:网络收集 时间:2026-08-03
导读: 《计算方法与程序设计实验》 main() { struct student *p; for(p=stu;p printf(\} Key: 运行结果:10101,Li Lin,M,18 10102,Zhang Fun,M,19 10104,Wang Min,F,20 分析:本程序定义了具有3个元素的全局结构体数组stu

《计算方法与程序设计实验》

main()

{ struct student *p; for(p=stu;p

printf(\}

Key: 运行结果:10101,Li Lin,M,18 10102,Zhang Fun,M,19 10104,Wang Min,F,20

分析:本程序定义了具有3个元素的全局结构体数组stu,每个元素由num,name,sex,age四 个成员组成,使用结构体指针变量控制循环,结构体指针变量引用对应的结构体成员 的形式是:指针变量->成员 (2)

#include\struct data { int a,b,c; };

void func(struct data y); main()

{ struct data x;

x.a=27; x.b=3; x.c=x.a+x.b;

printf(\printf(\…\\n\func(x);

printf(\}

void func(struct data y)

{ printf(\printf(\…\\n\

y.a=18; y.b=5; y.c=y.a*y.b;

printf(\printf(\}

Key: 运行结果:x.a=27 x.b=3 x.c=30 Call Func()…

y.a=27 y.b=3 y.c=30 Process…

y.a=18 y.b=5 y.c=90 Return...

x.a=27 x.b=3 x.c=30

分析:本程序使用结构体变量作实参调用func()函数,由于C语言中的函数调用是实参到形 参的单向传递,因此主函数调用func()函数时,虽然在func()函数中改变了形参y的 值,但返回主函数时,实参的值并不改变。另外,要改变结构体变量的值,只能逐个 改变它的成员值。引用结构体成员的形式是:结构体变量.成员。 2.程序设计

(1)有5 个学生,每个学生的数据包括学号、姓名、3 门课的成绩,从键盘输入5 个学 生数据,要求输出3 门课总平均成绩,以及最高分的学生的数据(包括学号、姓名、3 门课 的成绩、平均分数)。

要求:用一个 input 函数输入5 个学生数据;用一个output 函数输出5 个学生数据;用 一个average 函数求总平均分;用max 函数找出最高分学生数据;总平均分和最高分的学生 的数据都在主函数中输出。 【程序框架】

#include\

26

《计算方法与程序设计实验》

struct student { int num; char name[20]; float score[3]; float avg; };

/**********函数声明**********/ void input(struct student stu[5]); void output(struct student stu[5]); float average(struct student stu[5]);

struct student *max(struct student stu[5]); /**********主函数**********/ void main()

{struct student stu[5], *pmax; float avg; printf(\input(stu); /*输入学生数据*/ output(stu); /*输出学生数据*/ avg=average(stu); /*求总平均分*/

printf(\pmax=max(stu); /*查找最高分学生,用指针pmax 指向*/ printf(\

printf(\pmax->score[0], pmax->score[1], pmax->score[2], pmax->avg); /*输出最高分学生数据*/ }

/**********子函数**********/

void input(struct student stu[5]) { }

void output(struct student stu[5]) { }

float average(struct student stu[5]) /*求3 门课总平均分,并作为函数值返回*/ { }

struct student *max(struct student stu[5]) /*查找最高分学生,函数返回最高分学生的地址*/ { } Key:

/**********子函数**********/ void input(struct student stu[5]) {int i,j;

for(i=0;i<5;i++)

{ printf(\ scanf(\ getchar();

printf(\ gets(stu[i].name); for(j=0;j<3;j++)

{ printf(\ scanf(\}

27

《计算方法与程序设计实验》

void output(struct student stu[5]) {int i,j;

printf(\ for(i=0;i<5;i++)

{ printf(\ for(j=0;j<3;j++)

printf(\ printf(\

float average(struct student stu[5]) /*求3 门课总平均分,并作为函数值返回*/ {int i,j;

float aver=0; for(i=0;i<5;i++) { stu[i].avg=0; for(j=0;j<3;j++)

stu[i].avg+=stu[i].score[j]/3; aver+=stu[i].avg/5;} return aver;}

struct student *max(struct student stu[5]) /*查找最高分学生,函数返回最高分学生的地址*/ {struct student *p=stu,*q; for(q=&stu[1];qavgavg)p=q; return p;}

28

《计算方法与程序设计实验》

(2)建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。

要求:用一个create 函数创建链表,输入若干学生的信息存入链表中,以学号为0 作为结束标志;用一个output 函数输出链表中存放的学生信息;用一个delete 函数删除链表中其年龄等于输入年龄的结点。

【程序框架】

#include\#include\struct node { int num; char name[20]; char sex; int age;

struct node *next; /*next 指向下一个结点*/ }; /*链表结点类型定义*/

/**********函数声明**********/ struct node *create( );

void output(struct node *head);

struct node *delete(struct node *head, int age); /**********主函数**********/ void main()

{struct node *head; int age; printf(\head=create(); /*创建链表*/ printf(\output(head); /*输出链表*/

printf(\scanf(\

head=delete(head,age); /*删除链表中其年龄等于输入年龄age 的结点*/ printf(\output(head); /*输出删除后的链表*/ }

/**********子函数**********/

struct node *create( ) /*输入若干学生信息存入链表中,以学号为0 作为结束标志,从而 创建一个链表,函数返回链表的头指针*/ { }

void output(struct node *head) /*输出head 所指链表中的学生信息*/ { }

struct node *delete(struct node *head, int age) /*删除head 所指链表中其年龄等于给定值 age 的结点,函数返回删除后的链表头指针*/ { } Key:

/**********子函数**********/

struct node *create( ) /*输入若干学生信息存入链表中,以学号为0 作为结束标志,从而 创建一个链表,函数返回链表的头指针*/ {int num,age,n=1;char name[20],sex;

struct node *h=malloc(sizeof(struct node)),*s,*r=h; printf(\

29

《计算方法与程序设计实验》

scanf(\ while(num) { getchar();

printf(\

printf(\ printf(\ s=malloc(sizeof(struct node));

s->num=num; strcpy(s->name,name); s->sex=sex; s->age=age; r=r->next=s; n++;

printf(\ scanf(\ r->next=0; return h; }

void output(s …… 此处隐藏:2381字,全部文档内容请下载后查看。喜欢就下载吧 ……

电1C语言实验报告(9).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/595017.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)