This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
152 lines (125 loc) · 3.63 KB
/
algorithm.cpp
File metadata and controls
152 lines (125 loc) · 3.63 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
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
#include "algorithm.h"
Algorithm::Algorithm()
{
operations.append("+");
operations.append("-");
operations.append("*");
operations.append("/");
operations.append("(");
operations.append(")");
operations.append("^");
operations.append("cos");
operations.append("sin");
operations.append("tg");
operations.append("ln");
unaryOperations.append("cos");
unaryOperations.append("sin");
unaryOperations.append("tan");
unaryOperations.append("cos");
}
void Algorithm::setExpression(const QString &expression)
{
this->expression = expression;
items = split();
}
bool Algorithm::check()
{
int braces = 0;
foreach(QString item, items)
{
if(item == "(")
braces++;
else if(item == ")")
braces--;
}
return braces == 0;
}
Algorithm::~Algorithm(){}
double Algorithm::executeAction(QString first, QString action, QString second)
{
if(action == "sin")
return sin(first.toDouble());
else if(action == "cos")
return cos(first.toDouble());
else if(action == "tg")
return tan(first.toDouble());
else if(action == "ln")
return log(first.toDouble());
else if(action == "+")
return first.toDouble() + second.toDouble();
else if(action == "-")
return first.toDouble() - second.toDouble();
else if(action == "*")
return first.toDouble() * second.toDouble();
else if(action == "/")
return first.toDouble() / second.toDouble();
else if(action == "^")
return qPow(first.toDouble(), second.toDouble());
throw std::invalid_argument("Операция недоступна");
}
QVector<Algorithm::Entry> Algorithm::getEntries()
{
QVector<Entry> entries;
for(int i = 0; i < operations.size(); i++)
{
int position = expression.indexOf(operations[i], 0);
while(position > -1 && position < expression.size())
{
Entry entry;
entry.value = operations[i];
entry.startIndex = position;
entries.push_back(entry);
position += operations[i].size();
position = expression.indexOf(operations[i], position);
}
}
qSort(entries.begin(), entries.end(), [](const Entry& first, const Entry& second) {
return first.startIndex < second.startIndex;
});
return entries;
}
bool Algorithm::isNumber(QString item)
{
return !operations.contains(item);
}
QStringList Algorithm::split()
{
QVector<Entry> entries = getEntries();
QStringList result;
if(entries.size() == 0)
{
result.append(expression);
return result;
}
if(entries.size() > 0 && entries[0].startIndex > 0)
{
result.append(expression.mid(0, entries[0].startIndex));
result.append(entries[0].value);
}
for(int i = 1; i < entries.size(); i++)
{
int position = entries[i - 1].startIndex + entries[i - 1].value.size();
if(position != entries[i].startIndex)
{
int size = entries[i].startIndex - position;
result.append(expression.mid(position, size));
}
result.append(entries[i].value);
}
int lastEntry = entries.size() - 1;
int position = entries[lastEntry].startIndex + entries[lastEntry].value.size();
if(position < expression.size())
result.append(expression.mid(position, expression.size() - position));
return result;
}
bool Algorithm::isUnaryOperations(const QString &operation)
{
return unaryOperations.indexOf(operation) > -1;
}
void Algorithm::showItems()
{
foreach(QString item, items)
{
qDebug()<<item;
}
}