diff --git a/d7/d75/postfix__evaluation_8cpp.html b/d7/d75/postfix__evaluation_8cpp.html index 5817d51e0..2a209223e 100644 --- a/d7/d75/postfix__evaluation_8cpp.html +++ b/d7/d75/postfix__evaluation_8cpp.html @@ -153,6 +153,10 @@ Functions static void test_function_2 ()  Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}.
static void test_function_3 () +static void test_single_input () +static void test_not_enough_operands () +static void test_not_enough_operands_empty_input () +static void test_too_many_operands () int main ()  Main function.
@@ -288,18 +292,22 @@ Functions

Main function.

Returns
0 on exit
-

Definition at line 151 of file postfix_evaluation.cpp.

-
151 {
-
152 test_function_1();
-
153 test_function_2();
-
154 test_function_3();
-
155
-
156 std::cout << "\nTest implementations passed!\n";
-
157
-
158 return 0;
-
159}
-
static void test_function_2()
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}...
-
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
+

Definition at line 202 of file postfix_evaluation.cpp.

+
202 {
+ + +
205 test_function_3();
+
206 test_single_input();
+
207 test_not_enough_operands();
+
208 test_not_enough_operands_empty_input();
+
209 test_too_many_operands();
+
210
+
211 std::cout << "\nTest implementations passed!\n";
+
212
+
213 return 0;
+
214}
+
static void test_function_2()
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}...
+
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
@@ -327,30 +335,28 @@ Functions
Returns
stack[stackTop] returns the top value from the stack
-

Definition at line 86 of file postfix_evaluation.cpp.

-
86 {
-
87 std::stack<float> stack;
-
88
-
89 for (const auto &scan : input) {
-
90 if (is_number(scan)) {
-
91 stack.push(std::stof(scan));
-
92
-
93 } else {
-
94 const float op2 = stack.top();
-
95 stack.pop();
-
96 const float op1 = stack.top();
-
97 stack.pop();
-
98
-
99 evaluate(op1, op2, scan, stack);
-
100 }
-
101 }
-
102
-
103 std::cout << stack.top() << "\n";
-
104
-
105 return stack.top();
-
106}
-
void pop()
Definition stack.hpp:62
-
value_type top() const
Definition stack.hpp:56
+

Definition at line 97 of file postfix_evaluation.cpp.

+
97 {
+
98 std::stack<float> stack;
+
99
+
100 for (const auto &scan : input) {
+
101 if (is_number(scan)) {
+
102 stack.push(std::stof(scan));
+
103
+
104 } else {
+
105 const auto op2 = remove_from_stack(stack);
+
106 const auto op1 = remove_from_stack(stack);
+
107
+
108 evaluate(op1, op2, scan, stack);
+
109 }
+
110 }
+
111
+
112 const auto res = remove_from_stack(stack);
+
113 if (!stack.empty()) {
+
114 throw std::invalid_argument("Too many operands");
+
115 }
+
116 return res;
+
117}
char stack[MAX]
bool is_number(const std::string &s)
Checks if scanned string is a number.
@@ -382,15 +388,15 @@ Functions

Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.

Returns
none
-

Definition at line 115 of file postfix_evaluation.cpp.

-
115 {
-
116 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
-
117
- -
119
-
120 assert(answer == -4);
-
121}
-
float postfix_evaluation(const std::vector< std::string > &input)
Postfix Evaluation algorithm to compute the value from given input array.
+

Definition at line 126 of file postfix_evaluation.cpp.

+
126 {
+
127 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
+
128
+ +
130
+
131 assert(answer == -4);
+
132}
+
float postfix_evaluation(const std::vector< std::string > &input)
Postfix Evaluation algorithm to compute the value from given input array.
@@ -420,14 +426,14 @@ Functions

Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}.

Returns
none
-

Definition at line 128 of file postfix_evaluation.cpp.

-
128 {
-
129 std::vector<std::string> input = {"100", "200", "+", "2", "/",
-
130 "5", "*", "7", "+"};
-
131 float answer = others::postfix_expression::postfix_evaluation(input);
-
132
-
133 assert(answer == 757);
-
134}
+

Definition at line 139 of file postfix_evaluation.cpp.

