-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautotest.h
More file actions
104 lines (93 loc) · 2.64 KB
/
autotest.h
File metadata and controls
104 lines (93 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef AUTOTEST_H
#define AUTOTEST_H
#endif // AUTOTEST_H
#include <QString>
#include <QVector>
#include <QRandomGenerator>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include <QCheckBox>
#include <QMessageBox>
#include <QComboBox>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QPrinter>
#include <QPrintDialog>
#include <QPainter>
class Expression {
public:
QString generate();
QString generateOperand();
QChar generateOperator();
int maxNumber;
QVector<QChar> operators;
bool allowParentheses;
bool allowDecimals;
};
// 生成括号及调用其他生成函数
QString Expression::generate() {
int numOperands = QRandomGenerator::global()->bounded(2, 5);
QString expr;
int leftParenCount = 0;
for (int i = 0; i < numOperands; i++) {
if (i > 0 && allowParentheses && QRandomGenerator::global()->bounded(5) == 0) {
expr += "(";
leftParenCount++;
expr += generateOperand();
expr += generateOperator();
expr += generateOperand();
i += 2;
} else {
expr += generateOperand();
}
if (leftParenCount > 0 && (i == numOperands - 1 || QRandomGenerator::global()->bounded(3) == 0)) {
expr += ")";
leftParenCount--;
}
if (i < numOperands - 1) {
expr += generateOperator();
}
}
while (leftParenCount > 0) {
expr += ")";
leftParenCount--;
}
return expr;
}
// 生成操作数
QString Expression::generateOperand() {
double operand = QRandomGenerator::global()->bounded(1, maxNumber + 1);
if (allowDecimals && QRandomGenerator::global()->bounded(2) == 0) {
operand += static_cast<double>(QRandomGenerator::global()->bounded(100)) / 100.0;
return QString::number(operand, 'f', 2);
}
else {
return QString::number(operand);
}
}
// 生成操作符
QChar Expression::generateOperator() {
int index = QRandomGenerator::global()->bounded(operators.size());
return operators[index];
}
// 使用系统打印
void printDocument(QWidget *parent, const QString& expressions) {
QPrinter printer;
QPrintDialog printDialog(&printer, parent);
if (printDialog.exec() == QDialog::Accepted) {
QPainter painter(&printer);
QFont font("Arial", 12);
painter.setFont(font);
QStringList lines = expressions.split("\n");
int y = 100;
for (const QString& line : lines) {
painter.drawText(100, y, line);
y += 20;
}
}
}