第九章 (答案仅供参考),有问题大家可以在评论区一起讨论。
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");
}
}
可以收藏方便后续观看查找。