-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestions_Diez.cpp
More file actions
85 lines (80 loc) · 2.37 KB
/
Questions_Diez.cpp
File metadata and controls
85 lines (80 loc) · 2.37 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
#include "Questions_Diez.h"
namespace
{
Answer ParseAnswer(const QVector<QStringRef>& tokens, int& pos)
{
Answer answer { };
bool isNumber = false;
answer.isRight = tokens.at(pos).toInt(&isNumber) == 1;
if (!isNumber)
{
qDebug() << "Parsing of answer rightness is failed";
}
bool end = false;
for (++pos; pos < tokens.size() && !end;)
{
QString token = tokens.at(pos).toString();
token.toInt(&isNumber);
end = isNumber || token == "#";
if (!end)
{
answer.text += tokens.at(pos).toString() + " ";
++pos;
}
}
return answer;
}
Answers ParseAnswers(const QVector<QStringRef>& tokens)
{
Answers answers;
if (tokens.size() < 2)
{
qDebug() << "Detected question with wrong answers count";
return answers;
}
for (int i = 0; i < tokens.size();)
{
answers.push_back(ParseAnswer(tokens, i));
}
return answers;
}
Question ParseQuestion(const QStringRef& text)
{
Question question;
QVector<QStringRef> tokens = text.split('\n');
if (tokens.size() > 2)
{
int i = 1;
for (; i < tokens.size(); ++i)
{
bool isNumber = false;
tokens.at(i).toInt(&isNumber);
if (tokens.at(i).length() == 1 && isNumber)
{
break;
}
question.text += tokens.at(i).toString() + " ";
}
while (i--) {
tokens.pop_front();
}
question.answers = ParseAnswers(tokens);
}
return question;
}
}
void ParseQuestions_Diez(const QString& fileContent, Questions& questions)
{
for (int curIndex = fileContent.indexOf('#', 0); curIndex >= 0; )
{
int thisQuestion = curIndex;
curIndex = fileContent.indexOf('#', curIndex + 1);
int nextQuestion = (curIndex < 0) ? fileContent.size() : curIndex;
QStringRef allQuestion(&fileContent, thisQuestion, nextQuestion - thisQuestion);
Question question = ParseQuestion(allQuestion);
if (!question.IsEmpty())
{
questions.push_back(question);
}
}
}