c++图书管理系统带饼状图,
时间: 2023-06-29 09:11:59 浏览: 195
下面是一个简单的C++图书管理系统,带有饼状图功能的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include "qcustomplot.h"
using namespace std;
// 图书类
class Book {
public:
Book(string name, string author, float price, int count) : name(name), author(author), price(price), count(count) {}
string getName() const { return name; }
string getAuthor() const { return author; }
float getPrice() const { return price; }
int getCount() const { return count; }
private:
string name; // 书名
string author; // 作者
float price; // 价格
int count; // 数量
};
// 图书管理系统类
class BookManager {
public:
// 添加图书
void addBook(Book book) {
books.push_back(book);
}
// 统计所有图书的价格总和
float getTotalPrice() const {
float total = 0;
for (const Book &book : books) {
total += book.getPrice() * book.getCount();
}
return total;
}
// 统计所有图书的数量总和
int getTotalCount() const {
int total = 0;
for (const Book &book : books) {
total += book.getCount();
}
return total;
}
// 统计每个作者的图书数量
vector<pair<string, int>> getAuthorCount() const {
vector<pair<string, int>> authorCount;
for (const Book &book : books) {
bool found = false;
for (auto &item : authorCount) {
if (item.first == book.getAuthor()) {
item.second += book.getCount();
found = true;
break;
}
}
if (!found) {
authorCount.push_back(make_pair(book.getAuthor(), book.getCount()));
}
}
return authorCount;
}
// 显示饼状图
void showPieChart() const {
// 创建QCustomPlot对象
QCustomPlot *customPlot = new QCustomPlot();
// 获取每个作者的图书数量
vector<pair<string, int>> authorCount = getAuthorCount();
// 创建饼图数据
QVector<double> data;
QVector<QString> labels;
QVector<QColor> colors;
for (auto &item : authorCount) {
labels << QString::fromStdString(item.first);
data << item.second;
colors << QColor(rand()%256, rand()%256, rand()%256);
}
// 创建饼图
QCPBars *bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars->setWidth(1.2);
bars->setData(data);
bars->setBrush(QBrush(QColor(255,255,255,0)));
bars->setPen(QPen(QColor(255,255,255,0)));
// 设置饼图标签
QCPItemText *textLabel = new QCPItemText(customPlot);
textLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);
textLabel->position->setType(QCPItemPosition::ptAxisRectRatio);
textLabel->position->setCoords(0.5, 0); // 放在顶部中间
textLabel->setText("作者图书数量饼状图");
textLabel->setFont(QFont("sans", 14));
// 设置饼图参数
customPlot->legend->setVisible(true);
customPlot->legend->setFont(QFont("sans", 10));
customPlot->legend->setBrush(QBrush(QColor(255,255,255,200)));
customPlot->legend->setSelectableParts(QCPLegend::spItems);
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
customPlot->rescaleAxes();
customPlot->setBackground(QBrush(QColor(240,240,240)));
// 设置饼图数据标签
QCPItemTracer *tracer = new QCPItemTracer(customPlot);
tracer->setGraph(bars);
tracer->setInterpolating(false);
tracer->setStyle(QCPItemTracer::tsCircle);
tracer->setPen(QPen(Qt::black));
tracer->setBrush(Qt::white);
tracer->setSize(10);
for (int i = 0; i < data.size(); ++i) {
QCPItemText *dataLabel = new QCPItemText(customPlot);
dataLabel->setLayer("overlay");
dataLabel->setFont(QFont("sans", 10));
dataLabel->setText(QString::number(data[i]));
dataLabel->position->setType(QCPItemPosition::ptPolar);
dataLabel->position->setCoords((i+0.5)*2*M_PI/data.size(), data[i]);
if (i % 2 == 0)
dataLabel->setColor(Qt::black);
else
dataLabel->setColor(Qt::white);
tracer->addTargetableItem(dataLabel);
}
// 设置饼图数据
QCPPlotTitle *title = new QCPPlotTitle(customPlot);
title->setText(QString("总数量:%1").arg(getTotalCount()));
title->setFont(QFont("sans", 12));
customPlot->plotLayout()->insertRow(0);
customPlot->plotLayout()->addElement(0, 0, title);
// 添加饼图
customPlot->addGraph();
customPlot->graph()->setName("作者图书数量");
customPlot->graph()->setLineStyle(QCPGraph::lsNone);
customPlot->graph()->setScatterStyle(QCPScatterStyle::ssCircle);
customPlot->graph()->setScatterSize(10);
customPlot->graph()->setData(labels, data);
QSharedPointer<QCPAxisTickerText> ticker(new QCPAxisTickerText);
ticker->addTicks(labels, QVector<double>() << 1 << 2 << 3 << 4 << 5);
customPlot->xAxis->setTicker(ticker);
customPlot->xAxis->setTickLabelRotation(60);
customPlot->xAxis->setSubTicks(false);
customPlot->xAxis->setTickLength(0, 4);
customPlot->xAxis->setRange(0, 6);
customPlot->xAxis->setBasePen(QPen(Qt::black));
customPlot->yAxis->setRange(0, 10);
customPlot->yAxis->setPadding(5);
customPlot->yAxis->setLabel("数量");
customPlot->yAxis->setBasePen(QPen(Qt::black));
customPlot->yAxis->setTickPen(QPen(Qt::black));
customPlot->yAxis->grid()->setSubGridVisible(true);
customPlot->yAxis->setTickLabelColor(Qt::black);
customPlot->yAxis->setLabelColor(Qt::black);
customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
customPlot->yAxis->grid()->setSubGridColor(QColor(130, 130, 130));
// 显示饼图
customPlot->show();
}
private:
vector<Book> books; // 图书列表
};
int main() {
// 创建图书管理系统
BookManager manager;
// 添加图书
manager.addBook(Book("C++ Primer", "Lippman", 39.99, 10));
manager.addBook(Book("Effective C++", "Meyers", 29.99, 8));
manager.addBook(Book("The C++ Programming Language", "Stroustrup", 49.99, 6));
manager.addBook(Book("Head First C++", "Griffiths", 34.99, 12));
manager.addBook(Book("Thinking in C++", "Eckel", 29.99, 5));
// 显示总价和总数
cout << "总价:" << manager.getTotalPrice() << endl;
cout << "总数:" << manager.getTotalCount() << endl;
// 显示每个作者的图书数量
vector<pair<string, int>> authorCount = manager.getAuthorCount();
for (auto &item : authorCount) {
cout << item.first << ":" << item.second << endl;
}
// 显示饼状图
manager.showPieChart();
return 0;
}
```
这段代码创建了一个简单的图书管理系统,其中包括添加图书、统计总价和总数、统计每个作者的图书数量以及显示饼状图等功能。在显示饼状图时,使用QCustomPlot库创建一个饼状图,并使用统计的每个作者的图书数量作为饼状图的数据,最终显示在屏幕上。
阅读全文