第六次课内实验

第六次课内实验

1.有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输出学生的信息。要求:输入、输出不使用函数。

#include <stdio.h>
//有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输出学生的信息。要求:输入、输出不使用函数。
struct Student
{
	char Num[10];
	char Name[10];
	int score[3];
	float aver;
};//创建结构体,学号、姓名、3门课的成绩、平均成绩
void main()
{
	struct Student stu[6];
	int i;
	printf("请依次输入六个同学的学号、姓名、成绩:\n");
	for(i=0;i<6;i++)//input利用for循环输入数据
	{
		scanf("%s%s%d%d%d",stu[i].Num,stu[i].Name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;//通过计算得出平均分
	} 
	printf("输出的六个同学的学号、姓名、成绩和平均成绩为:\n");
	for(i=0;i<6;i++)//output利用for循环输出数据
	{
		printf("%s %s %d %d %d 平均成绩=%.1f\n",stu[i].Num,stu[i].Name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver);
	}
}

2.有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输出学生的信息。要求:输入、输出使用函数来实现。

#include <stdio.h>
//有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输出学生的信息。要求:输入、输出使用函数来实现。
struct Student
{
	char Num[10];
	char Name[10];
	int score[3];
	float aver;
};//创建结构体,学号、姓名、3门课的成绩、平均成绩
void main()
{
	struct Student stu[6];
	void input(struct Student stu[],int n);
	void output(struct Student stu[],int n);
	printf("请依次输入六个同学的学号、姓名、成绩:\n");
	input(stu,6);//input利用for循环输入数据
	printf("输出的六个同学的学号、姓名、成绩和平均成绩为:\n");
	output(stu,6);//output利用for循环输出数据

}
void input(struct Student stu[],int n)//input利用for循环输入数据
{
	int i;
	for(i=0;i<6;i++)
	{
		scanf("%s%s%d%d%d",stu[i].Num,stu[i].Name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;//通过计算得出平均分
	} 
}
void output(struct Student stu[],int n)//output利用for循环输出数据
{
	int i;
	for(i=0;i<6;i++)
	{
		printf("%s %s %d %d %d 平均成绩=%.1f\n",stu[i].Num,stu[i].Name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver);
	}
}

3.有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输入学生的姓名,查找并输出学生的信息,查找不成功输出“没有此学生”。要求:输入、查找使用函数来实现。

#include <stdio.h>
#include<string.h>
//有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出
//输入学生的姓名,查找并输出学生的信息,查找不成功输出“没有此学生”。要求:输入、查找使用函数来实现。
struct Student
{
	char Num[10];
	char Name[10];
	int score[3];
	float aver;
};//创建结构体,学号、姓名、3门课的成绩、平均成绩
void main()
{
	struct Student stu[6];
	void input(struct Student stu[],int n);
	void output(struct Student stu[],int n);
	void search(struct Student stu[],int n);
	printf("请依次输入六个同学的学号、姓名、成绩:\n");
	input(stu,6);//input利用for循环输入数据
	printf("输出的六个同学的学号、姓名、成绩和平均成绩为:\n");
	output(stu,6);//output利用for循环输出数据
	search(stu,6);
}
void input(struct Student stu[],int n)//input利用for循环输入数据
{
	int i;
	for(i=0;i<6;i++)
	{
		scanf("%s%s%d%d%d",stu[i].Num,stu[i].Name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;//通过计算得出平均分
	} 
}
void output(struct Student stu[],int n)//output利用for循环输出数据
{
	int i;
	for(i=0;i<6;i++)
	{
		printf("%s %s %d %d %d 平均成绩=%.1f\n",stu[i].Num,stu[i].Name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver);
	}
}
void search(struct Student stu[],int n)//search输入学生的姓名,查找并输出学生的信息,查找不成功输出“没有此学生”。要求:输入、查找使用函数来实现
{
	char name[10];
	int i,m=1;
	printf("输入需要查找的学生姓名\n");
	scanf("%s",&name);
	for(i=0;i<n;i++)
	{
		if(strcmp(name,stu[i].Name)==0)//strcmp函数判断字符串是否相同
		{
			printf("%s %s %d %d %d 平均成绩=%.1f\n",stu[i].Num,stu[i].Name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver);
			m=0;
		}

	}
	if(m)
	{
		printf("没有此学生\n");
	}
}

4.有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输出学生的信息。要求:输入、输出使用函数来实现,学生数据用单链表存储。

#include <stdio.h>
//有6个学生,每个学生的数据包括学号、姓名、3门课的成绩、平均成绩,输入每个学生的信息,平均成绩需通过计算得出,输出学生的信息。
//要求:输入、输出使用函数来实现,学生数据用单链表存储。
struct Student
{
	char Num[10];
	char Name[10];
	int score[3];
	float aver;
	struct Student *next;
};//创建结构体,学号、姓名、3门课的成绩、平均成绩并创建链表
void main()
{
	struct Student stu[6];
	void input(struct Student stu[],int n);
	void output(struct Student stu[],int n);
	printf("请依次输入六个同学的学号、姓名、成绩:\n");
	input(stu,6);//input利用for循环和链表输入数据
	printf("输出的六个同学的学号、姓名、成绩和平均成绩为:\n");
	output(stu,6);//output利用for循环和链表输出数据
}
void input(struct Student stu[],int n)//input利用for循环和链表输入数据
{
	int i;
	for(i=0;i<5;i++)
	{
		scanf("%s%s%d%d%d",stu[i].Num,stu[i].Name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;//通过计算得出平均分
		stu[i].next=&stu[i+1];
	} 
	scanf("%s%s%d%d%d",stu[i].Num,stu[i].Name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
	stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
	stu[i].next=NULL;//创建链表
}
void output(struct Student stu[],int n)//output利用for循环和链表输出数据
{
	int i;
	struct Student *head=&stu[0],*p;
	p=head;
	while(p!=NULL)
	{
		printf("%s %s %d %d %d 平均成绩=%.1f\n",p->Num,p->Name,p->score[0],p->score[1],p->score[2],p->aver);
		p=p->next;
	}
}

结果:

请依次输入六个同学的学号、姓名、成绩:
001 hu 12 12 12
002 gu 13 13 13
003 wa 14 14 14
004 li 15 15 15
005 zh 16 16 16
006 hr 17 17 17
输出的六个同学的学号、姓名、成绩和平均成绩为:
001 hu 12 12 12 平均成绩=12.0
002 gu 13 13 13 平均成绩=13.0
003 wa 14 14 14 平均成绩=14.0
004 li 15 15 15 平均成绩=15.0
005 zh 16 16 16 平均成绩=16.0
006 hr 17 17 17 平均成绩=17.0
请按任意键继续. . .

5.选做题:编写程序逆向创建一个单链表,即从后向前创建并连接单链表的结点。

#include <stdio.h>
#include <malloc.h>
//选做题:编写程序逆向创建一个单链表,即从后向前创建并连接单链表的结点。
struct student//定义链表
{
	long num;
	float score;
	struct student *next;//创建指针
};
struct student *creat()//逆向创建动态链表
{ 
	struct student *head=NULL,*p1, *p2;
	int n=0;
	p1=p2=(struct student*)malloc(sizeof (struct student));//构建动态变量
	scanf("%ld%f",&p1->num,&p1->score);
	while(p1->num!=0)
	{
		n=n+1;
		if (n==1) head=p1;
		else p1->next=p2;//将后一个结构体的指针指向前一个结构体
		p2=p1;
		p1=(struct student *) malloc(sizeof (struct student));
		scanf("%ld%f",&p1->num,&p1->score);//在while循环中逐个逆向输入数据
	}
	head=p2;
	return (head);
}
void main()
{
	struct student *creat();
	struct student *head,*p;
	int i;
	head=creat();
	p=head;
	printf("输出未插入新节点前链表\n");
	while(p!=NULL)//输出链表
	{
		printf("num:%ld    score:%5.1f\n",p->num,p->score);
		p=p->next;
	}
}  

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值