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

aaaaalyr 2021-12-16 21:43:30

怎么打

 

...全文
419 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
疯狂刷题中 2021-12-17
  • 打赏
  • 举报
回复 1

同学,请参考:

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


70,017

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