problems on postfix conversion

This is a discussion on problems on postfix conversion within the C++ Programming forums, part of the General Programming Boards category; i have a problem on converting infix to postfix evaluation and appending the characters in the string if the character ...

  1. #1
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question problems on postfix conversion

    i have a problem on converting infix to postfix evaluation and appending the characters in the string if the character is a digit

    Code:
    #include <iostream.h>
    #include "D:/turboc/bin/stack.h"
    #include <conio.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    void main()
    {
       clrscr();
       Stack *stack=new Stack(100);
       char x[100] ;
       cout<<"Input Equation: ";
       gets(x);
       char dest[100];
       for(int i=0;i<strlen(x);i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		cout<<x[i]; //this must append from the string dest
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stack->push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		cout<<stack->pop(); //this must append from the string dest
    	}
    
    
    
       }
       cout<<"\n\nPress any key to exit";
        getch();
    
    }
    the string dest used in evaluation of a postfix

    Code:
    evaluate(dest);
    if i input as 2(3+6) the result instead of 236+*, it becomes 236+. the first result applies if the input is (2*(3+6)). which lines of code should be edited and why? and how to append a character array and a character popped from the stack into the string? and also, can this code worked with longer equations?why?


    I HOPE you will find the way to correct that problem......

    NOTE:files attached below are .txt files containing c/c++ codes and it should be runnable in TurboC
    Attached Files Attached Files
    Last edited by blacksnake; 04-15-2008 at 01:46 AM.
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    1) Don't use Turbo C/C++.
    2) Use int main (http://cpwiki.sourceforge.net/Void_main)
    3) Never ever use gets, use fgets for C (http://cpwiki.sourceforge.net/buffer_overrun) and std::getline and std::string for C++.
    4) You allocate the stack on the heap when totally unnecessary plus you never delete it.

    Perhaps you should start with correcting the problems in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    Code:
    #include <iostream.h>
    #include "D:/turboc/bin/stack.h"
    #include <conio.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    {
       clrscr();
       Stack *stack=new Stack(100);
       char x[100] ;
       cout<<"Input Equation: ";
       fgets(x);
       char dest[100];
       for(int i=0;i<strlen(x);i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		cout<<x[i]; //this must append from the string dest
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stack->push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		cout<<stack->pop(); //this must append from the string dest
    	}
    
    
    
       }
       cout<<"\n\nPress any key to exit";
        getch();
       return 0;
    
    }
    is this code clear?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Quote Originally Posted by blacksnake View Post
    Code:
    #include "D:/turboc/bin/stack.h" // Get rid of Turbo C
    #include <conio.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main()
    {
       clrscr(); // Avoid if possible, non-standard
       Stack *stack=new Stack(100); // Get rid of new
       char x[100] ;
       cout<<"Input Equation: ";
       fgets(x); // Use std::getline instead
       char dest[100];
       for(int i=0;i<strlen(x);i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		cout<<x[i]; //this must append from the string dest
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stack->push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		cout<<stack->pop(); //this must append from the string dest
    	}
    
    
    
       }
       cout<<"\n\nPress any key to exit";
        getch(); // Avoid if possible
       return 0;
    
    }
    is this code clear?
    Comments are in red.
    But you also mention an evaluation function. Do you have one? The current code just prints out numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    Code:
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    int main()
    {
       
       stack<char> stk;
       char x[100] ;
       cout<<"Input Equation: ";
       getline(cin,x);
       for(int i=0;i<strlen(x);i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		cout<<x[i]; //this must append from the string dest
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stk.push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		cout<<stk.pop(); //this must append from the string dest
    	}
    
    
    
       }
       cout<<"\n\nPress any key to exit";
    
       return 0;
    
    }
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Code:
    char x[100] ;
    Should be
    Code:
    string x;
    Otherwise it shouldn't compile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    Code:
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    int main()
    {
       
       stack<char> stk;
       string x;
       cout<<"Input Equation: ";
       getline(cin,x);
       for(int i=0;i<x.length();i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		dest+=x[i];
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stk.push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		dest+=stk.pop(); 
    	}
    
    
    
       }
    
       cout<<"The Prefix value is: "<<dest;
       cout<<"\n\nPress any key to exit";
    
       return 0;
    
    }
    Last edited by blacksnake; 04-15-2008 at 02:53 AM.
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    "dest" is undefined. I believe you want it defined as a std::string, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    Code:
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    int main()
    {
       
       stack<char> stk;
       string x, dest;
       cout<<"Input Equation: ";
       getline(cin,x);
       for(int i=0;i<x.length();i++)
       {
    
    	if(isdigit(x[i]))
    	{
    		dest+=x[i];
    
    	}
    	else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    	{
    		stk.push(x[i]);
    	}
    	else if(x[i]==')')
    	{
    		dest+=stk.pop(); 
    	}
    
    
    
       }
    
       cout<<"The Prefix value is: "<<dest;
       cout<<"\n\nPress any key to exit";
    
       return 0;
    
    }
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your code doesn't seem to handle the opening bracket at all. Are you sure, though, that you want to allow expressions such as 2(3+4) instead of requiring typing all operators explicitly?

    Neither does your code appear to handle operator precedence.

    I suggest reading about the shunting yard algorithm.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    hello blacky,
    i may be wrong but as far as i know the algorithm itself is wrong.You are popping the stack only when you encounter ")" which is entirely wrong

    Here's the algorithm that may help you..

    1) if you encounter "(" push on it on stack
    2) if you encounter operators (+,-,*,/) then compare the operators with the top of the stack.As long as the top of the stack has operators with higher or equal priority to the scanned operators you have to keep popping the stack and store it in result

    Lastly you have to push the scanned operator on the stack

    3)If you encounter any digits or alphabets then store in in result

    4)if you encounter ")" keep popping the stack unless you encounter "(" on the stack and store it in result

    Lastly decrement top by 1 as "(" is of no importance to us

    These are the four steps you have to follow.Now follow this and try to do it. If you still face difficulty then tell me i will give you the working code for it

    -Chotti

  12. #12
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    Quote Originally Posted by chottachatri View Post
    hello blacky,
    i may be wrong but as far as i know the algorithm itself is wrong.You are popping the stack only when you encounter ")" which is entirely wrong

    Here's the algorithm that may help you..

    1) if you encounter "(" push on it on stack
    2) if you encounter operators (+,-,*,/) then compare the operators with the top of the stack.As long as the top of the stack has operators with higher or equal priority to the scanned operators you have to keep popping the stack and store it in result

    Lastly you have to push the scanned operator on the stack

    3)If you encounter any digits or alphabets then store in in result

    4)if you encounter ")" keep popping the stack unless you encounter "(" on the stack and store it in result

    Lastly decrement top by 1 as "(" is of no importance to us

    These are the four steps you have to follow.Now follow this and try to do it. If you still face difficulty then tell me i will give you the working code for it

    -Chotti
    can you prove about the 2nd step in this kind of algorithm
    ?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by blacksnake View Post
    can you prove about the 2nd step in this kind of algorithm
    ?
    What do you want to prove about it? Think about 9 * 8 + 4: that needs to be 9 8 * 4 +, while 9 + 8 * 4 is 9 8 4 * +. The operator precedence is going to tell you which is which.

  14. #14
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    i was understood on appending the integers the problem was a precedence...can u show the code to me on the precedence of operators?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by blacksnake View Post
    i was understood on appending the integers the problem was a precedence...can u show the code to me on the precedence of operators?
    I have no idea what you mean by that. Do you mean what is the precedence of operators? * and / are equally high and + and - are equally low. If you want details about the algorithm, you should go back to anon's post and maybe click the link this time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 06:44 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  4. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2008, 11:06 PM
  5. postfix
    By datainjector in forum C Programming
    Replies: 11
    Last Post: 11-20-2002, 07:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21