0

i am struggling with a string related problem in c++.

suppose i have a string s="6*6+4*8+6/6/6-632+81";

My main goal is to do the arithmetic operation from the string. So in the bellow code i am getting correct result when the integer value is a 1 digit integer.

string math[]="1+2"; Result:3;

but when the, string math[]="10+20" Then result is:2;

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {

    int A,B,val;
        char math[]="1+2";
        //char math[]="10+20";

        for (int i = 0 ; math[i]!=NULL; i++)
        {
            char ch =math[i];
             if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
             {

                  char ch2=math[i-1];

                 A=(ch2-'0');


                 char ch3=math[i+1];
                 B=(ch3-'0');

                 switch (ch) /* ch is an operator */
                {
                case '*':
                    val = A * B;
                    break;

                case '/':
                    val = A / B;
                    break;

                case '+':
                    val = A + B;
                    break;

                case '-':
                    val = A - B;
                    break;
                }

             }

            if ( math[i] == NULL)
            {
                break;
            }

        }


    cout<<val<<endl;

    }

so I realize the problem is choosing the value of A and B.Here I am not selecting the whole digit. so i want my A to be 10 and B 20.But my programm is selecting A=0 and B=2.So to get the whole digit .like A=10,i made these changes in my code[only for A.As a test].

        if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
        {
         char total;
         int k=i-1;
         cout<<"k="<<k<<endl;
         char p;
         p=math[k];
         for(;k>-1 && isdigit(p)==true;k--)
         {
             char c=math[k];
             cout<<"c"<<c<<endl;
             total=math[i-1];

             total=total+c;

         }

         char ch2=total;
         // char ch2=math[i-1];
        cout<<"ch2:"<<total<<endl;
         A=(ch2-'0');
         cout<<"A="<<A<<endl;

BUT now for A i am getting garbage value.Now how can i solve this.More specifically get the value of A=10 and B=20 and get the correct result for this math expression.

1
  • 1
    Math formula parsing is much more involved than that. Usually, you first tokenize the input, which should take care of grouping digits that correspond to a single number. Then you find the operator of least priority and recursively solve for the left and right operands. Commented Nov 21, 2019 at 17:07

1 Answer 1

1

Use std::string and std::stringstream like

#include <iostream>
#include <sstream>
#include <string>
using std::cout;
using std::string;
using std::stringstream;

void func(double &result, char op, double num);

int main() {
    string math = "6*6+4*8+6/6/6-632+81";
    stringstream mathStrm(math);

    double result;
    mathStrm >> result;
    char op;
    double num;
    while (mathStrm >> op >> num) {
        cout << "op: " << op << ", num: " << num << '\n';
        func(result, op, num);
    }
    cout << result << '\n';
}


void func(double &result, char op, double num) {
    switch (op) {
        case '+':
            result += num;
            break;
        case '-':
            result -= num;
            break;
        case '*':
            result *= num;
            break;
        case '/':
            result /= num;
    }
}

Notice that the string is evaluated from left to right without precedence.

Sign up to request clarification or add additional context in comments.

3 Comments

so how can i do it with precedence
@POKA that's a completely different question. You could store all tokens in containers and evaluate all operations in a given order, e.g. first division, then multiplication, then addition and then subtraction. Alternatively you could build a binary tree. This will only if your string only contains binary operations. If you add unary operations it becomes more complicated.
Thanks so much bro, but if i want to be like this (2+2)*(4+5) How can i do that ? in the upper code it give me 0 in resault.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.