From 67668448140e663e67bd889c6746e016cfee8259 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 14 Jan 2024 16:28:17 -0700 Subject: [PATCH] Reformat with clang-format --- src/infiz/infiz.cpp | 37 ++-- src/libinfiz/Evaluator.cpp | 291 +++++++++++++++---------------- src/libinfiz/RationalNumber.cpp | 68 ++++---- src/libinfiz/RationalNumber.h | 29 +-- src/libinfiz/Stack.h | 56 +++--- src/libinfiz/StringTokenizer.cpp | 120 ++++++------- src/libinfiz/StringTokenizer.h | 26 +-- test/tests.cpp | 65 ++++--- 8 files changed, 337 insertions(+), 355 deletions(-) diff --git a/src/infiz/infiz.cpp b/src/infiz/infiz.cpp index d351224..5f14689 100644 --- a/src/infiz/infiz.cpp +++ b/src/infiz/infiz.cpp @@ -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 #include +#include 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(input),max_line-1, '\n'); + char input[max_line];// NOLINT - while (std::cin.good()) { - StringTokenizer tokenizer(static_cast(input)); - const RationalNumber answer = evaluateExpression(tokenizer); - std::cout << "answer: "; + std::cin.getline(static_cast(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(input)); + const RationalNumber answer = evaluateExpression(tokenizer); + std::cout << "answer: "; - std::cin.getline(static_cast(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(input), max_line - 1, '\n'); + } +} diff --git a/src/libinfiz/Evaluator.cpp b/src/libinfiz/Evaluator.cpp index e57ea62..1c9f627 100644 --- a/src/libinfiz/Evaluator.cpp +++ b/src/libinfiz/Evaluator.cpp @@ -1,157 +1,156 @@ -#include #include "Evaluator.hpp" +#include -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 &numbers, Stack &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 &numbers, Stack &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 operators; - Stack 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.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 operators; + Stack 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.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 + } } - diff --git a/src/libinfiz/RationalNumber.cpp b/src/libinfiz/RationalNumber.cpp index 09ab5e1..00ef4e1 100644 --- a/src/libinfiz/RationalNumber.cpp +++ b/src/libinfiz/RationalNumber.cpp @@ -1,55 +1,51 @@ -//RationalNumber.h +// RationalNumber.h #include "RationalNumber.h" -RationalNumber::RationalNumber(int num, int den) // NOLINT - : numerator(num), denominator(den) -{ -} +RationalNumber::RationalNumber(int num, int den)// NOLINT + : numerator(num), denominator(den) +{} -RationalNumber RationalNumber::operator/(const RationalNumber &rhs) const { - return RationalNumber( // NOLINT - numerator * rhs.getDenominator(), - denominator * rhs.getNumerator() - ); +RationalNumber RationalNumber::operator/(const RationalNumber &rhs) const +{ + return RationalNumber(// NOLINT + numerator * rhs.getDenominator(), + denominator * rhs.getNumerator()); } -RationalNumber RationalNumber::operator*(const RationalNumber &rhs) const { - return RationalNumber( // NOLINT - numerator * rhs.getNumerator(), - denominator * rhs.getDenominator()); +RationalNumber RationalNumber::operator*(const RationalNumber &rhs) const +{ + return RationalNumber(// NOLINT + numerator * rhs.getNumerator(), + denominator * rhs.getDenominator()); } -RationalNumber RationalNumber::operator+(const RationalNumber &rhs) const { - const int denom = denominator * rhs.getDenominator(); - const int new_numerator = this->numerator * rhs.getDenominator(); - const int numer = new_numerator + (rhs.getNumerator() * denominator); +RationalNumber RationalNumber::operator+(const RationalNumber &rhs) const +{ + const int denom = denominator * rhs.getDenominator(); + const int new_numerator = this->numerator * rhs.getDenominator(); + const int numer = new_numerator + (rhs.getNumerator() * denominator); - return RationalNumber(numer, denom); // NOLINT + return RationalNumber(numer, denom);// NOLINT } -RationalNumber RationalNumber::operator-(const RationalNumber &rhs) const { - const int denom = denominator * rhs.getDenominator(); - - const int new_numerator = this->numerator * rhs.getDenominator(); - const int numer = new_numerator - (rhs.getNumerator() * denominator); +RationalNumber RationalNumber::operator-(const RationalNumber &rhs) const +{ + const int denom = denominator * rhs.getDenominator(); - return RationalNumber(numer, denom); // NOLINT -} + const int new_numerator = this->numerator * rhs.getDenominator(); + const int numer = new_numerator - (rhs.getNumerator() * denominator); -int RationalNumber::getDenominator() const { - return denominator; + return RationalNumber(numer, denom);// NOLINT } -RationalNumber RationalNumber::operator-() const { - return RationalNumber(numerator * -1, denominator); // NOLINT -} +int RationalNumber::getDenominator() const { return denominator; } -int RationalNumber::getNumerator() const { - return numerator; +RationalNumber RationalNumber::operator-() const +{ + return RationalNumber(numerator * -1, denominator);// NOLINT } -float RationalNumber::getFloat() const { - return ( (static_cast(numerator)) / (static_cast(denominator)) ); -} +int RationalNumber::getNumerator() const { return numerator; } +float RationalNumber::getFloat() const { return ((static_cast(numerator)) / (static_cast(denominator))); } diff --git a/src/libinfiz/RationalNumber.h b/src/libinfiz/RationalNumber.h index c23e9e9..93dbf61 100644 --- a/src/libinfiz/RationalNumber.h +++ b/src/libinfiz/RationalNumber.h @@ -1,30 +1,31 @@ #ifndef INFIZ_RATIONAL_NUMBER_H #define INFIZ_RATIONAL_NUMBER_H -//RationalNumber.h +// RationalNumber.h /** * A Class that stores the numerator and denominator * of a rational number. */ -class RationalNumber { +class RationalNumber +{ public: - RationalNumber(int num, int den); - RationalNumber operator/(const RationalNumber &rhs) const; - RationalNumber operator*(const RationalNumber &rhs) const; - RationalNumber operator+(const RationalNumber &rhs) const; - RationalNumber operator-(const RationalNumber &rhs) const; - RationalNumber operator-() const; + RationalNumber(int num, int den); + RationalNumber operator/(const RationalNumber &rhs) const; + RationalNumber operator*(const RationalNumber &rhs) const; + RationalNumber operator+(const RationalNumber &rhs) const; + RationalNumber operator-(const RationalNumber &rhs) const; + RationalNumber operator-() const; - int getNumerator() const; - int getDenominator() const; + int getNumerator() const; + int getDenominator() const; + + float getFloat() const; - float getFloat() const; - private: - int numerator; - int denominator; + int numerator; + int denominator; }; #endif \ No newline at end of file diff --git a/src/libinfiz/Stack.h b/src/libinfiz/Stack.h index d44ad6f..a14ce76 100644 --- a/src/libinfiz/Stack.h +++ b/src/libinfiz/Stack.h @@ -3,42 +3,42 @@ // Stack.h -#include #include #include +#include /** -* A Class that allows allows void * to be -* pushed onto an popped off of a stack. -*/ + * A Class that allows allows void * to be + * pushed onto an popped off of a stack. + */ -template -class Stack +template class Stack { public: - Stack(){} - - - Contained pop() { - assert(!data.empty()); - Contained toReturn = data.back(); - data.pop_back(); - return toReturn; - } - - void push(const Contained &newElem) { - data.push_back(newElem); - } - - const Contained * peek() const { - if (data.empty()) { - return NULL; - } else { - return &data.back(); - } - } + Stack() {} + + + Contained pop() + { + assert(!data.empty()); + Contained toReturn = data.back(); + data.pop_back(); + return toReturn; + } + + void push(const Contained &newElem) { data.push_back(newElem); } + + const Contained *peek() const + { + if (data.empty()) { + return NULL; + } else { + return &data.back(); + } + } + private: - std::vector data; + std::vector data; }; #endif \ No newline at end of file diff --git a/src/libinfiz/StringTokenizer.cpp b/src/libinfiz/StringTokenizer.cpp index e8676c2..1536b39 100644 --- a/src/libinfiz/StringTokenizer.cpp +++ b/src/libinfiz/StringTokenizer.cpp @@ -3,88 +3,76 @@ #include "StringTokenizer.h" #include -StringTokenizer::StringTokenizer(const std::string &n_string) // NOLINT +StringTokenizer::StringTokenizer(const std::string &n_string)// NOLINT : string(n_string), currentOffset(0), moreTokens(true) +{} + + +std::string StringTokenizer::nextToken() { -} + while (currentOffset < string.size() && isWhiteSpace(string[currentOffset])) { ++currentOffset; } + const size_type endOfToken = findTokenEnd(currentOffset, string); -std::string StringTokenizer::nextToken() -{ - while (currentOffset