余贞侠C语言程序设计课后参考答案

本篇博客涵盖了C语言编程的多个方面,包括计算日期在当年的位置(考虑闰年),实现学生信息的录入、不及格名单统计及成绩排序,以及通过结构体数组构建链表并进行链表操作,如查找和替换学生成绩。代码展示了基本的数据结构和算法应用。
摘要由CSDN通过智能技术生成

第九章 (答案仅供参考),有问题大家可以在评论区一起讨论。


9.1//定义一个结构体变量,包括年、月、日数据。计算该日在本年是第几天,注意闰年问题;
#include<stdio.h>
struct Data
{
	int year;
	int month;
	int day;
};
int main()
{
	struct Data a;
	printf("please input year/month/day:");
	scanf("%d/%d/%d",&a.year,&a.month,&a.day);
	int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
	int sum=a.day,i;//a.day表示本月的天数,所以在后边要算总月数时要-1;
	if(a.year%4==0&&a.year%100!=0||a.year%400==0)
	{
		m[1]++;
	}
	for(i=0;i<a.month-1;i++)//a.month表示的是输入的月份,
	{
		sum+=m[i];//计算当天在一年中的总天数;
	}
	printf("%d/%d/%d is this year %d day\n",a.year,a.month,a.day,sum);
	return 0;
}
9.2//做一个针对10个学生的简易成绩管理系统。学生信息包括学号、姓名、年龄、三门课成绩。
//功能包括统计不及格的名单并显示,对平时成绩进行从高到底的排序。
#include<stdio.h>
#include<stdlib.h>
#define N 3
struct STU
{
	char num[11];//学号;
	char name[21];//姓名;
	int age;//年龄;
	float score[3];//三门课成绩;
};
void PrintMenu();
void Input();
void fun1();//统计不及格名单;
void swap(int i,int j);//对成绩进行由高到底的排序;
void fun2();//对成绩进行由高到低的排序;并输出;
int flag=0;
struct STU stu[N];
int main()
{	
	while(1)
	{
		PrintMenu();//主菜单;
		int choose;
		scanf("%d",&choose);
		if(!choose)//相当于choose==0;
		{
			printf("感谢使用,欢迎下次使用,再见!");
			exit(0);//退出程序;
		}
		else if(choose==1)
		{
			Input();
		}
		else if(choose==2)
		{
			fun1();
		}
		else if(choose==3)
		{
			fun2();
		}
		else
		{
			printf("没有该选项!");
		}
	}
	return 0;
}
void PrintMenu()
{
	printf("\n欢迎使用简易学生管理系统,请对应下列序号输入你想要的功能,enter结束\n");
	printf("1 录入学生信息\n");
	printf("2 统计不及格学生信息\n");
	printf("3 平时成绩排序\n");
	printf("0 退出系统\n");
}
void Input()
{
	int i;
	for(i=0;i<N;i++)
	{
		printf("请输入第 %d/%d 个学生的信息:\n学号 姓名 年龄 语文 数学 英语\n",i+1,N);
		scanf("%s %s %d %f %f %f",stu[i].num,stu[i].name,&stu[i].age,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
	}
	flag=1;
	printf("录入成功。\n");
	if(!flag)//相当于flag==0;
	{
		printf("录入失败,请充录入.\n");
		return;
	}
}
void fun1()
{
	if(!flag)
	{
		printf("学生信息未录入,请先录入信息\n");
		return;
	}
	
	printf("不及格学生信息如下:\n");
	int i,j;
	for(i=0;i<N;i++)
	{
		int flag=0;
		for(j=0;j<3;j++)//对三门课程十分几个进行循环判断;
		{
			if(stu[i].score[j]<60)
			{
				flag=1;
				break;
			}
		}
		if(flag==1)
		{
			printf("学号 姓名 年龄 语文 数学 英语\n %6s%6s%6d%6.1f%6.1f%6.1f\n",stu[i].num,stu[i].name,stu[i].age,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
		}
	}
}
void fun2()
{
	if(!flag)//先判断有无录入信息;
	{
		printf("学生信息未录入,请先录入信息\n");
		return;
	}
	int i,j;
	for(i=0;i<N;i++)//比较排序;
		for(j=i+1;j<N;j++)
		{
			//for(a=0;a<3;a++)
			//{
			    if(stu[i].score[0]<stu[j].score[0])
				{
					swap(i,j);
				}
				else if(stu[i].score[0]=stu[j].score[0]&&stu[i].score[1]<stu[i].score[1])
				{
					swap(i,j);
				}
				else
				{
					swap(i,j);
				}
		}
		for(i=0;i<N;i++)
		{
			printf("学号 姓名 年龄 语文 数学 英语\n %6s%6s%6d%6.1f%6.1f%6.1f\n",stu[i].num,stu[i].name,stu[i].age,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
		}
}
void swap(int i,int j)
{
	struct STU t=stu[i];
	stu[i]=stu[j];
	stu[j]=t;
}







9.3//有十个学生的信息,包括学号,姓名,年龄,组成结构体数组。将该数组的10个学生数据读出形成链表;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 2

struct STU
{
	char num[11];//学号
	char name[21];//姓名
	int age;//年龄
	struct STU *next;//链表指针域
}stu[N];
int main()
{
	int i;
	struct STU *p,*head;
	for(i=0;i<N;i++)
	{
		printf("please input %d student information:",i+1);
		scanf("%s %s %d",stu[i].num,stu[i].name,&stu[i].age);
	}
	head=&stu[0];//创建链表,头指针指向结构体变量的第一个元素;
	for(i=1;i<N;i++)
	{
		stu[i-1].next=&stu[i];//前一个结点指针域指向后一个指针(指针记得加地址);
	}
	p=head;//移动指针指向头指针所指的结点;
	while(p)//当链表存在时进行循环输出
	{
		printf("%s %s %d\n",p->num,p->name,p->age);
		p=p->next;//移动指针指向它的下一个指针;
	}
	return 0;
}


		



9.4//给定一个链表,每个链表中的结点包括学号,成绩,在其中查找某个学号的学生结点,将成绩替换成指定新成绩;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct STU
{
	char num[11];//学号
	float score;//成绩
	struct STU *next;//链表指针域;
};
struct STU *Create(int n);//创建链表存放学生信息;
void print(struct STU *head);//输出;
void find(struct STU *head,struct STU stu);//查找某个学号的学生结点;
int main()
{
	struct STU *head=NULL,stu;
	int n;
	printf("please input student number:");
	scanf("%d",&n);
//	char a[11];
	head=Create(n);//创建链表存放学生信息
    printf("学号 成绩\n");
	print(head);
	printf("please input you want change num:");
	scanf("%s %f",stu.num,&stu.score);
	find(head,stu);
	printf("修改后的信息\n");
	print(head);
	return 0;
}
struct STU *Create(int n)
{
	int i=0;
	struct STU *head=NULL,*p1,*p2;
	while(i<n)
	{
		p1=(struct STU *)malloc(sizeof(struct STU));
		p1->next=NULL;//为链表开辟空间;最后一个结点指向空;
		printf("请输入第%d个学生的信息:",i+1);
		scanf("%s %f",p1->num,&p1->score);
		if(head==NULL)//链表为空的情况
		{
			head=p1;//产生第一个结点;
			p2=p1;//p2指向当前链表的尾结点;
		}
		else
		{
			p2->next=p1;//p2的指针域指向p1,链接到尾结点p2后;
			p2=p1;//p2指向新的尾结点;
		}
		i++;
	}
	return head;
}
void print(struct STU *head)
{
	struct STU *p;
	p=head;
	while(p!=NULL)//当链表存在时,输出;
	{
		printf("%s %.2f\n",p->num,p->score);
		p=p->next;//下一个结点;
	}
}
void find(struct STU *head,struct STU stu)
{
	struct STU *p;
	p=head;
	while(p!=NULL)//链表存在
	{
		//printf("%s %f\n",p->num,p->score);
	//	printf("%s %f\n",stu.num,stu.score);
		if(strcmp(p->num,stu.num)==0)
		{
			//strcpy(p->num,stu.num);//将要修改的学号复制到指针p所指的结点中;
			p->score=stu.score;
			printf("%s %f\n",p->num,p->score);
		}
		p=p->next;
	}
	if(p==NULL)
	{
		printf("无该学号\n");
	}

}
			




可以收藏方便后续观看查找。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野性的鬼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值