Click here to Skip to main content
15,916,835 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Debug Assertion Failure on CreateDialogIndirect Pin
SutterA22-Aug-08 7:04
SutterA22-Aug-08 7:04 
GeneralRe: Debug Assertion Failure on CreateDialogIndirect Pin
Mark Salsbery22-Aug-08 7:27
Mark Salsbery22-Aug-08 7:27 
GeneralRe: Debug Assertion Failure on CreateDialogIndirect Pin
SutterA22-Aug-08 9:04
SutterA22-Aug-08 9:04 
GeneralRe: Debug Assertion Failure on CreateDialogIndirect Pin
Mark Salsbery22-Aug-08 9:18
Mark Salsbery22-Aug-08 9:18 
AnswerRe: Debug Assertion Failure on CreateDialogIndirect Pin
SutterA22-Aug-08 9:29
SutterA22-Aug-08 9:29 
Questionnot able to write the data into image file [modified] Pin
Bernaad12-Aug-08 1:29
Bernaad12-Aug-08 1:29 
AnswerRe: not able to write the data into image file Pin
Maito Gai12-Aug-08 2:54
Maito Gai12-Aug-08 2:54 
Questionthe code is correct but the output is wong [modified] Pin
Maito Gai12-Aug-08 0:57
Maito Gai12-Aug-08 0:57 
I have here the code:

#include <iostream>
#include <string>
#include <stack>
#include <cctype>  //alnum
#include <cassert> //assert
using namespace std;
#include <sstream>

string  infixTOposfix(string);
int evaluarRPN(string);

int main ()
{ // main began

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;
} // main end


string infixTOposfix(string exp)
{
/*-------------------------------------------------------------------------
   Function to convert an infix expression exp to postfix.

   Precondition:  None
   Postcondition: Postfix expression corresponding to exp is returned
       or an error message displayed if exp is not well-formed.
-------------------------------------------------------------------------*/

   char token,                   // character in exp
        topToken;                // token on top of opStack
   stack<char> opStack;                // stack of operators
   string postfixExp;            // postfix expression
   const string BLANK = " ";
   for (int i = 0; i < exp.length(); i++)
   {
      token = exp[i];
      switch(token)
      {
         case ' ' : break;       // do nothing -- skip blanks
         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 :  // operand
                    postfixExp.append(BLANK + token);
                    for(;; )
                    {
                       if ( !isalnum(exp[i+1]) ) break; // end of identifier
                       i++;
                       token = exp[i];
                       postfixExp.append(1, token);
                    }
      }
   }
   // Pop remaining operators on the stack
   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)
{ // evaluarRPN began
stack<int> numStack;

int x, 
    y,
    toInt;
  
string number;

for (int i = 0; i < posfijo.length(); i++  )
 { // for began  

  if ( isdigit(posfijo[i]) )
  { // if began
   number = posfijo[i];
   stringstream ss( number );
   ss >> toInt;
   numStack.push( toInt );
  } // if end
  
  else if (posfijo[i] != ' ' )
  { // else began
      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 );
  } // else end
  
 } // for end
return numStack.top(); 
} // evaluarRPN end 


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
AnswerRe: the code is correct but the output is wong Pin
toxcct12-Aug-08 1:13
toxcct12-Aug-08 1:13 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 1:19
Maito Gai12-Aug-08 1:19 
GeneralRe: the code is correct but the output is wong Pin
toxcct12-Aug-08 1:29
toxcct12-Aug-08 1:29 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 1:35
Maito Gai12-Aug-08 1:35 
GeneralRe: the code is correct but the output is wong Pin
toxcct12-Aug-08 1:39
toxcct12-Aug-08 1:39 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 1:40
Maito Gai12-Aug-08 1:40 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 2:20
Maito Gai12-Aug-08 2:20 
GeneralRe: the code is correct but the output is wong Pin
toxcct12-Aug-08 2:25
toxcct12-Aug-08 2:25 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 2:37
Maito Gai12-Aug-08 2:37 
GeneralRe: the code is correct but the output is wong Pin
David Crow12-Aug-08 3:50
David Crow12-Aug-08 3:50 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 4:07
Maito Gai12-Aug-08 4:07 
QuestionRe: the code is correct but the output is wong Pin
David Crow12-Aug-08 4:14
David Crow12-Aug-08 4:14 
GeneralRe: the code is correct but the output is wong Pin
David Crow12-Aug-08 3:45
David Crow12-Aug-08 3:45 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 3:49
Maito Gai12-Aug-08 3:49 
GeneralRe: the code is correct but the output is wong Pin
David Crow12-Aug-08 3:51
David Crow12-Aug-08 3:51 
GeneralRe: the code is correct but the output is wong Pin
Maito Gai12-Aug-08 3:58
Maito Gai12-Aug-08 3:58 
QuestionRe: the code is correct but the output is wong Pin
David Crow12-Aug-08 4:04
David Crow12-Aug-08 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.