equation evaluator

hello everyone,

I have written a code 2 calculate an equation entered by user. I have to prioritize the operators in order of precedence with ( coming first then * and / then + and - . I am having a logical error in my code, but I don't know how to solve it. The problem is with

sub_equation.replace(sub_equation.at(position[i]-1),2,y);

and

equation.replace(equation.at(position[i]-1),2,y);

The error says that there is no enough memory allocation
Your help is needed. Thanks :)


***********************************************************
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <stack>
using namespace std;

void main ()
{
	string equation, sub_equation="",y="";
	stringstream ss;
	int brace_r, brace_l;
	//stack <char> s;
	cout<<"Please enter your equation"<<endl;
	getline(cin,equation);
	/*
	string x="abcd";
	equation.replace(3,7,x);
	cout<<equation<<endl;
	*/
	int l=equation.length();
	const char *eq; 
	eq= equation.c_str(); //converting sub_equation to char array

	int i,j,k, counter, operators,x;
	int position[50];
	brace_r=equation.find('(');
	brace_l=equation.find(')');
	do
	{


		while (brace_r>0 && brace_r<50) //find all braces in equation
		{
			if (brace_l>0 && brace_l<50) //making sure that braces match
			{
				sub_equation=equation.substr(brace_r,brace_l-brace_r+1); //copying chars between braces in new string
				//sub_equation1=sub_equation; 
				const char *sub_eq; 
				sub_eq= sub_equation.c_str(); //converting sub_equation to char array
				counter=0; //counter for operators between braces
				operators=0; //counter for 
				
				for (k=0;k<50;k++) //array to determine location of operators
					position[k]=-1;
				j=0;
				for (i=0; i<sub_equation.length();i++)
				{
					if (sub_eq[i]==('*') || sub_eq[i]==('/'))
					{
						counter++;
						position[j]=i;
						j++;
					}
				}

				for (i=0; i<sub_equation.length();i++)//loop to evaluate & replace value of data in braces
				{
					if (counter!=0)
					{

						if (position [i]!=-1)
						{
							if (sub_equation.at(position[i])=='*')
							{
								x=int(sub_equation.at(position[i]-1))*int(sub_equation.at(position[i]+1));
								ss<<x;
								ss>>y;
								counter--;
							    sub_equation.replace(sub_equation.at(position[i]-1),2,y);
							//	sub_equation.erase(sub_equation.at(position[i]-1),2);
							//	sub_equation.insert(sub_equation.at(position[i]-1),y);

							}

							else if (sub_equation.at(position[i])=='/')
							{
								x=int(sub_equation.at(position[i]-1))/int(sub_equation.at(position[i]+1));
								counter--;
								ss<<x;
								ss>>y;
								sub_equation.replace(sub_equation.at(position[i]-1),2,y);
							//	sub_equation.erase(sub_equation.at(position[i]-1),2);
							//	sub_equation.insert(sub_equation.at(position[i]-1),y);

							}
						}
						
					}
					else 
						break;
					
				}

				counter=0;
				for (k=0;k<50;k++)
					position[k]=-1;
				for (i=0; i<sub_equation.length();i++)
				{
					j=0;

					if (sub_eq[i]==('+') || sub_eq[i]==('-'))
					{
						counter++;
						position[j]=i;
						j++;
					}
				}


				for (i=0; i<sub_equation.length();i++)//loop to evaluate&replace value of data in braces
				{
					if (counter!=0)
					{

						if (position [i]!=-1)
						{
							if (sub_equation.at(position[i])=='+')
							{
								x=int(sub_equation.at(position[i]-1))+int(sub_equation.at(position[i]+1));
								counter--;
								ss<<x;
								ss>>y;
								sub_equation.replace(sub_equation.at(position[i]-1),2,y);
								//sub_equation.erase(sub_equation.at(position[i]-1),2);
								//sub_equation.insert(sub_equation.at(position[i]-1),y);
							}

							else if (sub_equation.at(position[i])=='-')
							{
								x=int(sub_equation.at(position[i]-1))-int(sub_equation.at(position[i]+1));
								counter--;
								ss<<x;
								ss>>y;
								
								sub_equation.replace(sub_equation.at(position[i]-1),2,y);
								//sub_equation.erase(sub_equation.at(position[i]-1),2);
								//sub_equation.insert(sub_equation.at(position[i]-1),y);

							}
							else break;


						}
					}
					else 
						break;
				}

			}
		}
		ss<<sub_equation;
		ss>>y;
		equation.replace(brace_r, brace_l-brace_r+1, y);
		counter=0; //counter for operators between braces
		operators=0; //counter for 
		for (k=0;k<50;k++)
			position[k]=-1;
		for (i=0; i<equation.length();i++)
		{
			j=0;
			if (eq[i]==('*') || eq[i]==('/'))
			{
				counter++;
				position[j]=i;
				j++;
			}
		}
		for (i=0; i<equation.length();i++)//loop to evaluate & replace value of data in braces
		{
			if (counter!=0)
			{
				if (position [i]!=-1)
				{
					if (equation.at(position[i])=='*')
					{
						x=int(equation.at(position[i]-1))*int(equation.at(position[i]+1));
						ss<<x;
						ss>>y;
						counter--;
						equation.replace(equation.at(position[i]-1),2,y);
						//		equation.erase(equation.at(position[i]-1),2);
						//		equation.insert(equation.at(position[i]-1),y);

					}
					else if (equation.at(position[i])=='/')
					{
						x=int(equation.at(position[i]-1))/int(equation.at(position[i]+1));
						counter--;
						ss<<x;
						ss>>y;
						equation.replace(equation.at(position[i]-1),2,y);
						//		equation.erase(equation.at(position[i]-1),2);
						//		equation.insert(equation.at(position[i]-1),y);

					}
				}
			}
		}
	}while (equation.find('+')<50 && equation.find('-')<50 && equation.find('*')<50 && equation.find('/')<50);
		cout<<"The inserted equation equals "<<equation<<endl;

		system ("pause");
}
Last edited on
once again, if you want to paste code in the forum use the "Source code" button tagged with <> in the Format: button box. Anything you put between the {code} and {/code} willhave all its indentation intact, otherwise we aint gonna even try reading this.
Last edited on
ok sorry. i am just new to the forum and i didnt know how 2 do it. thanx 4 ur feedback :)
another pro tip of the day,

