|
I have here the code:
#include <iostream>
#include <string>
#include <stack>
#include <cctype>
#include <cassert>
using namespace std;
#include <sstream>
string infixTOposfix(string);
int evaluarRPN(string);
int main ()
{
string infix,
posfixWspaces,
dummy;
double result;
char repeat;
do {
cout << "\nPlease enter the expression in INFIX: ";
getline(cin,infix);
posfixWspaces = infixTOposfix(infix);
cout << "This is the expression in POSTFIX: " << posfixWspaces << endl;
result = evaluarRPN(posfixWspaces);
cout << "The expression after the evaluation: " << result;
cout << "\nDo you want to repeat? Enter 'y' ";
cin >> repeat;
getline(cin,dummy);
} while ( repeat == 'y');
system("PAUSE");
return 0;
}
string infixTOposfix(string exp)
{
char token,
topToken;
stack<char> opStack;
string postfixExp;
const string BLANK = " ";
for (int i = 0; i < exp.length(); i++)
{
token = exp[i];
switch(token)
{
case ' ' : break;
case '(' : opStack.push(token);
break;
case ')' : for (;; )
{
assert (!opStack.empty());
topToken = opStack.top();
opStack.pop();
if (topToken == '(') break;
postfixExp.append(BLANK + topToken);
}
break;
case '+' : case '-' :
case '*' : case '/': case'%':
for (;; )
{
if (opStack.empty() ||
opStack.top() == '(' ||
(token == '*' || token == '/' || token == '%') &&
(opStack.top() == '+' || opStack.top() == '-'))
{
opStack.push(token);
break;
}
else
{
topToken = opStack.top();
opStack.pop();
postfixExp.append(BLANK + topToken);
}
}
break;
default :
postfixExp.append(BLANK + token);
for(;; )
{
if ( !isalnum(exp[i+1]) ) break;
i++;
token = exp[i];
postfixExp.append(1, token);
}
}
}
for (;; )
{
if (opStack.empty()) break;
topToken = opStack.top();
opStack.pop();
if (topToken != '(')
{
postfixExp.append(BLANK + topToken);
}
else
{
cout << " *** Error in infix expression ***\n";
break;
}
}
return postfixExp;
}
int evaluarRPN(string posfijo)
{
stack<int> numStack;
int x,
y,
toInt;
string number;
for (int i = 0; i < posfijo.length(); i++ )
{
if ( isdigit(posfijo[i]) )
{
number = posfijo[i];
stringstream ss( number );
ss >> toInt;
numStack.push( toInt );
}
else if (posfijo[i] != ' ' )
{
y = numStack.top();
numStack.pop();
x = numStack.top();
numStack.pop();
if (posfijo[i] == '+')
numStack.push( x+y );
else if (posfijo[i] == '-')
numStack.push( x-y );
else if (posfijo[i] == '*')
numStack.push( x*y );
else
numStack.push( x/y );
}
}
return numStack.top();
}
the question here is: why do i get this result...
when I put 5-5 instead of 0 the result is 1
when I put 11*5 instead of 55... the result is 5
when I put 100+100 the result is 0 instead of 200
when I put 100/100 the result is an error message box... </int></char></sstream></cassert></cctype></stack></string></iostream>
modified on Tuesday, August 12, 2008 8:13 AM
|
|
|
|
|
1) have you read the posting guidelines ? your code is too huge, and not even formatted.
2) your thread title is not really presenting what the question will be about.
3), have you stepped into the debugger and inspect the variables states, step by step ?
|
|
|
|
|
actually I'm just a new member here today. sorry for violating the rules and uhmmm... I've already solved a part of it but it only works for one digit number only do you have any tip that can also work in more than 1 digit, a decimal value and exponents?
|
|
|
|
|
Maito Gai wrote: actually I'm just a new member here today
your account says you've been a member since Tuesday, March 04, 2008...
Maito Gai wrote: sorry for violating the rules
it's not too late to read them[^] at least...
So please edit your message consequently.
|
|
|
|
|
what I mean is this is my first time to post a thread. All before this time is to read the tutorials here... like reading online books.
|
|
|
|
|
that doesn't forbid you to follow the advices I've already been given you twice now
|
|
|
|
|
|
you can read it now more better thanks to your tip for the design
|
|
|
|
|
nice try
unfortunately, the "Ignore HTML tags" checkbox forbids the <pre> and <code> tags to be shown.
uncheck it and replace the < with < instead.
|
|
|
|
|
|
But the for() loops still have smiley faces. Use the Preview button before posting.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
it's done no smiley faces anymore
|
|
|
|
|
Now what you need to do is figure out if the problem is with infixTOposfix() or evaluarRPN() . In other words, it makes no sense to post evaluarRPN() if infixTOposfix() is not working. Make sense?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Maito Gai wrote: ...do you have any tip that can also work in more than 1 digit...
Use the debugger to step through your code. Don't just use cout to determine if your code is working.
Does infix contain the right value before calling infixTOposfix() ? Does token in infixTOposfix() have the right value with each iteraton of the for() loop?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
I don't know how to also reason out the error instead of a pop-up error message box.
|
|
|
|
|
Maito Gai wrote: I don't know how to...
Use F5 instead of Ctrl+F5. Be sure and set appropriate breakpoints.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
I've encounter this message:
'postfix validation.exe': Loaded 'C:\Documents and Settings\Aldrich Uy\My Documents\Visual Studio 2005\Projects\postfix validation\debug\postfix validation.exe', Binary was not built with debug information.
'postfix validation.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'postfix validation.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
The program '[832] postfix validation.exe: Native' has exited with code -1073741811 (0xc000000d).
I couldn't believe that binary was not built with debug information
|
|
|
|
|
Maito Gai wrote: 'postfix validation.exe': Loaded 'C:\Documents and Settings\Aldrich Uy\My Documents\Visual Studio 2005\Projects\postfix validation\debug\postfix validation.exe', Binary was not built with debug information.
So why don't you just change the project setting to Debug (instead of Release)?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
either you don't read the answers you get, or you don't understand them, ... or you're a moron.
I've told you since the beginning of this thread to use your debugger[^].
now, 6 hours later, you come and say you have an error message that implicitely says that you didn't built in debug mode. Couldn't you just say it earlier ?
|
|
|
|
|
ahhh... the pop-up message says:
"No Debugging information
Debugging information for postfix validation.exe cannot be found or does not match. Binary was not built with debug information."
|
|
|
|
|
Maito Gai wrote: ...also reason out the error instead of a pop-up error message box.
What does this message box say? Could it be related to a division-by-zero error?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Maybe I've saw the problem because of for example in box of array it only consist of one digit regardless of length of the operand and when it passes through the element which is an operator the counter of the operand resets to the first operand value after the operator.
for clearness:
10/2 which is equal to 5, it only gets 0/2 and the answer is 0 ;
10/10 which must be 1, but the error message box will appear instead of the correct answer.
help:
is there a way to concatenate the elements of the array?(except operators)
|
|
|
|
|
I would suggest you to use a debugger and follow the code to track ur mistake
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
I've already solved a part of it but it only works for one digit number only do you have any tip that can also work in more than 1 digit, a decimal value and exponents?
|
|
|
|
|
eventually the error there is I can't input negative values, decimal values, exponential values and more than 1 digit values... that's the real error
|
|
|
|