Add files via upload
Added better Ans support Added Pi and e Improved decimal numbers Added help page, accesible via <help>
This commit is contained in:
parent
75fc193a65
commit
b909747dca
31
Console.cpp
31
Console.cpp
|
@ -3,8 +3,9 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
char* Console::input = new char[INPUT_BUFFER_SIZE];
|
std::string Console::input = "";
|
||||||
|
|
||||||
|
|
||||||
void Console::run()
|
void Console::run()
|
||||||
|
@ -14,8 +15,6 @@ void Console::run()
|
||||||
awaitInput(); // Waits for input
|
awaitInput(); // Waits for input
|
||||||
handleInput(); // Handles input
|
handleInput(); // Handles input
|
||||||
}
|
}
|
||||||
|
|
||||||
delete[] input; // After process is terminated, release the pointer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,13 +22,37 @@ void Console::run()
|
||||||
void Console::awaitInput()
|
void Console::awaitInput()
|
||||||
{
|
{
|
||||||
std::cout << ">>>";
|
std::cout << ">>>";
|
||||||
std::cin >> std::setw(INPUT_BUFFER_SIZE) >> input; // Await input
|
std::cin >> input; // Await input
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Console::handleInput()
|
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);
|
double result = Interpreter::interpret(input);
|
||||||
std::cout << result << std::endl << std::endl;
|
std::cout << result << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
const constexpr unsigned int INPUT_BUFFER_SIZE = 256;
|
#include <string>
|
||||||
|
|
||||||
class Console
|
class Console
|
||||||
{
|
{
|
||||||
|
@ -12,7 +12,7 @@ public:
|
||||||
static void run();
|
static void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static char* input;
|
static std::string input;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////
|
||||||
/// \brief Waits for user input and writes it into a variable
|
/// \brief Waits for user input and writes it into a variable
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#define TOKEN_LIST_ERROR INTERPRETER_ERROR << "Failed to create Token list +++" << std::endl; return false
|
#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 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 ILLEGAL_OPERATOR_POSITION INTERPRETER_ERROR << "An operator was found in an illegal position +++" << std::endl; return false
|
||||||
|
#define UNEXPECTED_SYMBOL INTERPRETER_ERROR << "<ans> 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 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
|
#define ENDS_ON_DECIMAL_POINT INTEGER_ERROR << "Integers cannot end with a decimal point +++" << std::endl; return false
|
||||||
|
|
|
@ -3,9 +3,12 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#define EULER 2.718281828459045
|
||||||
|
#define PI 3.1415926535
|
||||||
|
|
||||||
double Interpreter::result = 0;
|
double Interpreter::result = 0;
|
||||||
bool Interpreter::justNumber = false;
|
bool Interpreter::justNumber = false;
|
||||||
std::string Interpreter::numbers = "0123456789Aa.,";
|
std::string Interpreter::numbers = "0123456789Aaep.,";
|
||||||
std::string Interpreter::operators = "+-*/^_";
|
std::string Interpreter::operators = "+-*/^_";
|
||||||
std::string Interpreter::string = "";
|
std::string Interpreter::string = "";
|
||||||
std::map<char, unsigned int> Interpreter::operation_order = {
|
std::map<char, unsigned int> Interpreter::operation_order = {
|
||||||
|
@ -17,7 +20,7 @@ std::map<char, unsigned int> Interpreter::operation_order = {
|
||||||
{'_', 2}
|
{'_', 2}
|
||||||
};
|
};
|
||||||
|
|
||||||
double Interpreter::interpret(char * _string)
|
double Interpreter::interpret(std::string _string)
|
||||||
{
|
{
|
||||||
string = _string;
|
string = _string;
|
||||||
justNumber = false;
|
justNumber = false;
|
||||||
|
@ -77,17 +80,59 @@ bool Interpreter::createSymbolList(std::vector<Symbol*>& symbols)
|
||||||
{
|
{
|
||||||
bool number = false;
|
bool number = false;
|
||||||
bool hasOperator = false;
|
bool hasOperator = false;
|
||||||
|
bool expectOperator = false;
|
||||||
std::string num = "";
|
std::string num = "";
|
||||||
|
|
||||||
for (char c : string)
|
for (char c : string)
|
||||||
{
|
{
|
||||||
if (std::find(std::begin(numbers), std::end(numbers), c) != std::end(numbers))
|
if (std::find(std::begin(numbers), std::end(numbers), c) != std::end(numbers))
|
||||||
{
|
{
|
||||||
if (c == 'A' || c == 'a')
|
if (expectOperator)
|
||||||
{
|
{
|
||||||
//printf("%f", result);
|
UNEXPECTED_SYMBOL;
|
||||||
symbols.push_back(new Number(result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 == ',')
|
if (c == '.' || c == ',')
|
||||||
{
|
{
|
||||||
num += '.';
|
num += '.';
|
||||||
|
@ -101,15 +146,16 @@ bool Interpreter::createSymbolList(std::vector<Symbol*>& symbols)
|
||||||
|
|
||||||
if (std::find(std::begin(operators), std::end(operators), c) != std::end(operators))
|
if (std::find(std::begin(operators), std::end(operators), c) != std::end(operators))
|
||||||
{
|
{
|
||||||
|
|
||||||
hasOperator = true;
|
hasOperator = true;
|
||||||
if (number == true)
|
if (number == true)
|
||||||
{
|
{
|
||||||
//std::cout << "++++++++++++" << num << "++++++++++++++" << std::endl;
|
//std::cout << "++++++++++++" << num << "++++++++++++++" << std::endl;
|
||||||
if (num != "")
|
if (num != "" && !expectOperator)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (number == true) symbols.push_back(new Number(std::stod(num)));
|
symbols.push_back(new Number(std::stod(num)));
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
|
@ -131,6 +177,8 @@ bool Interpreter::createSymbolList(std::vector<Symbol*>& symbols)
|
||||||
TOO_MANY_OPERATORS;
|
TOO_MANY_OPERATORS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expectOperator = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,7 +188,7 @@ bool Interpreter::createSymbolList(std::vector<Symbol*>& symbols)
|
||||||
ILLEGAL_OPERATOR_POSITION;
|
ILLEGAL_OPERATOR_POSITION;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num != "")
|
if (num != "" && !expectOperator)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -159,6 +207,14 @@ bool Interpreter::createSymbolList(std::vector<Symbol*>& symbols)
|
||||||
return true;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
class Interpreter
|
class Interpreter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static double interpret(char* _string);
|
static double interpret(std::string _string);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool scanForErrors();
|
static bool scanForErrors();
|
||||||
|
|
Loading…
Reference in a new issue