Skip to content

Commit

Permalink
Reformat with clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
lefticus committed Jan 14, 2024
1 parent 49cb38d commit 6766844
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 355 deletions.
37 changes: 18 additions & 19 deletions src/infiz/infiz.cpp
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');
}
}
291 changes: 145 additions & 146 deletions src/libinfiz/Evaluator.cpp
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
}
}

Loading

0 comments on commit 6766844

Please sign in to comment.