+
139 {
+
140 std::vector<std::string> input = {"100", "200", "+", "2", "/",
+
141 "5", "*", "7", "+"};
+ +
143
+
144 assert(answer == 757);
+
145}
@@ -454,17 +460,161 @@ Functions
-

Definition at line 136 of file postfix_evaluation.cpp.

-
136 {
-
137 std::vector<std::string> input = {
-
138 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
-
139 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
-
140 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
-
141 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
- -
143
-
144 assert(answer == 22);
-
145}
+

Definition at line 147 of file postfix_evaluation.cpp.

+
147 {
+
148 std::vector<std::string> input = {
+
149 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+
150 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+
151 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
+
152 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
+ +
154
+
155 assert(answer == 22);
+
156}
+
+
+
+ +

◆ test_not_enough_operands()

+ +
+
+ + + + + +
+ + + + + + + +
void test_not_enough_operands ()
+
+static
+
+ +

Definition at line 165 of file postfix_evaluation.cpp.

+
165 {
+
166 std::vector<std::string> input = {"+"};
+
167 bool throws = false;
+
168 try {
+ +
170 } catch (std::invalid_argument &) {
+
171 throws = true;
+
172 }
+
173 assert(throws);
+
174}
+
+
+
+ +

◆ test_not_enough_operands_empty_input()

+ +
+
+ + + + + +
+ + + + + + + +
void test_not_enough_operands_empty_input ()
+
+static
+
+ +

Definition at line 176 of file postfix_evaluation.cpp.

+
176 {
+
177 std::vector<std::string> input = {};
+
178 bool throws = false;
+
179 try {
+ +
181 } catch (std::invalid_argument &) {
+
182 throws = true;
+
183 }
+
184 assert(throws);
+
185}
+
+
+
+ +

◆ test_single_input()

+ +
+
+ + + + + +
+ + + + + + + +
void test_single_input ()
+
+static
+
+ +

Definition at line 158 of file postfix_evaluation.cpp.

+
158 {
+
159 std::vector<std::string> input = {"1"};
+ +
161
+
162 assert(answer == 1);
+
163}
+
+
+
+ +

◆ test_too_many_operands()

+ +
+
+ + + + + +
+ + + + + + + +
void test_too_many_operands ()
+
+static
+
+ +

Definition at line 187 of file postfix_evaluation.cpp.

+
187 {
+
188 std::vector<std::string> input = {"1", "2"};
+
189 bool throws = false;
+
190 try {
+ +
192 } catch (std::invalid_argument &) {
+
193 throws = true;
+
194 }
+
195 assert(throws);
+
196}
diff --git a/d7/d75/postfix__evaluation_8cpp_source.html b/d7/d75/postfix__evaluation_8cpp_source.html index ebf354249..e809e8316 100644 --- a/d7/d75/postfix__evaluation_8cpp_source.html +++ b/d7/d75/postfix__evaluation_8cpp_source.html @@ -165,74 +165,129 @@ $(function(){initNavTree('d7/d75/postfix__evaluation_8cpp_source.html','../../',
77 }
78}
-
79
-
-
86float postfix_evaluation(const std::vector<std::string> &input) {
-
87 std::stack<float> stack;
-
88
-
89 for (const auto &scan : input) {
-
90 if (is_number(scan)) {
-
91 stack.push(std::stof(scan));
-
92
-
93 } else {
-
94 const float op2 = stack.top();
-
95 stack.pop();
-
96 const float op1 = stack.top();
-
97 stack.pop();
-
98
-
99 evaluate(op1, op2, scan, stack);
-
100 }
-
101 }
-
102
-
103 std::cout << stack.top() << "\n";
-
104
-
105 return stack.top();
-
106}
+
79
+
80namespace {
+
81float remove_from_stack(std::stack<float> &stack) {
+
82 if (stack.empty()) {
+
83 throw std::invalid_argument("Not enough operands");
+
84 }
+
85 const auto res = stack.top();
+
86 stack.pop();
+
87 return res;
+
88}
+
89} // namespace
+
90
+
+
97float postfix_evaluation(const std::vector<std::string> &input) {
+
98 std::stack<float> stack;
+
99
+
100 for (const auto &scan : input) {
+
101 if (is_number(scan)) {
+
102 stack.push(std::stof(scan));
+
103
+
104 } else {
+
105 const auto op2 = remove_from_stack(stack);
+
106 const auto op1 = remove_from_stack(stack);
+
107
+
108 evaluate(op1, op2, scan, stack);
+
109 }
+
110 }
+
111
+
112 const auto res = remove_from_stack(stack);
+
113 if (!stack.empty()) {
+
114 throw std::invalid_argument("Too many operands");
+
115 }
+
116 return res;
+
117}
-
107} // namespace postfix_expression
-
108} // namespace others
-
109
-
-
115static void test_function_1() {
-
116 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
-
117
- -
119
-
120 assert(answer == -4);
-
121}
+
118} // namespace postfix_expression
+
119} // namespace others
+
120
+
+
126static void test_function_1() {
+
127 std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
+
128
+ +
130
+
131 assert(answer == -4);
+
132}
-
122
-
-
128static void test_function_2() {
-
129 std::vector<std::string> input = {"100", "200", "+", "2", "/",
-
130 "5", "*", "7", "+"};
- -
132
-
133 assert(answer == 757);
-
134}
-
-
135
-
136static void test_function_3() {
-
137 std::vector<std::string> input = {
-
138 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
-
139 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
-
140 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
-
141 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
+
133
+
+
139static void test_function_2() {
+
140 std::vector<std::string> input = {"100", "200", "+", "2", "/",
+
141 "5", "*", "7", "+"};
143
-
144 assert(answer == 22);
+
144 assert(answer == 757);
145}
-
146
-
-
151int main() {
- - -
154 test_function_3();
-
155
-
156 std::cout << "\nTest implementations passed!\n";
+
+
146
+
147static void test_function_3() {
+
148 std::vector<std::string> input = {
+
149 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+
150 "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
+
151 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
+
152 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
+ +
154
+
155 assert(answer == 22);
+
156}
157
-
158 return 0;
-
159}
+
158static void test_single_input() {
+
159 std::vector<std::string> input = {"1"};
+ +
161
+
162 assert(answer == 1);
+
163}
+
164
+
165static void test_not_enough_operands() {
+
166 std::vector<std::string> input = {"+"};
+
167 bool throws = false;
+
168 try {
+ +
170 } catch (std::invalid_argument &) {
+
171 throws = true;
+
172 }
+
173 assert(throws);
+
174}
+
175
+
176static void test_not_enough_operands_empty_input() {
+
177 std::vector<std::string> input = {};
+
178 bool throws = false;
+
179 try {
+ +
181 } catch (std::invalid_argument &) {
+
182 throws = true;
+
183 }
+
184 assert(throws);
+
185}
+
186
+
187static void test_too_many_operands() {
+
188 std::vector<std::string> input = {"1", "2"};
+
189 bool throws = false;
+
190 try {
+ +
192 } catch (std::invalid_argument &) {
+
193 throws = true;
+
194 }
+
195 assert(throws);
+
196}
+
197
+
+
202int main() {
+ + +
205 test_function_3();
+
206 test_single_input();
+
207 test_not_enough_operands();
+
208 test_not_enough_operands_empty_input();
+
209 test_too_many_operands();
+
210
+
211 std::cout << "\nTest implementations passed!\n";
+
212
+
213 return 0;
+
214}
for std::invalid_argument
Definition stack.hpp:19
void pop()
Definition stack.hpp:62
@@ -242,10 +297,10 @@ $(function(){initNavTree('d7/d75/postfix__evaluation_8cpp_source.html','../../',
Functions for Postfix Expression algorithm.
void evaluate(float a, float b, const std::string &operation, std::stack< float > &stack)
Evaluate answer using given last two operands from and operation.
bool is_number(const std::string &s)
Checks if scanned string is a number.
-
float postfix_evaluation(const std::vector< std::string > &input)
Postfix Evaluation algorithm to compute the value from given input array.
-
static void test_function_2()
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}...
-
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
-
int main()
Main function.
+
float postfix_evaluation(const std::vector< std::string > &input)
Postfix Evaluation algorithm to compute the value from given input array.
+
static void test_function_2()
Test function 2 with input array {'100', '200', '+', '2', '/', '5', '*', '7', '+'}...
+
static void test_function_1()
Test function 1 with input array {'2', '3', '1', '*', '+', '9', '-'}.
+
int main()
Main function.