/
centerwidget.cpp
executable file
·162 lines (137 loc) · 4.24 KB
/
centerwidget.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <QtGui>
#include <QVector>
#include <fstream>
#include <string>
#include "centerwidget.h"
//各类图形矩阵题
#include "overlay.h" //图形叠加类
#include "portfolio.h" //图形组合类
#include "split.h" //图形拆分类
#include "clockwise.h" //图形旋转类
//其他待扩展类型的矩阵题
//构造函数
CenterWidget::CenterWidget(int questions):questionMax(questions)
{
layout=new QGridLayout(this);
questionNum=1;//题号初始化为1
qnum=new QLabel(QString("Question %1").arg(questionNum),this);
layout->addWidget(qnum,0,0,1,1,Qt::AlignLeft);
//随机生成新的矩阵题
newQuestion();
layout->addWidget(matrices,1,0);
//加入计时显示
QHBoxLayout *buttom=new QHBoxLayout();
showtime=new QLCDNumber(this);
showtime->setSegmentStyle(QLCDNumber::Filled);
buttom->addWidget(showtime);
//加入下一题按钮Next
buttom->addSpacing(150);
buttonNext=new QPushButton(QIcon("../images/next.png"),"Next",this);
buttonNext->setMinimumSize(80,40);
buttom->addWidget(buttonNext);
connect(buttonNext,SIGNAL(clicked()),this,SLOT(nextMatrix()));
layout->addLayout(buttom,2,0);
//计时设置
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
time.start();
timer->start(1000);
update();
}
//生成下一个矩阵题
void CenterWidget::nextMatrix()
{
userAnswers.push_back(matrices->getUserAnswer());
questionNum++;//题号加1
if(questionNum>questionMax){//已达到测试题数
getResult();
return;
}
//消去窗口中的当前矩阵题
layout->removeWidget(matrices);
layout->removeWidget(qnum);
delete matrices;
delete qnum;
//在窗口中加入下一个矩阵题
newQuestion();
qnum=new QLabel(QString("Question %1").arg(questionNum),this);
layout->addWidget(qnum,0,0);
layout->addWidget(matrices,1,0);
}
//计时刷新
void CenterWidget::update()
{
int countdown=questionMax*30000-time.elapsed();
if(countdown<=0)
{
getResult();
return;
}
QString text = QString("%1:%2").arg(countdown/60000).arg(countdown%60000/1000);
if(text.section(':',1,1).size()<2)
text.insert(text.size()-1,"0");
showtime->display(text);
}
//设置最大题数
void CenterWidget::setMaxNum(int max)
{
questionMax=max;
}
//显示测试结果
void CenterWidget::getResult()
{
delete timer;
int count=0;
std::string info;
QMessageBox msg;//测试结果对话框
QString str;
std::ifstream infile("info.dat");
//测试者信息
std::getline(infile,info);
std::getline(infile,info);
std::getline(infile,info);
str.append("Name:\t");
str.append(info.c_str());
str.append("\r\n");
std::getline(infile,info);
str.append("Age:\t");
str.append(info.c_str());
str.append("\r\n");
std::getline(infile,info);
str.append("Gender:\t");
str.append(info=="0"?"Male":"Female");
str.append("\r\n\r\n");
infile.close();
for(int i=0;i<rightAnswers.size()&&i<userAnswers.size();i++)
if(rightAnswers[i]==userAnswers[i])
count++;
str.append(QString("You've got %1 correct answers out of %2 questions").arg(count).arg(questionMax));
str.append("\r\n\r\n");
float score=(float)count/(float)questionMax;
switch((int)(score*5)){
case 0:str.append("I'm sorry about you");break;
case 1:str.append("You need improvement");break;
case 2:str.append("You're Normal");break;
case 3:str.append("You're Good");break;
case 4:str.append("You're Brilliant");break;
case 5:str.append("You're a Geniu");break;
}
msg.setText(str);
msg.exec();
}
//随机生成新的矩阵题
void CenterWidget::newQuestion()
{
//设置随机数种子
QTime t=QTime::currentTime();
qsrand(t.msec()+t.second()*1000);
int k=qrand()%4;
switch(k){
case 0:matrices=new Overlay();break;//图形叠加
case 1:matrices=new Portfolio();break;//图形组合
case 2:matrices=new Split();break;//图形拆分
case 3:matrices=new Clockwise();break;//图形旋转
}
//放置正确答案
rightAnswers.push_back(matrices->getRightAnswer());
}