From b909747dca4b9638d122faadb96b71f84f40342d Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Sun, 30 Sep 2018 00:05:44 +0200 Subject: [PATCH] Add files via upload Added better Ans support Added Pi and e Improved decimal numbers Added help page, accesible via --- CAS.exe | Bin 31744 -> 31743 bytes Console.cpp | 31 ++++++++++++++++++--- Console.hpp | 4 +-- Error.hpp | 1 + Interpreter.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++------ Interpreter.hpp | 2 +- 6 files changed, 95 insertions(+), 15 deletions(-) diff --git a/CAS.exe b/CAS.exe index ac2aa94dbd1b7582c62bdd66c00f8ef6ace6401f..a8b3215eafb3beb6d94e3a132bc21b45f6bcec2e 100644 GIT binary patch delta 14 WcmZqp!TA3>D07riZTL1t6 diff --git a/Console.cpp b/Console.cpp index 5dbdb66..63e4151 100644 --- a/Console.cpp +++ b/Console.cpp @@ -3,8 +3,9 @@ #include #include +#include -char* Console::input = new char[INPUT_BUFFER_SIZE]; +std::string Console::input = ""; void Console::run() @@ -14,8 +15,6 @@ void Console::run() awaitInput(); // Waits for input handleInput(); // Handles input } - - delete[] input; // After process is terminated, release the pointer } @@ -23,13 +22,37 @@ void Console::run() void Console::awaitInput() { std::cout << ">>>"; - std::cin >> std::setw(INPUT_BUFFER_SIZE) >> input; // Await input + std::cin >> input; // Await input } void Console::handleInput() { + for (char c : input) + { + c = tolower(c); + } + + if (input == "help") + { + std::cout << "This is a CAS in the Windows Console. You can enter any valid mathematical expression and it will print the answer." << std::endl; + std::cout << "This program follows PEMDAS, but does not support parantheses." << std::endl << std::endl; + std::cout << "Any expression like <4-7+42.6*7> is valid. No need to put a <=>. The program can't solve for variables" << std::endl << std::endl; + std::cout << "Available operations:" << std::endl; + std::cout << "+ ~ Addition" << std::endl + << "- ~ Subtraction" << std::endl + << "* ~ Multiplication" << std::endl + << "/ ~ Division" << std::endl + << "^ ~ Exponents" << std::endl + << "_ ~ Roots" << std::endl << std::endl; + std::cout << "You can use the following symbols as well: " << std::endl; + std::cout << "a ~ Result of the last calculation" << std::endl + << "p ~ Pi" << std::endl + << "e ~ Euler's number" << std::endl << std::endl; + return; + } + double result = Interpreter::interpret(input); std::cout << result << std::endl << std::endl; } diff --git a/Console.hpp b/Console.hpp index 3ac54b5..93eaeca 100644 --- a/Console.hpp +++ b/Console.hpp @@ -1,6 +1,6 @@ #pragma once -const constexpr unsigned int INPUT_BUFFER_SIZE = 256; +#include class Console { @@ -12,7 +12,7 @@ public: static void run(); private: - static char* input; + static std::string input; /////////////////////////////////////////////////////////// /// \brief Waits for user input and writes it into a variable diff --git a/Error.hpp b/Error.hpp index 0c1c596..bf2f5fb 100644 --- a/Error.hpp +++ b/Error.hpp @@ -10,6 +10,7 @@ #define TOKEN_LIST_ERROR INTERPRETER_ERROR << "Failed to create Token list +++" << std::endl; return false #define TOO_MANY_OPERATORS INTERPRETER_ERROR << "Found multiple operators in sequence +++" << std::endl; return false #define ILLEGAL_OPERATOR_POSITION INTERPRETER_ERROR << "An operator was found in an illegal position +++" << std::endl; return false +#define UNEXPECTED_SYMBOL INTERPRETER_ERROR << " symbol must be followed by an operator symbol +++" << std::endl; return false #define INTEGER_TOO_BIG(x) INTEGER_ERROR << "Integer <" << (x) << "> cannot be converted to int +++" << std::endl; return false; #define ENDS_ON_DECIMAL_POINT INTEGER_ERROR << "Integers cannot end with a decimal point +++" << std::endl; return false diff --git a/Interpreter.cpp b/Interpreter.cpp index a4f6318..c1d07f2 100644 --- a/Interpreter.cpp +++ b/Interpreter.cpp @@ -3,9 +3,12 @@ #include +#define EULER 2.718281828459045 +#define PI 3.1415926535 + double Interpreter::result = 0; bool Interpreter::justNumber = false; -std::string Interpreter::numbers = "0123456789Aa.,"; +std::string Interpreter::numbers = "0123456789Aaep.,"; std::string Interpreter::operators = "+-*/^_"; std::string Interpreter::string = ""; std::map Interpreter::operation_order = { @@ -17,7 +20,7 @@ std::map Interpreter::operation_order = { {'_', 2} }; -double Interpreter::interpret(char * _string) +double Interpreter::interpret(std::string _string) { string = _string; justNumber = false; @@ -77,17 +80,59 @@ bool Interpreter::createSymbolList(std::vector& symbols) { bool number = false; bool hasOperator = false; + bool expectOperator = false; std::string num = ""; for (char c : string) { if (std::find(std::begin(numbers), std::end(numbers), c) != std::end(numbers)) { - if (c == 'A' || c == 'a') + if (expectOperator) { - //printf("%f", result); - symbols.push_back(new Number(result)); + UNEXPECTED_SYMBOL; } + + if (number) + { + if (c == 'A' || c == 'a') + { + symbols.push_back(new Number(std::stod(num) * result)); + expectOperator = true; + } + + if (c == 'e') + { + symbols.push_back(new Number(std::stod(num) * EULER)); + expectOperator = true; + } + + if (c == 'p') + { + symbols.push_back(new Number(std::stod(num) * PI)); + expectOperator = true; + } + } + else + { + if (c == 'A' || c == 'a') + { + symbols.push_back(new Number(result)); + expectOperator = true; + } + + if (c == 'e') + { + symbols.push_back(new Number(EULER)); + expectOperator = true; + } + + if (c == 'p') + { + symbols.push_back(new Number(PI)); + expectOperator = true; + } + } + if (c == '.' || c == ',') { num += '.'; @@ -101,15 +146,16 @@ bool Interpreter::createSymbolList(std::vector& symbols) if (std::find(std::begin(operators), std::end(operators), c) != std::end(operators)) { + hasOperator = true; if (number == true) { //std::cout << "++++++++++++" << num << "++++++++++++++" << std::endl; - if (num != "") + if (num != "" && !expectOperator) { try { - if (number == true) symbols.push_back(new Number(std::stod(num))); + symbols.push_back(new Number(std::stod(num))); } catch (...) { @@ -131,6 +177,8 @@ bool Interpreter::createSymbolList(std::vector& symbols) TOO_MANY_OPERATORS; } } + + expectOperator = false; } } @@ -140,7 +188,7 @@ bool Interpreter::createSymbolList(std::vector& symbols) ILLEGAL_OPERATOR_POSITION; } - if (num != "") + if (num != "" && !expectOperator) { try { @@ -159,6 +207,14 @@ bool Interpreter::createSymbolList(std::vector& symbols) return true; } + /*for (auto it : symbols) + { + if(it->isNumber()) + std::cout << it->value() << std::endl; + else + std::cout << (char)it->value() << std::endl; + }*/ + return true; } diff --git a/Interpreter.hpp b/Interpreter.hpp index d0e52c0..29135ae 100644 --- a/Interpreter.hpp +++ b/Interpreter.hpp @@ -8,7 +8,7 @@ class Interpreter { public: - static double interpret(char* _string); + static double interpret(std::string _string); private: static bool scanForErrors();