CPP
CPP
h>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <queue>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <algorithm>
#include <map>
#include <stack>
#include <limits>
class classOperator
public:
classOperator(char&,int&);
int value;
char actualclassOperator;
actualclassOperator = receivedclassOperator;
value = receivedValue;
pointer_To_leftOperand=&receivedleft;
pointer_To_rightOperand=&receivedright;
class Parenthesis
public:
Parenthesis(unsigned int,char);
char typeOfParenthesis;
private:
};
position = receivedPosition;
typeOfParenthesis = parenthesisType;
class Expression
public:
Expression(std::string,bool);
double result;
private:
bool hasBrackets;
//is 'n', then the position of the closing parenthesis should be vectorSize-n in the
//same vector
void getSubExpressions(); //Gets the contents between each parenthesis so that they may be
evaluated
long double evaluate();
std::string expressionString;
};
Expression::Expression(std::string expression, bool sub) //NOTE: being a sub means that it was
surrounded
hasBrackets = false;
expressionString = expression;
containsBrackets();
if (hasBrackets == true)
getParentheses();
getSubExpressions();
evaluate();
{
std::map<char, unsigned int> classOperatorMap;
classOperatorMap['*']=2;
classOperatorMap['/']=2;
classOperatorMap['+']=1;
classOperatorMap['-']=1;
classOperatorMap['^']=2;
classOperatorMap['=']=1;
classOperatorMap['s']=1;
classOperatorMap['i']=1;
classOperatorMap['n']=1;
std::vector<classOperator> classOperators;
int flag = 0;
if(expressionString[index] == 'b')
numberString+= expressionString[index];
flag = 1;
numberString+= expressionString[index];
flag = 2;
numberString+= expressionString[index];
flag = 3;
numberString+= expressionString[index];
flag = 4;
else
numberString+= expressionString[index];
if (expressionString.size() == index)
{
if(flag == 1)
numbers.push_back(number);
numberString="";
if(flag == 2)
numbers.push_back(number);
numberString = "";
if(flag == 3)
number = 3.14;
numbers.push_back(number);
numberString = "";
if(flag == 4)
number = 3;
numbers.push_back(number);
numberString = "";
else
numberString = "";
numbers.push_back(number);
numberString = "";
if(numbers.size() > 2)
{
classOperator tempclassOperator(expressionString[index],value);
classOperators.push_back(tempclassOperator);
else
classOperator tempclassOperator(expressionString[index],value);
classOperators.push_back(tempclassOperator);
if(numbers.size() >= 2)
classOperators[index].addOperands(numbers[index],numbers[index+1]);
else
classOperators[index].addOperands(numbers[0],numbers[1]);
std::sort(classOperators.begin(),classOperators.end());
}
printOperatorVector(classOperators);
std::stack<classOperator> classOperatorStack;
classOperatorStack.push(classOperators[index]);
if (classOperatorStack.top().actualclassOperator == '*')
Result = *classOperatorStack.top().pointer_To_leftOperand*
*classOperatorStack.top().pointer_To_rightOperand;
numberStack.push(Result);
if(classOperatorStack.empty() == false)
classOperatorStack.pop();
{
Result = *classOperatorStack.top().pointer_To_leftOperand/
*classOperatorStack.top().pointer_To_rightOperand;
numberStack.push(Result);
if(classOperatorStack.empty() == false)
classOperatorStack.pop();
Result = *classOperatorStack.top().pointer_To_leftOperand+
*classOperatorStack.top().pointer_To_rightOperand;
numberStack.push(Result);
if(classOperatorStack.empty() == false)
classOperatorStack.pop();
Result = *classOperatorStack.top().pointer_To_leftOperand-
*classOperatorStack.top().pointer_To_rightOperand;
numberStack.push(Result);
if(classOperatorStack.empty() == false)
classOperatorStack.pop();
}
else if (classOperatorStack.top().actualclassOperator == '^')
Result = pow(*classOperatorStack.top().pointer_To_leftOperand,
*classOperatorStack.top().pointer_To_rightOperand);
numberStack.push(Result);
if(classOperatorStack.empty() == false)
classOperatorStack.pop();
Result = *classOperatorStack.top().pointer_To_rightOperand;
numberStack.push(Result);
if(classOperatorStack.empty() == false)
classOperatorStack.pop();
if(!classOperatorStack.empty())
classOperatorStack.top().pointer_To_leftOperand = &Result;
classOperatorStack.top().pointer_To_rightOperand = &Result;
}
}
result = numberStack.top();
return 0;
void Expression::containsBrackets()
if (expressionString[index]=='(' ||expressionString[index]==')' )
hasBrackets = true;
void Expression::getParentheses() //Finds the parentheses and their positions in the expression
vector_parentheses.push_back(temporary);
}
void Expression::getSubExpressions()
if(vector_parentheses[index].typeOfParenthesis == '(')
// | ( | ( | ( | ) | ) | ) |
// * - - - - *
// n=0 =size-n
//in an array of parentheses, corresponding closing parenthesis for an opening bracket at position
in is
// at [size-n]
--digits_after;
std::stringstream ss;
expressionString+=' ';
else
expressionString.replace(positionOfOpeningParenthesis,positionOfClosingParenthesis-
positionOfOpeningParenthesis +1,stringResult);
}
}
evaluate(); //Evaluates new expression containing the result of the sub expression
int main() {
std::cout << "Input support for negative input numbers" << std::endl;
ifstream file;
std::string path;
//cin>>path;
file.open(path.c_str());
if(!file.is_open())
return 0;
string line;
std::string expression;
while(getline(file, expression))
if(expression == "Quit")
break;
Expression expressionToEvaluate(expression, false);
std::cout << "Here is the output: " << std::fixed << setprecision(2)<<
expressionToEvaluate.result<< std::endl;
file.close();
return 0;