String Calculator

I'm working on a string calculator which will be able to solve questions like 14-7+(3/2)= but the whole parsing thing is a lot more complicated than i expected.
I've decided taht the steps in the program will be:
1. get calculation string
2. while parsing the string
3. find the numbers in the string
4. store the number into a variable
5. return the position of the last digit in the number
6. starting from returned position, look for operator and use a switch sattement e.t.c

I am confused about step 2,3 and 5.
2. I'm not sure how to parse the string
3. How can you move through a string and get the program to pick out the numbers
6. which function could i use to return the position of the last digit.

I'd really appreciate the help.
use a STACK to check the braces/paranthesis,

it will be easier if you stick with Reverse Polish Notation,

if you dont know what stack and RPN are, take a look at these links

http://en.wikipedia.org/wiki/Reverse_Polish_notation
http://en.wikipedia.org/wiki/Stack_%28data_structure%29

there are more ways to do this... just think creatively.....

cheers...

p.s.
if you know about tree data structures this will be another hint,
the funny thing is that if you write your mathamatical expression to a tree, you can get both RPN and the normal notation by just changing the way you traverse on the tree...
http://en.wikipedia.org/wiki/Binary_tree
http://math.hws.edu/eck/cs225/s03/binary_trees/

EDIT---
ok, now i see that you are heading a harder way...
trying to figerout the numbers inside a string and then use them will take so much time and space(memory)... what you have to do is,
just read the string as a stream of characters... ( use a while loop to read until the char is NULL )
and put every number you meet inside of some where( stack is best )....
Last edited on
I really appreciate your help but that sounds pretty advanced and I'm still just a beginner hobbyist programmer. I do intend to look into those but hopefully sometime later.

.. what you have to do is,
just read the string as a stream of characters... ( use a while loop to read until the char is NULL )
and put every number you meet inside of some where( stack is best )....


Yup, that's what i've done and this oversimplistic code is what i've come up with:
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
#include <iostream>
#include <string>
using namespace std;

int nextInt(string);
int nextOper(string);
int calculate(int,int, int);

int main()
{
	int sum=0;
	string calc;

	do{ 
		cout<<"Enter calculation (e.g. 2+2= ): ";
		int num=nextInt(calc);
		int op=nextOper(calc);
		sum=calculate(num, sum, op);
		
	}while(getline(cin,calc,'='));
cout<<"Result after: "<<sum<<endl;
return 0;
}

int nextInt(string s)
{
	char ch;
	int n;
	while(s.get(ch))
	
		if(ch>='0' && ch<='9')
		{
			cin.putback(ch);
			cin >>n;
			break;
		}
	return n;
}

int nextOper(string s)
{
	char ch;
	int n;
	while(s.get(ch))
	
		if(ch == '+'){cin.putback(ch); n=1; break;}
		else if(ch == '-'){cin.putback(ch); n=2; break;}
		else if(ch == '*'){cin.putback(ch); n=3; break;}
		else if(ch == '/'){cin.putback(ch); n=4; break;}
		else {n=0; break;}
	return n;
}

int calculate(int num,int sum, int op)
{
switch(op)
{
	case 1: {sum+=num; break;}
	case 2: {sum-=num; break;}
	case 3: {sum*=num; break;}
	case 4: {sum/=num; break;}
	default: sum+=num;
}
}


I'm pretty sure there are some logical errors in there but for the moment i'd like to know is (and I know this is dumb ) why can't i use get() with strings - I mean isn't a string just an array of characters? Is there some alternative I can use?
Topic archived. No new replies allowed.