-
Notifications
You must be signed in to change notification settings - Fork 0
/
TArithm.cpp
154 lines (151 loc) · 3.1 KB
/
TArithm.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
#include "TArithm.h"
#include "TStack.h"
#include <iostream>
#include <string>
using namespace std;
TArithm::TArithm(string str) {
mist_imbalance = 0;
mist_braces = 0;
mist_null = 0;
infix = str;
postfix = Postfix();
res = Calc();
}
TArithm::~TArithm() = default;
string TArithm::Postfix() {
string Postfix;
TStack op;
int right_brace = 0, left_brace = 0;
int num_counter = 0, op_counter = 0;
for (char i : infix) {
if (IsNumber(i)) {
Postfix += i;
num_counter++;
}
else if (i == ')') {
right_brace++;
while (!op.Empty() && op.Top() != '(') {
Postfix += op.Get();
}
op.Get();
}
else if (i == '(') {
left_brace++;
op.Put(i);
}
else {
op_counter++;
while (!op.Empty() && GetPriority(op.Top()) >= GetPriority(i)) {
Postfix += op.Get();
}
op.Put(i);
}
}
while (!op.Empty()) {
Postfix += op.Get();
}
if (!(num_counter == op_counter + 1)) {
mist_imbalance++;
}
if (left_brace != right_brace) {
mist_braces++;
}
if (left_brace + right_brace > 0) {
Braces();
}
return Postfix;
}
bool TArithm::IsNumber(char i) {
return ((i == '1') || (i == '2') || (i == '3') || (i == '4') || (i == '5') || (i == '6') || (i == '7') || (i == '8') || (i == '9') || (i == '0'));
}
int TArithm::GetPriority(double i) {
int p = 0;
if (i == '+' || i == '-')
p = 1;
else if (i == '*' || i == '/')
p = 2;
return p;
}
double TArithm::Calc() {
TStack Stack;
for (char i : postfix) {
if (IsNumber(i)) Stack.Put(i - '0');
else {
double second = Stack.Get();
double first = Stack.Get();
double res = 0;
switch (i) {
case '+':
res = first + second;
break;
case '-':
res = first - second;
break;
case '*':
res = first * second;
break;
case '/':
if (second == 0) {
mist_null++;
res = 0;
break;
}
res = first / second;
break;
}
Stack.Put(res);
}
}
return (Stack.Get());
}
void TArithm::Input() {
mist_null = 0;
mist_imbalance = 0;
mist_braces = 0;
cin >> infix;
postfix = Postfix();
res = Calc();
}
void TArithm::Output() {
cout << "Âûðàæåíèå: " << infix << endl;
cout << "Ïîñòôèêñíàÿ ôîðìà: " << postfix << endl;
if ((mist_braces + mist_imbalance + mist_null) == 0) {
cout << "Ðåçóëüòàò: " << res << endl;
}
else {
if (mist_null > 0) {
cout << "Îøèáêà: äåëåíèå íà íîëü" << endl;
}
if (mist_imbalance > 0) {
cout << "Îøèáêà: êîëè÷åñòâî îïåðàöèé íå ñîîòâåòñòâóåò êîëè÷åñòâó îïåðàíäîâ (îïåðàöèè = îïåðàíäû - 1)" << endl;
}
if (mist_braces > 0) {
cout << "Îøèáêà: êîëè÷åñòâî ëåâûõ ñêîáîê íå ñîîòâåòñòâóåò êîëè÷åñòâó ïðàâûõ ñêîáîê èëè ñêîáêè ðàñïîëîæåíû íåâåðíî" << endl;
}
}
cout << endl << endl;
}
void TArithm::Braces() {
cout << "\t(\t\t\t)" << endl;
double counter = 0;
TStack Stack;
for (char i : infix) {
if (i == '(') {
Stack.Put(++counter);
}
else if (i == ')') {
counter++;
if (!Stack.Empty()) {
cout << "\t" << Stack.Get() << "\t\t\t" << counter << endl;
}
else {
cout << "\t" << 0 << "\t\t\t" << counter << endl;
mist_braces++;
}
}
}
while (!Stack.Empty()) {
cout << "\t" << Stack.Get() << "\t\t\t" << 0 << endl;
mist_braces++;
}
}