关于输入书本的问题

问题描述:有5本书,每本书包含书号,书名,作者,价格,出版社的信息,编写函数:1.输出5本书信息。2.输出价格最高的那本书的信息。3.输出价格最低的那本书信息。在主函数中使用这3个函数

问题连接:有5本书,每本书包含书号,书名,作者,价格,出版社的信息,编写函数:1.输出5本书信息。2.输出价格最高的那本书的信息。3.输出价格最低的那本书信息。在主函数中使用这3个函数-CSDN社区

我的回复:

#include <stdio.h>
// #include <string.h>

struct MyBook {
	char number[20]; // 书号
	char title[100]; // 书名
	char author[20]; // 作者
	double price; // 单价
	char publisher[100]; // 出版社

};

#define MAX_BOOKS 1 // 书的数量

MyBook myBooks[MAX_BOOKS]; // 书本列表
int HighestIndex = 0; // 最高价书的索引
int LowestIndex = 0; // 最低价书的索引

void inputData(); // 输入资料
void printData(int index); // 打印资料

int main() {

	inputData();
	printf("\n\n列印所有书本的资料:\n");
	printData(-1);
	printf("\n\n列印最高价的书本资料:\n");
	printData(HighestIndex);
	printf("\n\n列印最低价的书本资料:\n");
	printData(LowestIndex);

	return 0;
}

// 输入资料
void inputData() {
	double highestPrice = 0.00; // 最高价
	double lowestPrice = 999999.00; // 最低价
	for (int index = 0; index < MAX_BOOKS; index++) {
		// 如果严谨点,应该检测书号是否重复以及单价不能少于等于0
		printf("--- 请输入第%d本书的资料 ---", index + 1);
		printf("\n\t书号:");
		scanf("%s", myBooks[index].number);
		printf("\t书名:");
		scanf("%s", myBooks[index].title);
		printf("\t作者:");
		scanf("%s", myBooks[index].author);
		printf("\t单价:");
		scanf("%lf", &myBooks[index].price);
		printf("\t出版社:");
		scanf("%s", myBooks[index].publisher);
		// 比较最高价
		if (myBooks[index].price > highestPrice) {
			highestPrice = myBooks[index].price;
			HighestIndex = index;
		}
		// 比较最低价
		if (myBooks[index].price < lowestPrice) {
			lowestPrice = myBooks[index].price;
			LowestIndex = index;
		}
	}
}

// 打印资料
void printData(int index) {
	int startNumber = 0;
	int endNumber = MAX_BOOKS;
	if (index != -1) {
		startNumber = index;
		endNumber = index + 1;
	}
	printf("================================================================\n");
	for (int i = startNumber; i < endNumber; i++) {
		printf("书  号:%s\n", myBooks[i].number);
		printf("书  名:%s\n", myBooks[i].title);
		printf("作  者:%s\n", myBooks[i].author);
		printf("单  价:%6.2lf\n", myBooks[i].price);
		printf("出版社:%s\n", myBooks[i].publisher);
		printf("----------------------------------------------------------------\n");
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值