- Joined
- Jan 30, 2013
- Messages
- 12,455
(NOTE : ignores hierarchy)
Why this code only works if there's no secondary number in the left side of equation and there's only two level of stacks at max?
Working examples :
(2-1)
(8*5)
Other 1 time calculation, if they have ()
((2-1)-1)
((2*4)+4)
Any two tiered calculation if neither side has any value above 9
In other cases, they don't work.
Also, ( (a+b) - (c+d) )) where a,b,c,d are int results in infinite loop.
[IGNORE THE ERROR CHECKS PRIOR TO THE CALCULATION, ALL EQUATION BEING INPUT CONSIDERED PROPERLY SET]
Why this code only works if there's no secondary number in the left side of equation and there's only two level of stacks at max?
Working examples :
(2-1)
(8*5)
Other 1 time calculation, if they have ()
((2-1)-1)
((2*4)+4)
Any two tiered calculation if neither side has any value above 9
In other cases, they don't work.
Also, ( (a+b) - (c+d) )) where a,b,c,d are int results in infinite loop.
[IGNORE THE ERROR CHECKS PRIOR TO THE CALCULATION, ALL EQUATION BEING INPUT CONSIDERED PROPERLY SET]
C:
#include <iostream>
#include <stack>
#include <string>
char opr[4] = {'+','-','*','/'};
char num[10] = {'0','1','2','3','4','5','6','7','8','9'};
using namespace std;
void LevelControl(int lvl, int pos)
{
if (lvl>=0)
cout << lvl;
else if (lvl==-1)
cout << endl << "Invalid Level" << endl;
else if (lvl==-2)
cout << endl << "Ending not closed" << endl;
}
bool ArePair(char opening, char closing)
{
if (opening == '(' && closing == ')') return true;
}
bool ParBalance(string data,bool control)
{
stack<char> S;
int level=0,i;
for (i=0;i<data.length();i++)
{
if (data[i]=='(')
{
S.push(data[i]);
level++;
}
else if (data[i]==')')
{
if(S.empty() || !ArePair(S.top(),data[i]))
{
if (control==true)
LevelControl(-1,i+1);
return false;
}
else
{
level--;
S.pop();
}
}
if (control==true)
LevelControl(level,i+1);
}
if (S.empty()==false && control==true) LevelControl(-2,i+1);
return S.empty() ? true:false;
}
void ValReport(int i)
{
cout << endl << "Error in equation at column " <<i;
}
bool Valid(string data)
{
for(int i=1;i<data.length();i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
if (data[i]==opr[j] && (data[i-1]==opr[k] || (i>1 && data[i-2]==opr[k])))
{
ValReport(i);
return false;
}
}
}
}
return true;
}
int S2I(string s)
{
int x = 0;
for (int i=0;i<s.length();i++)
{
x = x*10 + ((int) s[i]-'0');
}
return x;
}
string I2S(int i)
{
string s;
if (i!=0)
{
while(i!=0)
{
s = (char) ((i%10)+'0') + s;
i = i/10;
}
}
else
{
s = ((char) (i)+'0');
}
cout << "Value is : " << s << endl;
return s;
}
string Computate(string data)
{
int num1,num2,res,i,j;
char op;
bool stnum = false;
string s;
for (i=0;i<data.length();i++)
{
cout << "2" << endl;
for(j=0;j<10;j++)
{
if (data[i]==num[j])
{
cout << "3" << endl;
s = (char)data[i] + s;
}
}
if (stnum==false)
{
num1=S2I(s);
s.clear();
stnum = true;
}
else
{
num2=S2I(s);
}
for(j=0;j<4;j++)
{
if (data[i]==opr[j])
{
op = data[i];
}
}
}
cout << "4" << endl;
cout << "Get Num (1,2) : " << num1 << "," << num2 << endl;
switch (op)
{
case '+' : res = num1 + num2;break;
case '-' : res = num1 - num2;break;
case '*' : res = num1 * num2;break;
case '/' : res = num1 / num2;break;
}
cout << "Get Res : "<< res << endl;
cout << "5" << endl;
s = I2S(res);
cout << "6" << endl;
return s;
}
string Loop(string data)
{
string tempdata;
stack<char> S;
int temp;
for (int i=0;i<=data.length();i++)
{
if (data[i]=='(')
{
S.push(i);
cout << "PUSH" << endl;
if (S.size()>9)
{
cout << "Oversize!";
break;
}
}
else if (data[i]==')')
{
tempdata = data.substr(S.top()+1,i-1);
cout << i << endl;
tempdata = Computate(tempdata);
data.replace(S.top(),i,tempdata);
if (S.size()==1)
{
data.erase();
data = tempdata;
}
cout << "Current Data : " <<data << endl;
S.pop();
cout << "POP" << endl;
break;
}
}
if (!S.empty())
return Loop(data);
else
return data;
}
int CountAll(string data)
{
int temp;
string last;
last = Loop(data);
temp = S2I(last);
return temp;
}
int main()
{
char input[70];
string data;
cout << "Input : " << endl;
cin.getline(input, sizeof(input));
data = input;
if (ParBalance(data,true))
cout << endl << "Balanced"<< endl;
else
cout << endl << "Not Balanced"<< endl;
if (Valid(data))
cout << endl << "Valid"<< endl;
else
cout << endl << "Invalid"<< endl;
cout << "Result is : " << CountAll(data);
return 0;
}
Last edited: