第一范文网 - 专业文章范例文档资料分享平台

实验7结构体程序设计

来源:用户分享 时间:2025/10/29 1:23:26 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

C语言程序设计实验报告

实验七、结构体程序设计

班级 211 学号 201421125 姓名 秦兰兰

【实验目的】

(1)掌握结构体类型的概念、定义和使用;

(2)掌握结构体数组、结构体指针的定义和使用;

【实验内容及步骤】

1、输入5位同学的一组信息,包括学号、姓名、数学成绩、计算机成绩,求得每位同学的

平均分和总分,然后按照总分从高到低排序。 【程序代码】:

#include struct student {int num;

char name[20];

double math,com,score,average; }st[5]; main()

{ int i,j;

struct student t;

printf(\请输入五位同学的信息:学号,姓名,数学,计算机\\n\for(i=0;i<5;i++)

{ scanf(\st[i].score=st[i].math+st[i].com; st[i].average=st[i].score/2; }

for(j=0;j<5;j++)//成绩的排序 for(i=j+1;i<5;i++)

if(st[i].score>st[j].score) {t=st[i];st[i]=st[j];st[j]=t;} printf(\每位同学的排名\\n\

printf(\名次\\t学号\\t姓名\\t总分\\t平均分\\n\for(i=0;i<5;i++)

printf(\

i+1,st[i].num,st[i].name,st[i].score,st[i].average); printf(\}

【运行结果】

1

2.定义一个结构体变量(包括年、月、日)。编写一个函数days,计算该日期在本年中是第几天(注意闰年问题)。由主函数将年月日传递给days函数,计算之后,将结果传回到主函数输出。 【程序代码】

#include struct date

{ int year,month,day;}dy; main()

{int days(struct date dy);//函数的声明 printf(\请输入日期:\\n\

scanf(\printf(\该日期是本年的第%d天:\\n\}

int days(struct date dy)

{ int run(int year); //函数的声明

int sum,i, count[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for(i=1,sum=dy.day;i

if(run(dy.year)==1&&dy.month>=3) sum++; return sum; }

int run(int year)//判断闰年的函数

{if(year%4==0&&year0!=0||year@0==0) return 1;

else return 0; }

【运行结果】

2

3.p330 12题

#include #include

#define LEN sizeof(struct student) struct student {int num;

char name[20]; char sex[10]; int age;

struct student * next; };

int n=0;

struct student * head=NULL;

struct student * creat(void)//定义函数创建动态链表 { struct student * p1; struct student * p2;

p1=p2=(struct student *)malloc(LEN);

scanf(\while(p1->num!=0) {n=n+1;

if(n==1)head=p1; else p2->next=p1; p2=p1;

p1=(struct student *)malloc(LEN);

scanf(\}

p2->next=NULL; return head; }

struct student * del( void)//删除节点的函数 { int a; struct student *p1; struct student *p2;

3

struct student *p; printf(\请输入学生信息:\

p=creat();

printf(\每位学生信息为:\\n\while(p!=NULL)

{printf(\p=p->next;}

printf(\要删除的学生年龄:\scanf(\if(head==NULL)

printf(\没有信息!\p1=head;

while(p1->next!=NULL&&p1->age!=a) { p2=p1;

p1=p1->next;} if(p1->age==a) {

if(p1=head)head=p1->next; else p2->next=p1->next; free(p1); }

else printf(\没有符合条件的学生!\\n\return head; }

main() {

struct student *p; p=del();

printf(\删除该学生之后的学生为:\\n\while(p!=NULL)

{printf(\p=p->next;} }

运行结果:

4

5

搜索更多关于: 实验7结构体程序设计 的文档
实验7结构体程序设计.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2bwtr0phol1qw0a8dn3s_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top