C语言程序设计实验报告
实验七、结构体程序设计
班级 211 学号 201421125 姓名 秦兰兰
【实验目的】
(1)掌握结构体类型的概念、定义和使用;
(2)掌握结构体数组、结构体指针的定义和使用;
【实验内容及步骤】
1、输入5位同学的一组信息,包括学号、姓名、数学成绩、计算机成绩,求得每位同学的
平均分和总分,然后按照总分从高到低排序。 【程序代码】:
#include
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
{ 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 #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
相关推荐: