How to convert string "1+3" to operation

Jul 18, 2011 at 4:26pm
I am working on a c++ assignment , I figure out how to determain the numbers , but what about the operation in the string , for example 1-2 or 1+2
right now my program would make the following

operand1=1
operand2=2
then somehow i need to determain the minus or the plus in between to make that operation.

Hope to find help on how to determine that , is theres a way to cast that string + or - to convert it to an operation .


thanks
Jul 18, 2011 at 4:31pm
Expression parsers:

http://tinyurl.com/6dtn5k9
Jul 18, 2011 at 6:23pm
If you can extract the operator character from your string as a char, then you can use a switch():
1
2
3
4
5
6
7
8
9
10
11
12
13
char opp = /* extract it from string */;

switch(opp)
{
    case '+':
        // add operand1 and operand2
        break;
    case '-':
        // subtract operand1 and operand2
        break;
    default:
        std::cout << "Unknown operator: " << opp << '\n';
}
Jul 19, 2011 at 2:16am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main()
{
	clrscr();
	char a[20],op;int j=0,x1=0,x2=0;
	cout<<"Enter the series of operations : ";
	gets(a);
	while((a[j]!='+')&&(a[j]!='-')&&(a[j]!='*')&&(a[j]!='/')&&(a[j]!='\0'))
	{
			x1=(10*x1)+a[j++]-48;
	}
	while(a[j]!='\0')
	{
		x2=0;
		op=a[j++];
		while((a[j]!='+')&&(a[j]!='-')&&(a[j]!='*')&&(a[j]!='/')&&(a[j]!='\0'))
		{
			x2=(10*x2)+a[j++]-48;
		}
		switch(op)
		{
		case '+':{x1=x1+x2;}break;
		case '-':{x1=x1-x2;}break;
		case '*':{x1=x1*x2;}break;
		case '/':{x1=x1/x2;}break;
		}
	}
	cout<<x1;
	getch();
}
Jul 19, 2011 at 3:00am
I actually like rambo's, but there are a couple of things that could be added/improved.

First, the concept of operator precedence doesn't exist. E.g. 1+2*3 outputs 9 instead of 7.
A possible solution to this is to evaluate the expression in two steps. During the first step,
perform multiplications and divisions and during the second, additions and subtractions.
That is, input like this -> 1+2*5-3*4+6 should first be turned into 1+10-12+6 and then into 5.

Second, the string - to - num conversions could be made using stringstreams (rambo probably can't use them,
as his compiler most likely doesn't support them :P ( @rambo, throw turbo c++ in the recycle bin and get this:
http://sourceforge.net/projects/codeblocks/files/Binaries/10.05/Windows/codeblocks-10.05mingw-setup.exe )).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

double eval_L1_exp(const string & expression)
{
    double result, number;
    char operation;

    stringstream buffer(expression);

    buffer >> result;

    while (buffer >> operation >> number)
    {
        switch(operation)
        {
            case '+': result += number; break;
            case '-': result -= number; break;
        }
    }

    return result;
}

string L2_exp_to_L1_exp(const string & expression)
{
    double operand1, operand2;
    char operation;

    stringstream buffer(expression + '#');
    stringstream result;

    buffer >> operand1;

    while (buffer >> operation)
    {
        switch(operation)
        {
            case '+': case '-':
                result << operand1 << operation;
                buffer >> operand1; break;

            case '*': buffer >> operand2; operand1 *= operand2; break;
            case '/': buffer >> operand2; operand1 /= operand2; break;

            case '#': result << operand1; break;
        }
    }

    return result.str();
}

int main()
{
    string L1_expression;
    string L2_expression;

    while (true)
    {
        getline(cin, L2_expression);

        if (L2_expression == "") break;

        L1_expression = L2_exp_to_L1_exp(L2_expression);

        cout << L1_expression << endl;

        cout << eval_L1_exp(L1_expression) << endl;
    }

    return 0;
}
Last edited on Jul 19, 2011 at 3:15am
Jul 19, 2011 at 12:05pm
i didnt check for the dmas rule....
i got used to borland and i'm still in my schooling..i dont want the syntax to get mixed up......
Topic archived. No new replies allowed.