-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
134 lines (114 loc) · 4.13 KB
/
main.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <QApplication>
#include <QFile>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>
#include <QTextEdit>
#include <QTextStream>
#include <algorithm>
#include <chrono>
#include <random>
class JustRollIt : public QMainWindow
{
Q_OBJECT
public:
JustRollIt(QWidget *parent = nullptr)
: QMainWindow(parent)
{
setWindowTitle("抽奖工具");
QVBoxLayout *layout = new QVBoxLayout;
inputFileButton = new QPushButton("选择参与者名单文件");
connect(inputFileButton, &QPushButton::clicked, this, &JustRollIt::loadParticipantsFromFile);
layout->addWidget(inputFileButton);
numParticipantsLabel = new QLabel("参与者人数:");
layout->addWidget(numParticipantsLabel);
numWinnersLabel = new QLabel("中奖者人数:");
numWinnersInput = new QLineEdit;
winnersLayout = new QHBoxLayout;
winnersLayout->addWidget(numWinnersLabel);
winnersLayout->addWidget(numWinnersInput);
layout->addLayout(winnersLayout);
startButton = new QPushButton("开始抽奖");
connect(startButton, &QPushButton::clicked, this, &JustRollIt::startLottery);
layout->addWidget(startButton);
winnersLabel = new QLabel("中奖者名单:");
layout->addWidget(winnersLabel);
winnersTextEdit = new QTextEdit;
winnersTextEdit->setReadOnly(true);
layout->addWidget(winnersTextEdit);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}
private slots:
void loadParticipantsFromFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "选择参与者名单文件");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
int numParticipants = 0;
while (!in.atEnd()) {
in.readLine();
numParticipants++;
}
file.close();
numParticipantsLabel->setText("参与者人数:" + QString::number(numParticipants));
participants.clear();
file.open(QIODevice::ReadOnly | QIODevice::Text);
while (!in.atEnd()) {
participants.append(in.readLine());
}
file.close();
} else {
QMessageBox::critical(this, "错误", "无法打开文件。");
}
}
}
void startLottery()
{
int numWinners = numWinnersInput->text().toInt();
if (numWinners <= 0 || numWinners > participants.size()) {
QMessageBox::warning(this,
"警告",
"无效的中奖者人数。请确保中奖者人数大于0且不超过参与者人数。");
return;
}
winnersTextEdit->clear();
QList<QString> remainingParticipants = participants;
std::shuffle(remainingParticipants.begin(),
remainingParticipants.end(),
std::mt19937(std::chrono::steady_clock::now().time_since_epoch().count()));
for (int i = 0; i < numWinners; ++i) {
if (remainingParticipants.isEmpty()) {
QMessageBox::warning(this, "警告", "所有参与者已中奖。");
break;
}
QString winner = remainingParticipants.takeFirst();
winnersTextEdit->append(QString::number(i + 1) + ". " + winner);
}
}
private:
QPushButton *inputFileButton;
QLabel *numParticipantsLabel;
QLabel *numWinnersLabel;
QLineEdit *numWinnersInput;
QPushButton *startButton;
QLabel *winnersLabel;
QTextEdit *winnersTextEdit;
QHBoxLayout *winnersLayout;
QStringList participants;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
JustRollIt justRollIt;
justRollIt.show();
return app.exec();
}
#include "main.moc"