一本图书的信息包括:图书编号(BookNo),书名(BookName),总册数(Total),借出数(Lend),价格(Value),利用结构体实现以下要求:
输入这5本图书信息;输出5本图书信息及每本图书剩余在库的册数,要求:按照在库册数从多到少输出,排序可选择冒泡、选择、快速排序等方式;实现一个查找函数ValueSearch(),按照图书名查找图书库中的图书价格,如果找到,则返回该图书的价格,如果没有找到,则输出“在书库中未找到”,返回0;利用查找函数ValueSearch()查找“大学计算机”和“大学物理”两本图书,并输出函数返回的相应的结果。
#include<iostream>#include<string>#include<iomanip>using namespace std;const int N=5;const int L=55;class BOOKS{public:int BookNo,Total,Lend,Surplus;float Value;string BookName;BOOKS Input(){BOOKS B;cin >> B.BookNo >> B.BookName >> B.Total >> B.Lend >> B.Value;B.Surplus=B.Total-B.Lend;return B;}void Show() {cout << BookNo << "\t" << BookName << "\t" << Total << "\t" << Lend << "\t" << Surplus << "\t" << Value << endl;}void Show1() {cout << BookNo << "\t" << BookName << "\t" << Surplus << endl;}};void ValueSearch(BOOKS B[N]) {int i, j;string bookname;cout << "请输入图书名:" ;cin >> bookname;for (j = 0, i = 0; i < N; i++) {if (B[i].BookName == bookname) {cout << "图书的价格为:" << B[i].Value << endl;j++;}}if (j == 0) {cout << "在图书系统中未找到!!!" << endl; }return ;}int main() {BOOKS books[N];BOOKS book;int i = 0;cout << "请输入5本图书信息:" << endl;cout << setfill('-') << setw(L) << "\n";for (i = 0; i < N; i++) {books[i]=book.Input();}cout << "\n图书信息显示:" << endl;cout << setfill('-') << setw(L) << "\n";cout << "编号\t书名\t\t总册数\t借出数\t剩余数\t价格" << endl;cout << setfill('-') << setw(L) << "\n";for (i = 0; i < N; i++) {books[i].Show();}cout << endl << "按照在库册数从多到少为:" <<endl;cout << "编号\t书名\t\t剩余数" << endl;cout << setfill('-') << setw(L) << "\n";for (i = 0; i < N; i++) {for (int j = 0; j + i + 1 < N; j++) {if (books[j].Surplus < books[j + 1].Surplus) {BOOKS temp = books[j];books[j] = books[j + 1];books[j + 1] = temp;}}}for (i = 0; i < N; i++) {books[i].Show1();}cout << setfill('-') << setw(L) << "\n";ValueSearch(books);cout << endl;return 0;}
一本图书的信息包括:图书编号(BookNo) 书名(BookName) 总册数(Total) 借出数(Lend) 价格(Value) 利用结构体实现以下要求:
如果觉得《一本图书的信息包括:图书编号(BookNo) 书名(BookName) 总册数(Total) 借出》对你有帮助,请点赞、收藏,并留下你的观点哦!