generated from cpp-best-practices/cmake_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
337 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
//infiz.cpp | ||
// infiz.cpp | ||
|
||
#include "../libinfiz/Evaluator.hpp" | ||
#include "../libinfiz/RationalNumber.h" | ||
#include "../libinfiz/Stack.h" | ||
#include "../libinfiz/StringTokenizer.h" | ||
|
||
#include <iostream> | ||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
const int max_line = 255; | ||
|
||
int main(int /*argc*/, char * /*args*/[]) { | ||
|
||
char input[max_line]; // NOLINT | ||
int main(int /*argc*/, char * /*args*/[]) | ||
{ | ||
|
||
std::cin.getline(static_cast<char*>(input),max_line-1, '\n'); | ||
char input[max_line];// NOLINT | ||
|
||
while (std::cin.good()) { | ||
StringTokenizer tokenizer(static_cast<const char*>(input)); | ||
const RationalNumber answer = evaluateExpression(tokenizer); | ||
std::cout << "answer: "; | ||
std::cin.getline(static_cast<char *>(input), max_line - 1, '\n'); | ||
|
||
if (answer.getDenominator() == 1) { | ||
std::cout << answer.getNumerator() << '\n'; | ||
} else { | ||
std::cout << answer.getNumerator() << '/' << answer.getDenominator() << " (" << answer.getFloat() | ||
<< ")" << '\n'; | ||
} | ||
while (std::cin.good()) { | ||
StringTokenizer tokenizer(static_cast<const char *>(input)); | ||
const RationalNumber answer = evaluateExpression(tokenizer); | ||
std::cout << "answer: "; | ||
|
||
std::cin.getline(static_cast<char *>(input),max_line-1, '\n'); | ||
} | ||
} | ||
if (answer.getDenominator() == 1) { | ||
std::cout << answer.getNumerator() << '\n'; | ||
} else { | ||
std::cout << answer.getNumerator() << '/' << answer.getDenominator() << " (" << answer.getFloat() << ")" << '\n'; | ||
} | ||
|
||
std::cin.getline(static_cast<char *>(input), max_line - 1, '\n'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,156 @@ | ||
|
||
#include <cstdlib> | ||
#include "Evaluator.hpp" | ||
#include <cstdlib> | ||
|
||
|
||
int precedence(Operators input) { | ||
switch (input) { | ||
case CLOSE_PAREN: | ||
return 3; | ||
case PLUS_SIGN: | ||
case MINUS_SIGN: | ||
return 1; | ||
case MULTIPLY_SIGN: | ||
case DIVIDE_SIGN: | ||
return 2; | ||
case OPEN_PAREN: | ||
return 0; | ||
} | ||
|
||
return 0; | ||
int precedence(Operators input) | ||
{ | ||
switch (input) { | ||
case CLOSE_PAREN: | ||
return 3; | ||
case PLUS_SIGN: | ||
case MINUS_SIGN: | ||
return 1; | ||
case MULTIPLY_SIGN: | ||
case DIVIDE_SIGN: | ||
return 2; | ||
case OPEN_PAREN: | ||
return 0; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
||
void evaluateStacks(Stack<RationalNumber> &numbers, Stack<int> &operators) { | ||
bool eatOpenParen = false; | ||
bool cont = true; | ||
|
||
if ( *operators.peek() == CLOSE_PAREN ) { | ||
eatOpenParen = true; | ||
operators.pop(); | ||
} | ||
|
||
while ( (operators.peek() != NULL) && cont) { // NOLINT | ||
|
||
switch( *operators.peek() ) { | ||
case OPEN_PAREN: | ||
if (eatOpenParen) { | ||
operators.pop(); | ||
cont = false; | ||
} else { | ||
cont = false; | ||
} | ||
|
||
break; | ||
case PLUS_SIGN:{ | ||
operators.pop(); | ||
const RationalNumber operand2 = numbers.pop(); | ||
const RationalNumber operand1 = numbers.pop(); | ||
numbers.push(operand1 + operand2); | ||
break; | ||
} | ||
|
||
case MINUS_SIGN: { | ||
operators.pop(); | ||
numbers.push(-numbers.pop()); | ||
break; | ||
} | ||
|
||
case MULTIPLY_SIGN: { | ||
operators.pop(); | ||
const RationalNumber operand2 = numbers.pop(); | ||
const RationalNumber operand1 = numbers.pop(); | ||
numbers.push(operand1 * operand2); | ||
break; | ||
} | ||
|
||
case DIVIDE_SIGN: { | ||
operators.pop(); | ||
const RationalNumber operand2 = numbers.pop(); | ||
const RationalNumber operand1 = numbers.pop(); | ||
numbers.push(operand1 / operand2); | ||
break; | ||
} | ||
} | ||
} | ||
void evaluateStacks(Stack<RationalNumber> &numbers, Stack<int> &operators) | ||
{ | ||
bool eatOpenParen = false; | ||
bool cont = true; | ||
|
||
if (*operators.peek() == CLOSE_PAREN) { | ||
eatOpenParen = true; | ||
operators.pop(); | ||
} | ||
|
||
while ((operators.peek() != NULL) && cont) {// NOLINT | ||
|
||
switch (*operators.peek()) { | ||
case OPEN_PAREN: | ||
if (eatOpenParen) { | ||
operators.pop(); | ||
cont = false; | ||
} else { | ||
cont = false; | ||
} | ||
|
||
break; | ||
|
||
case PLUS_SIGN: { | ||
operators.pop(); | ||
const RationalNumber operand2 = numbers.pop(); | ||
const RationalNumber operand1 = numbers.pop(); | ||
numbers.push(operand1 + operand2); | ||
break; | ||
} | ||
|
||
case MINUS_SIGN: { | ||
operators.pop(); | ||
numbers.push(-numbers.pop()); | ||
break; | ||
} | ||
|
||
case MULTIPLY_SIGN: { | ||
operators.pop(); | ||
const RationalNumber operand2 = numbers.pop(); | ||
const RationalNumber operand1 = numbers.pop(); | ||
numbers.push(operand1 * operand2); | ||
break; | ||
} | ||
|
||
case DIVIDE_SIGN: { | ||
operators.pop(); | ||
const RationalNumber operand2 = numbers.pop(); | ||
const RationalNumber operand1 = numbers.pop(); | ||
numbers.push(operand1 / operand2); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
RationalNumber evaluateExpression(StringTokenizer &tokenizer) { | ||
Stack<int> operators; | ||
Stack<RationalNumber> numbers; | ||
|
||
while (tokenizer.hasMoreTokens()) { | ||
|
||
std::string next = tokenizer.nextToken(); | ||
|
||
Operators value = PLUS_SIGN; | ||
|
||
if (!next.empty()) { | ||
bool operation = false; | ||
switch(next[0]) { | ||
case '+': | ||
value = PLUS_SIGN; | ||
operation = true; | ||
break; | ||
case '/': | ||
value = DIVIDE_SIGN; | ||
operation = true; | ||
break; | ||
case '-': | ||
value = MINUS_SIGN; | ||
operation = true; | ||
break; | ||
case '*': | ||
value = MULTIPLY_SIGN; | ||
operation = true; | ||
break; | ||
case ')': | ||
value = CLOSE_PAREN; | ||
operation = true; | ||
break; | ||
case '(': | ||
value = OPEN_PAREN; | ||
operation = true; | ||
break; | ||
|
||
default: | ||
operation = false; | ||
numbers.push(RationalNumber(atoi(next.c_str()), 1)); // NOLINT | ||
break; | ||
} | ||
|
||
if (operation) { | ||
switch (value) { | ||
case OPEN_PAREN: | ||
operators.push(value); | ||
break; | ||
case CLOSE_PAREN: | ||
operators.push(value); | ||
evaluateStacks(numbers, operators); | ||
break; | ||
default: | ||
if (operators.peek() != NULL // NOLINT | ||
&& precedence(value) <= precedence(static_cast<Operators>(*operators.peek())) ) { | ||
evaluateStacks(numbers, operators); | ||
} | ||
operators.push(value); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (operators.peek() != NULL) // NOLINT | ||
evaluateStacks(numbers, operators); | ||
|
||
if (numbers.peek() != NULL) { // NOLINT | ||
return *numbers.peek(); | ||
} else { | ||
return RationalNumber(0,0); // NOLINT | ||
} | ||
RationalNumber evaluateExpression(StringTokenizer &tokenizer) | ||
{ | ||
Stack<int> operators; | ||
Stack<RationalNumber> numbers; | ||
|
||
while (tokenizer.hasMoreTokens()) { | ||
|
||
std::string next = tokenizer.nextToken(); | ||
|
||
Operators value = PLUS_SIGN; | ||
|
||
if (!next.empty()) { | ||
bool operation = false; | ||
switch (next[0]) { | ||
case '+': | ||
value = PLUS_SIGN; | ||
operation = true; | ||
break; | ||
case '/': | ||
value = DIVIDE_SIGN; | ||
operation = true; | ||
break; | ||
case '-': | ||
value = MINUS_SIGN; | ||
operation = true; | ||
break; | ||
case '*': | ||
value = MULTIPLY_SIGN; | ||
operation = true; | ||
break; | ||
case ')': | ||
value = CLOSE_PAREN; | ||
operation = true; | ||
break; | ||
case '(': | ||
value = OPEN_PAREN; | ||
operation = true; | ||
break; | ||
|
||
default: | ||
operation = false; | ||
numbers.push(RationalNumber(atoi(next.c_str()), 1));// NOLINT | ||
break; | ||
} | ||
|
||
if (operation) { | ||
switch (value) { | ||
case OPEN_PAREN: | ||
operators.push(value); | ||
break; | ||
case CLOSE_PAREN: | ||
operators.push(value); | ||
evaluateStacks(numbers, operators); | ||
break; | ||
default: | ||
if (operators.peek() != NULL// NOLINT | ||
&& precedence(value) <= precedence(static_cast<Operators>(*operators.peek()))) { | ||
evaluateStacks(numbers, operators); | ||
} | ||
operators.push(value); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (operators.peek() != NULL)// NOLINT | ||
evaluateStacks(numbers, operators); | ||
|
||
if (numbers.peek() != NULL) {// NOLINT | ||
return *numbers.peek(); | ||
} else { | ||
return RationalNumber(0, 0);// NOLINT | ||
} | ||
} | ||
|
Oops, something went wrong.