C++ Program for String Calculator [DEVCPP/GCC]

PROGRAM

// C++ Program for String Calculator

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

class Program
{
private:

    string expression;
    int indexOfPreviousOperator;
    int indexOfNextOperator;

public:

Program(string ex)
    {
        expression = ex;
    }

    void Evaluate()
    {
        int result = 0;

        if ((expression.find('+') == string::npos && expression.find('-') == string::npos && expression.find('*') == string::npos && expression.find('/') == string::npos))
{
                cout<<stoi(expression);
        }
     
        for (int i = 0; i < expression.size(); i++)
{
            if (expression[i] == '/')
{
                result = getFirstOperand(i) / getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, indexOfNextOperator - indexOfPreviousOperator);
                expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
            else if (expression[i] == '*')
{
                result = getFirstOperand(i) * getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, indexOfNextOperator - indexOfPreviousOperator);
                expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
            else if (expression[i] == '+' && expression.find('/') == string::npos && expression.find('*') == string::npos)
{
                result = getFirstOperand(i) + getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, (indexOfNextOperator - indexOfPreviousOperator));
expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
            else if (expression[i] == '-' && expression.find('/') == string::npos && expression.find('*') == string::npos)
{
                result = getFirstOperand(i) - getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, indexOfNextOperator - indexOfPreviousOperator);
                expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
        }
    }

    int getFirstOperand(int index)
    {
        string operand = "";
        indexOfPreviousOperator = 0;
     
        for (int i = (index - 1); i >= 0; i--)
{
            if (!isdigit(expression[i]))
{
                indexOfPreviousOperator = i;
                break;
            }
        }
        operand = expression.substr(indexOfPreviousOperator, (index - indexOfPreviousOperator));
        return stoi(operand);
    }

    int getSecondOperand(int index)
    {
        string operand = "";
        indexOfNextOperator = expression.size();
     
        for (int i = (index + 1); i < expression.size(); i++)
{
            if (!isdigit(expression[i]))
            {
                indexOfNextOperator = i;
                break;
            }
        }
        operand = expression.substr((index + 1), (indexOfNextOperator - index - 1));
        return stoi(operand);
    }
};

int main()
{
string inputExpression;

cout<<"ENTER EXPRESSION TO BE EVALUATED: ";
      cin>>inputExpression;
 
      Program obj(inputExpression);
obj.Evaluate();

      return 0;
}

OUTPUT



Share this

Related Posts

Latest

2 comments

comments
November 12, 2018 at 6:24 PM delete

thank you for sharing useful post.
c++ programming tutorial
welookups

Reply
avatar
December 11, 2018 at 1:58 PM delete

Thank you for sharing valuable information. Nice post. I enjoyed reading this post. The whole blog is very nice found some good stuff and good information here Thanks
Tutor Jobs in Delhi | Home Tutors in Delhi

Reply
avatar

FIND US ON FACEBOOK!