-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (88 loc) · 3.64 KB
/
main.cpp
File metadata and controls
103 lines (88 loc) · 3.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
#include "autotest.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.resize(800, 400);
window.setWindowTitle("四则运算生成器");
QVBoxLayout *layout = new QVBoxLayout(&window);
QLabel *numLabel = new QLabel("请输入要生成的题目数量:");
QLineEdit *numInput = new QLineEdit;
QLabel *maxNumLabel = new QLabel("请输入最大数值:");
QLineEdit *maxNumInput = new QLineEdit;
QLabel *operatorsLabel = new QLabel("请选择运算符 (+, -, *, /):");
QLineEdit *operatorsInput = new QLineEdit;
QLabel *parenthesesLabel = new QLabel("是否允许括号:");
QCheckBox *parenthesesInput = new QCheckBox;
QLabel *decimalsLabel = new QLabel("是否允许小数:");
QCheckBox *decimalsInput = new QCheckBox;
QLabel *outputMethodLabel = new QLabel("输出方式:");
QComboBox *outputMethodComboBox = new QComboBox;
outputMethodComboBox->addItem("显示在界面");
outputMethodComboBox->addItem("输出到文件");
outputMethodComboBox->addItem("打印");
QPushButton *generateButton = new QPushButton("生成");
QLabel *outputLabel = new QLabel("生成的表达式将显示在这里");
layout->addWidget(numLabel);
layout->addWidget(numInput);
layout->addWidget(maxNumLabel);
layout->addWidget(maxNumInput);
layout->addWidget(operatorsLabel);
layout->addWidget(operatorsInput);
layout->addWidget(parenthesesLabel);
layout->addWidget(parenthesesInput);
layout->addWidget(decimalsLabel);
layout->addWidget(decimalsInput);
layout->addWidget(outputMethodLabel);
layout->addWidget(outputMethodComboBox);
layout->addWidget(generateButton);
layout->addWidget(outputLabel);
Expression exp;
QObject::connect(generateButton, &QPushButton::clicked, [&]() {
bool ok;
int numExpressions = numInput->text().toInt(&ok);
if (!ok) {
QMessageBox::warning(&window, "错误", "请输入有效的数字!");
return;
}
exp.maxNumber = maxNumInput->text().toInt();
QString ops = operatorsInput->text();
if (ops.isEmpty()) {
QMessageBox::warning(&window, "错误", "请输入至少一个操作符!");
return;
}
for (QChar op : ops) {
if (op != '+' && op != '-' && op != '*' && op != '/') {
QMessageBox::warning(&window, "错误", "请输入合法的运算符 (+, -, *, /)!");
return;
}
}
exp.operators = QVector<QChar>(ops.begin(), ops.end());
exp.allowParentheses = parenthesesInput->isChecked();
exp.allowDecimals = decimalsInput->isChecked();
QString expressions;
for (int i = 0; i < numExpressions; i++) {
expressions += exp.generate() + " =\n";
}
if (outputMethodComboBox->currentIndex() == 0) {
outputLabel->setText(expressions);
}
else if (outputMethodComboBox->currentIndex() == 1) {
QString fileName = QFileDialog::getSaveFileName(&window, "保存文件", "", "文本文件 (*.txt)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << expressions;
file.close();
} else {
QMessageBox::warning(&window, "错误", "无法打开文件进行写入!");
}
}
}
else {
printDocument(&window, expressions);
}
});
window.show();
return app.exec();
}