首页 > 其他分享 >周二打卡

周二打卡

时间:2023-04-25 18:55:23浏览次数:33  
标签:cout title Book books 周二 打卡 书籍 string

题目描述:

设计一个简单的图书管理系统,需要实现以下功能:

  1. 添加书籍:输入书名、作者、出版社、出版日期等信息,添加一本新书。
  2. 删除书籍:输入书号或书名,删除一本已有的书籍。
  3. 查询书籍:输入书号或书名,查询一本已有的书籍。
  4. 显示所有书籍:按照书号排序,输出图书馆中所有的书籍。

设计思路:

为了实现这个图书管理系统,需要设计以下几个类:

  1. Book类:用于储存书籍信息,包括书名、作者、出版社、出版日期等属性,以及一些方法,如获取与设置书籍信息。
  2. Library类:用于储存所有的书籍,包括添加书籍、删除书籍、查询书籍、显示所有书籍等方法。
  3. Menu类:用于显示菜单,接收用户输入,并调用相应的Library类中的方法。

程序流程图:

Copy Code
start --> displayMenu --> getUserChoice --> performAction --> end
                        |               |
                        v               v
                    addBook          removeBook
                        |               |
                        v               v
                      search          display

代码实现:

 

#include <bits/stdc++.h>
using namespace std;
class Book {
private:
string title;
string author;
string publisher;
string publicationDate;
public:
Book(string t, string a, string p, string pd) :
title(t), author(a), publisher(p), publicationDate(pd) {}
string getTitle() { return title; }
string getAuthor() { return author; }
string getPublisher() { return publisher; }
string getPublicationDate() { return publicationDate; }
};



class Library {
private:
vector<Book> books;
public:
void addBook(Book book) {
books.push_back(book);
}
void removeBook(string bookTitle) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == bookTitle) {
books.erase(books.begin() + i);
break;
}
}
}
Book searchBook(string bookTitle) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == bookTitle) {
return books[i];
}
}
// 如果没找到书籍,则返回一个空的Book对象
return Book("", "", "", "");
}
void displayAllBooks() {
// 按照书名排序
sort(books.begin(), books.end(), [](Book a, Book b) {
return a.getTitle() < b.getTitle();
});

// 输出所有的书籍
for (Book book : books) {
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Publisher: " << book.getPublisher() << endl;
cout << "Publication Date: " << book.getPublicationDate() << endl << endl;
}
}
};



class Menu {
private:
Library library;
public:
Menu() {
library = Library();
}

void displayMenu() {
cout << "1. Add a book" << endl;
cout << "2. Remove a book" << endl;
cout << "3. Search for a book" << endl;
cout << "4. Display all books" << endl;
cout << "5. Quit" << endl << endl;
}

int getUserChoice() {
int choice;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
return choice;
}

void performAction(int choice) {
string title, author, publisher, publicationDate;
Book book("", "", "", "");

switch (choice) {
case 1:
cout << "Enter the title of the book: ";
getline(cin >> ws, title);
cout << "Enter the author of the book: ";
getline(cin >> ws, author);
cout << "Enter the publisher of the book: ";
getline(cin >> ws, publisher);
cout << "Enter the publication date of the book: ";
getline(cin >> ws, publicationDate);
book = Book(title, author, publisher, publicationDate);
library.addBook(book);
cout << title << " has been added to the library." << endl << endl;
break;
case 2:
cout << "Enter the title of the book you want to remove: ";
getline(cin >> ws, title);
library.removeBook(title);
cout << title << " has been removed from the library." << endl << endl;
break;
case 3:
cout << "Enter the title of the book you want to search for: ";
getline(cin >> ws, title);
book = library.searchBook(title);
if (book.getTitle() != "") {
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Publisher: " << book.getPublisher() << endl;
cout << "Publication Date: " << book.getPublicationDate() << endl << endl;
} else {
cout << title << " is not in the library." << endl << endl;
}
break;
case 4:
library.displayAllBooks();
break;
case 5:
exit(0);
break;
default:
cout << "Invalid choice. Please try again." << endl << endl;
}
}
};

int main() {
Menu menu = Menu();
int choice;

while (true) {
menu.displayMenu();
choice = menu.getUserChoice();
menu.performAction(choice);
}

return 0;
}

标签:cout,title,Book,books,周二,打卡,书籍,string
From: https://www.cnblogs.com/zeyangshuaige/p/17353543.html

相关文章

  • 打卡3(Java)
    image.pnimportjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);Stringa=sc.next(),b=sc.next(),c=sc.next();Stringres;if("vertebrado".equals(a......
  • 打卡10
    #include<stdio.h>intmain(){charc='1';//强制类型转换取到的是其字符对应的ASCII值inta=(int)c;printf("%d",a);//取到字符本身的整型值intb=c-48;printf("%d",b);return0;} ......
  • 2023年4月25日周二
    计划了解调试功能,mock功能如何实现的知道接口怎么回事,尝试或明白一个接口怎么写精简代码学习angular框架回顾上一周的博客接口中的请求头是怎么回事执行08点59分  查重09点07分  完全重头跑一次代码09点34分  回顾上一周的博客10点02分  跑代码,修改界......
  • 4月25日打卡
     #include<bits/stdc++.h>usingnamespacestd;intmain(){intx1,x2,x3,x5,x8,y1,y2,y3,y5,y8;doublemax=0.0,result;for(x8=0;x8<=2;x8++){for(x5=0;x5<=(20-8*x8)/5;x5++){......
  • 建民每日打卡4.25
    一、问题描述本题要求你计算A−B。不过麻烦的是,A和B都是字符串——即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。二、流程设计输入为包含空格的字符串,所以用getls()读入。每个字符串都是由可见的ASCII码和空白字符组成,所以只需要将B中出现的......
  • 打卡第十一天
    编写一个求x的n次方的函数一、1.新定义函数power,在进行调用/二、三、#include<iostream>usingnamespacestd;doublepower(doublex,intn){ doubleq=1.0; while(n--) q*=x; returnq;}intmain(){ cout<<"4的3次方是"<<power(4,3)<<endl; return0;}四、#incl......
  • 打卡6
    #include<iostream>usingnamespacestd;intmain(){intscore=0; cout<<"请您输入一个分数:"<<endl; cin>>score; if(score>700) { cout<<"恭喜您考上石家庄铁道大学"<<endl; }elseif(score>600) { ......
  • 4.24打卡
    二、设计思路、1.先输出前两个月的兔子数,然后进入循环2.循环中每过一个月都会有新兔子产生,迭代求出当前月份的兔子数3.求和输出每个月的兔子总数 三、程序流程图 四、代码实现#include<stdio.h>usingnamespacestd;intmain(){longrab1=1,rab2=1,rab......
  • 第七天打卡
    #include<iostream>usingnamespacestd;intmain(){floata,b,c,d,x;a=1000/(1+12*0.0063);b=(a+1000)/(1+12*0.0063);c=(b+1000)/(1+12*0.0063);d=(c+1000)/(1+12*0.0063);x=(d+1000)/(1+12*0.0063);printf("应存入的钱数为:%0.2f......
  • 打卡8
    1.请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。#includevoid main(){char letter;printf("please input the first letter of someday\n");while ((letter=getch())!='Y')/*当所按字母为 Y 时才结束*/{ switch (letter){ca......