【C语言】图书登记及学生信息登记作业代码

有一批图书,每本图书要登记作者姓名、书名、出版社、出版年月、价格等信息,试编写一个程序完成下列任务:

⑴ 读入每本书的信息存入数组中。
⑵ 输出价格在19.80元以下的书名及出版社名。
⑶ 输出2006年以后出版的图书具体信息。
#include<stdio.h>
#define N 99
struct date				//出版日期
{
	int year;
	int month;
	int day;
};
struct xinxi
{
	char author[N];		//作者
	char bookname[N];	//书名
	char publisher[N];	//出版社
	struct date publish_date;		//结构体嵌套
	float price;		//价格
}xx[N];
void main()
{
	int n;
	printf("要登记书的数量:");
	scanf("%d",&n);
	int i,j;
	struct xinxi t;
	printf("分别输入%d本书的作者、书名、出版社、出版年月、价格:\n",n);
	for(i=0; i<n; i++)
	{
		scanf("%s %s %s %d-%d-%d %f",xx[i].author,xx[i].bookname,xx[i].publisher,
				&xx[i].publish_date.year,&xx[i].publish_date.month,&xx[i].publish_date.day,&xx[i].price);

	}
	for(i=0; i<n; i++)
	{
		if(xx[i].price < 19.80)
			printf("价格在19.80元以下的书名及出版社名:%s  %s\n",xx[i].bookname,xx[i].publisher);
		if(xx[i].publish_date.year > 2006)
			printf("2006年以后出版的图书具体信息:%s %s %s %d-%d-%d %.2f\n",xx[i].author,xx[i].bookname,xx[i].publisher,
				xx[i].publish_date.year,xx[i].publish_date.month,xx[i].publish_date.day,xx[i].price);
	}
	
}

有8名学生,每个学生包括学号、姓名和成绩,要求按成绩递增排序并输出。要求如下:

⑴ 学生信息的输入和输出在主函数内实现。
⑵ 按成绩递增排序在sort函数中实现。
#include <stdio.h>
#include <stdlib.h>
#define N 8
struct Student{                                    //定义结构体类型Student
	int num;
	char name[20];
	float score;
};
void input(Student *st, int n);                    //定义输入函数
void print(Student *st, int n);                    //定义输出函数
void sort(Student *st, int n);                     //定义排序函数
int main()
{
	Student *stu;                                  //定义结构体指针stu
	int n;
	stu=(Student*)malloc(N*sizeof(Student));       //为stu分配内存空间
	input(stu, N);                                 //调用输入函数
	sort(stu, N);                                  //调用排序函数
	print(stu, N);                                 //调用输出函数
	system("pause");
	return 0;
}
void input(Student *st, int n)				//输入函数
{
	int i;
	for (i=0; i<n; i++){
		printf("请输入第%d个学生的信息(学号,姓名,成绩):\n ", i+1);
		scanf("%d %s %f", &st[i].num, st[i].name, &st[i].score);
	}
}
void print(Student *st, int n)				//输出函数
{
	Student *p;
	for (p=st, printf("Sort by:\n"); p<st+n; printf("%d %-7s %.2f\n", p->num, p->name, p->score), p++);
}
void sort(Student *st, int n)				//排序函数
{
	int i, j;
	Student temp;
	for (i=0; i<n; i++)
		for (j=i+1; j<n; j++)
			if (st[i].score>st[j].score)
			{
				temp=st[i], st[i]=st[j], st[j]=temp;
			}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值