***use functions...!!!***

I mean, whats the point of equation evaluator if its not in a function. If you make the same code just function oriented, then you can use that function in other code as well. lets say tomorow you want to make some algorythm thingy majingy, which could use lets say an equation evaluator. then all you'll have to do is #include the function defenition/implementation and you can use it.
anyway, i used the debbuger a little bit, and your code is fuuuuulll of bugs, its not just a little bit but let me get you started.

1
2
3
4
5
6
7
8
9
10
11
for (i=0; i<sub_equation.length();i++)
{
	j=0;

	if (sub_eq[i]==('+') || sub_eq[i]==('-'))
	{
		counter++;
		position[j]=i;
		j++;
	}
}


j, whatever it does or supposed to do, is always 0 no matter what, the way you reset it right after you increment it assures you that it will stay 0.

what you probably meant was this
1
2
3
4
5
6
7
8
9
10
11
j=0;
for (i=0; i<sub_equation.length();i++)
{

	if (sub_eq[i]==('+') || sub_eq[i]==('-'))
	{
		counter++;
		position[j]=i;
	}
	j++;
}


further more your sub_equation does not reduce brackets () meaning that for equation = 3+(4+4)
sub_equation = (4+4)

are you taking that into consideration?

further more
 
x=int(sub_equation.at(position[i]-1))+int(sub_equation.at(position[i]+1));


this line of code doesnt work properly, I know its a drag to convert chars to digits but basically you need to look into the ascii table to know what each char stands for basically '4' = 52 so your x will evaluate to 52+52 if the sub_equation is (4+4). the solution is to subtract 48 from the char representation to get the digit correctly.

http://images.search.conduit.com/ImagePreview/?q=ascii&ctid=CT2296690&SearchSource=4&FollowOn=true&PageSource=Results&SSPV=&CUI=&UP=&UM=&start=0&pos=1

this picture should shed some light.


you probably have more bugs,


I highly recommend that you learn to program step by step, writing and testing small portions of code at a time so you can fix these small errors as they appear, and the do appear, right in the rear.
Topic archived. No new replies allowed.