-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrd_calculator.cpp
171 lines (126 loc) · 3.85 KB
/
rd_calculator.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
163
164
165
166
167
168
169
170
171
#include "rd_calculator.h"
/*--------------------------FUNCTION-----------------------------------------*/
RDCalc::RDCalc( const std::string some_sequence ):
srcseq_ ( some_sequence ),
srcseq_ptr_ ( 0 ),
result_ ( 0 )
{
std::cerr << "[RDCalc]\n"; //!TODO macro with NDEBUG support
}
/*--------------------------FUNCTION-----------------------------------------*/
RDCalc::~RDCalc()
{
std::cerr << "[~RDCalc]\n";
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::calculate()
{
std::cerr << "[calculate]\n";
std::cerr << "[calculate] calculating " << srcseq_ << std::endl;
result_ = get_grammar();
return get_result();
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::get_result() //!TODO rename
{
std::cerr << "[get_result]\n";
return result_;
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::get_grammar()
{
std::cerr << "[get_grammar]\n";
int value = get_expression(); //?
if( srcseq_[srcseq_ptr_] != '$' )
{
std::cout << "[get_grammar] Syntax error!\n";
std::cout << "[get_grammar] Finished at symbol №" << srcseq_ptr_ << "\n";
return {};
}
std::cerr << "[get_grammar] returning " << value << "\n";
return value;
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::get_expression()
{
std::cerr << "[get_expression]\n";
int value = get_token();
int current_value = 0;
while( srcseq_[srcseq_ptr_] == '+' || srcseq_[srcseq_ptr_] == '-' )
{
char op = srcseq_[srcseq_ptr_];
srcseq_ptr_++;
current_value = get_token();
if( op == '-' )
{
value -= current_value;
continue;
}
value += current_value;
}
std::cerr << "[get_expression] returning " << value << "\n";
return value;
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::get_token()
{
std::cerr << "[get_token]\n";
int value = get_parentheses();
int current_value = 0;
while( srcseq_[srcseq_ptr_] == '*' || srcseq_[srcseq_ptr_] == '/' ) //!TODO переместить получение значения оператора в конец
{
std::cerr << "[get_token] while iteration\n";
char op = srcseq_[srcseq_ptr_];
srcseq_ptr_++;
current_value = get_parentheses();
if( op == '/' )
{
value /= current_value;
continue;
}
value *= current_value;
}
std::cerr << "[get_token] returning " << value << "\n";
return value;
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::get_parentheses()
{
std::cerr << "[get_parentheses]\n";
if( srcseq_[srcseq_ptr_] == '(' )
{
srcseq_ptr_++;
int value = get_expression();
std::cerr << "[get_parentheses] symbol = " << srcseq_[srcseq_ptr_] << "\n";
if( srcseq_[srcseq_ptr_] != ')' ) //!!!!!!!!!!
{
std::cout << "[get_parenthese] Syntax error!\n";
std::cout << "[get_parenthese] Finished at symbol №" << srcseq_ptr_ << "\n";
}
srcseq_ptr_++;
std::cerr << "[get_parentheses] returning " << value << "\n";
return value;
}
int value = get_number(); //дебаг
std::cerr << "[get_parentheses] returning " << value << "\n";
return value;
}
/*--------------------------FUNCTION-----------------------------------------*/
int RDCalc::get_number()
{
std::cerr << "[get_number]\n";
int value = 0;
do
{
std::cerr << "[get_number] do-while iteration\n";
std::cerr << "[get_number] symbol = " << srcseq_[srcseq_ptr_] << "\n";
std::cerr << "[get_number] symbolcode = " << srcseq_[srcseq_ptr_] - '0' << "\n";
value = value * 10 + ( srcseq_[srcseq_ptr_] - '0' );
//value = value + 10 + ( srcseq_[srcseq_ptr_] );
std::cerr << "[get_number] value = " << value << "\n";
srcseq_ptr_++;
}
while( '0' <= srcseq_[srcseq_ptr_] && srcseq_[srcseq_ptr_] <= '9' );
std::cerr << "[get_number] returning " << value << "\n";
return value;
}