图书信息管理及排序

#include <stdio.h>
#define N 10 

struct date {
	int year;
	int month;
	int day;
};

struct information {
	long number;
	char name[100];
	char author[100];
	char press[100];
	float price;
	struct date pressdate;
}book[N];

void read()
{
	int i;
	for (i = 0;i < N;i++) {
		printf("请输入第%d本书的信息:\n", i+1);
		scanf("%ld%s%s%s%f%d%d%d", \
			&book[i].number, book[i].name, book[i].author, book[i].press, &book[i].price,\
			&book[i].pressdate.year, &book[i].pressdate.month, &book[i].pressdate.day);
	}
}

void print()
{
	int i;
	struct information *p = book;
	for (i = 0;i < N;i++) {
		printf("number:%ld\nname:%s\nauthor:%s\npress:%s\nprice:%f\ndate:%d/%d/%d\n", \
			p[i].number, p[i].name, p[i].author, p[i].press, p[i].price, \
			p[i].pressdate.month, p[i].pressdate.day, p[i].pressdate.year);
	}
}

void swap(struct information *a, struct information *b)
{
	struct information temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

void sort()
{
	int i, j, k;
	for (i = 0;i < N - 1;i++) {
		k = i;
		for (j = i + 1;j < N;j++) {
			if (book[k].number > book[j].number) {
				k = j;
			}
		}
		if (k != i) {
			swap(book + k, book + j);
		}
	}
}

int main(void)
{
	read();
	sort();
	print();

	return 0;
}

void price_average()
{
	int i;
	float sum = 0, average;
	for (i = 0;i < N;i++) {
		sum += book[i].price;
	}
	average = sum / N;
	printf("The average price:%f\n", average);
}
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值