1、问题描述 定义图书类,属性有:书名、出版社、isbn号、作者、库存量、价格等信息
时间: 2024-01-02 11:00:26 浏览: 78
图书类是一种用来描述图书的模板或者蓝图。在这个类中,我们可以定义图书的各种属性,包括书名、出版社、ISBN号、作者、库存量、价格等信息。
首先是书名属性,用来表示图书的名称或者标题。每本图书都有一个独特的书名。
其次是出版社属性,用来表示图书的出版社或者出版公司。这个属性可以告诉我们图书是由哪个出版社出版的。
然后是ISBN号属性,用来表示图书的国际标准书号。每本图书都有一个唯一的ISBN号,可以用来区分不同的图书。
接下来是作者属性,用来表示图书的作者或者撰写者。这个属性可以告诉我们图书是由哪个作者编写的。
然后是库存量属性,用来表示图书当前的库存数量。这个属性可以告诉我们有多少本图书还可以供应。
最后是价格属性,用来表示图书的价格。这个属性可以告诉我们图书的销售价格。
通过定义这些属性,我们可以创建图书对象,并且可以获取或者修改这些属性的值。比如,我们可以创建一本名为《Java编程思想》的图书对象,它的出版社是机械工业出版社,ISBN号是9787111213826,作者是Bruce Eckel,库存量是50本,价格是99元。
相关问题
用c++实现以下程序:题目2 "图书管理系统设计" 1、问题描述定义图书类,属性有:书名、出版社、 ISBN 号、作者、库存量、价格等信息和相关的对属性做操作的行为。主要完成对图书的销售、统计和图书的简单管理.
好的,下面是一个简单的图书管理系统的C++代码示例:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 图书类
class Book {
public:
Book(string name, string author, string publisher, string ISBN, int stock, double price) {
this->name = name;
this->author = author;
this->publisher = publisher;
this->ISBN = ISBN;
this->stock = stock;
this->price = price;
}
// 获取属性
string getName() {
return name;
}
string getAuthor() {
return author;
}
string getPublisher() {
return publisher;
}
string getISBN() {
return ISBN;
}
int getStock() {
return stock;
}
double getPrice() {
return price;
}
// 修改属性
void setName(string name) {
this->name = name;
}
void setAuthor(string author) {
this->author = author;
}
void setPublisher(string publisher) {
this->publisher = publisher;
}
void setISBN(string ISBN) {
this->ISBN = ISBN;
}
void setStock(int stock) {
this->stock = stock;
}
void setPrice(double price) {
this->price = price;
}
// 增加库存
void addStock(int num) {
stock += num;
}
// 减少库存
void reduceStock(int num) {
stock -= num;
}
private:
string name; // 书名
string author; // 作者
string publisher; // 出版社
string ISBN; // ISBN号
int stock; // 库存量
double price; // 价格
};
// 图书馆类
class Library {
public:
// 添加图书
void addBook(Book book) {
books.push_back(book);
cout << "添加图书成功!" << endl;
}
// 删除图书
void removeBook(string ISBN) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getISBN() == ISBN) {
books.erase(books.begin() + i);
cout << "删除图书成功!" << endl;
return;
}
}
cout << "没有找到要删除的图书!" << endl;
}
// 显示所有图书
void displayAllBooks() {
cout << "图书馆一共有" << books.size() << "本图书:" << endl;
for (int i = 0; i < books.size(); i++) {
cout << "书名:" << books[i].getName() << ",作者:" << books[i].getAuthor()
<< ",出版社:" << books[i].getPublisher() << ",ISBN号:" << books[i].getISBN()
<< ",库存量:" << books[i].getStock() << ",价格:" << books[i].getPrice() << endl;
}
}
// 销售图书
void sellBook(string ISBN, int num) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getISBN() == ISBN) {
if (books[i].getStock() >= num) {
books[i].reduceStock(num);
cout << "销售图书成功!" << endl;
}
else {
cout << "库存不足,无法销售!" << endl;
}
return;
}
}
cout << "没有找到要销售的图书!" << endl;
}
// 统计图书总数和总价值
void statistics() {
int totalNum = 0;
double totalPrice = 0.0;
for (int i = 0; i < books.size(); i++) {
totalNum += books[i].getStock();
totalPrice += books[i].getStock() * books[i].getPrice();
}
cout << "图书馆一共有" << totalNum << "本图书,总价值为" << totalPrice << "元。" << endl;
}
private:
vector<Book> books; // 图书集合
};
// 主函数
int main() {
Library library; // 创建一个图书馆对象
// 添加图书
Book book1("C++ Primer", "Stanley B. Lippman", "人民邮电出版社", "9787115226923", 10, 98.0);
library.addBook(book1);
Book book2("Effective C++", "Scott Meyers", "机械工业出版社", "9787111218652", 5, 49.0);
library.addBook(book2);
// 显示所有图书
library.displayAllBooks();
// 销售图书
library.sellBook("9787115226923", 3);
// 统计图书总数和总价值
library.statistics();
return 0;
}
```
上面这个代码实现了一个较为完整的图书管理系统,可以添加图书、删除图书、显示所有图书、销售图书、统计图书总数和总价值等功能。你可以根据自己的需求进行修改和扩展。
采用链表数据结构实现一“图书管理系统〞,为用户提供管理和查询服务。 图书信息包括8个数据项:书名,ISBN、作者、出版社、出版时间,入库时间, 库存量(图书馆购买此书的数量),借阅数(被借阅的数量)等信息。完成以下 操作:
好的,我会给出一个简单的链表数据结构实现图书管理系统的示例代码。在此之前,需要先定义图书信息的结构体,如下所示:
```c
typedef struct book {
char name[50]; // 书名
char ISBN[20]; // ISBN
char author[50]; // 作者
char publisher[50]; // 出版社
char pub_time[20]; // 出版时间
char in_time[20]; // 入库时间
int stock; // 库存量
int borrow; // 借阅数
struct book *next; // 指向下一本书的指针
} Book;
```
接下来,我们可以定义一个链表的头结点:
```c
Book *head = NULL;
```
然后,我们可以实现以下操作:
1. 添加图书信息
```c
void add_book() {
Book *p = (Book*) malloc(sizeof(Book));
printf("请输入书名:");
scanf("%s", p->name);
printf("请输入ISBN:");
scanf("%s", p->ISBN);
printf("请输入作者:");
scanf("%s", p->author);
printf("请输入出版社:");
scanf("%s", p->publisher);
printf("请输入出版时间:");
scanf("%s", p->pub_time);
printf("请输入入库时间:");
scanf("%s", p->in_time);
printf("请输入库存量:");
scanf("%d", &p->stock);
printf("请输入借阅数:");
scanf("%d", &p->borrow);
p->next = NULL;
if (head == NULL) {
head = p;
} else {
Book *q = head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
printf("添加成功!\n");
}
```
2. 删除图书信息
```c
void del_book() {
char ISBN[20];
printf("请输入要删除的图书的ISBN:");
scanf("%s", ISBN);
Book *p = head;
Book *q = NULL;
while (p != NULL && strcmp(p->ISBN, ISBN) != 0) {
q = p;
p = p->next;
}
if (p == NULL) {
printf("未找到该图书!\n");
} else {
if (p == head) {
head = p->next;
} else {
q->next = p->next;
}
free(p);
printf("删除成功!\n");
}
}
```
3. 修改图书信息
```c
void modify_book() {
char ISBN[20];
printf("请输入要修改的图书的ISBN:");
scanf("%s", ISBN);
Book *p = head;
while (p != NULL && strcmp(p->ISBN, ISBN) != 0) {
p = p->next;
}
if (p == NULL) {
printf("未找到该图书!\n");
} else {
printf("请输入新的书名(原书名:%s):", p->name);
scanf("%s", p->name);
printf("请输入新的作者(原作者:%s):", p->author);
scanf("%s", p->author);
printf("请输入新的出版社(原出版社:%s):", p->publisher);
scanf("%s", p->publisher);
printf("请输入新的出版时间(原出版时间:%s):", p->pub_time);
scanf("%s", p->pub_time);
printf("请输入新的入库时间(原入库时间:%s):", p->in_time);
scanf("%s", p->in_time);
printf("请输入新的库存量(原库存量:%d):", p->stock);
scanf("%d", &p->stock);
printf("请输入新的借阅数(原借阅数:%d):", p->borrow);
scanf("%d", &p->borrow);
printf("修改成功!\n");
}
}
```
4. 查询图书信息
```c
void query_book() {
char ISBN[20];
printf("请输入要查询的图书的ISBN:");
scanf("%s", ISBN);
Book *p = head;
while (p != NULL && strcmp(p->ISBN, ISBN) != 0) {
p = p->next;
}
if (p == NULL) {
printf("未找到该图书!\n");
} else {
printf("书名:%s\n", p->name);
printf("ISBN:%s\n", p->ISBN);
printf("作者:%s\n", p->author);
printf("出版社:%s\n", p->publisher);
printf("出版时间:%s\n", p->pub_time);
printf("入库时间:%s\n", p->in_time);
printf("库存量:%d\n", p->stock);
printf("借阅数:%d\n", p->borrow);
}
}
```
5. 显示所有图书信息
```c
void show_all() {
if (head == NULL) {
printf("当前没有图书信息!\n");
} else {
Book *p = head;
while (p != NULL) {
printf("书名:%s\n", p->name);
printf("ISBN:%s\n", p->ISBN);
printf("作者:%s\n", p->author);
printf("出版社:%s\n", p->publisher);
printf("出版时间:%s\n", p->pub_time);
printf("入库时间:%s\n", p->in_time);
printf("库存量:%d\n", p->stock);
printf("借阅数:%d\n", p->borrow);
p = p->next;
}
}
}
```
以上就是一个简单的链表数据结构实现图书管理系统的示例代码。