图书信息管理及排序

#include 
#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);
}

你可能感兴趣的:(c语言,assignments)