CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Oct 2005
    Location
    USA
    Posts
    22

    C program: infix to postfix

    i am to write a program that converts an expression (entered by user) from infix notation to postfix notation. the expressions will only consist of single-character variables: a, b, c, …, A, B, C, … ; single-digit decimal numbers: 0, 1, 2, …, 9 ; left and right parentheses: (, ) ; and only the following set of operators: *, /, +, -

    and specifications of the assignment are as follows:
    • When testing, consider using the “if” and “if else”, or “while” control structures. You must use the
    “switch” conditional control structure when testing for Operator symbols.
    • Variable (letter) or number – print it.
    • Left parenthesis – push onto stack.
    • Right parenthesis – pop and print each operator from the stack until a left parenthesis is found.
    Discard (do not print) both parentheses. If the null character gets to the top of the stack before a left
    parenthesis, then the parentheses were unbalanced – this is an unrecoverable error.
    • Operator symbol:
    − If this symbol has a higher precedence than the operator at the top of stack, push this symbol
    onto the stack.
    − If this symbol has a lower or equal precedence than the operator at the top of stack, pop and print
    the symbol from the top of stack. (Note that the null character on the stack should be treated as
    an operator with the lowest precedence.)
    − Keep doing this rule until the current symbol gets pushed onto the stack.
    − You must use the “switch” conditional control structure when testing for Operator
    symbols.
    • Null character – pop and print all operators from the stack. If a left parenthesis appears on the stack
    during this process, then the parentheses were unbalanced – this is an unrecoverable error.

    * Note that only operators and left parentheses will ever be pushed onto the stack.


    You must
    write a main() function that does the following:
    1. Allocate a character array that will hold a string of at least 40 characters.

    2. Read an input string from the user and put it in the allocated array. Assume that the string will be a
    legal infix-notation expression, using only the characters specified above. In other words, you don't
    need to test for multi-character symbols or other characters. There must be no whitespace
    characters in the expression, or else a single scanf will not work.

    3. Process each character of the array (string), using the rules above, printing the postfix expression to
    the console (using printf).

    4. If an error is found during expression processing, caused by unbalanced parentheses, then print
    "ERROR: Unbalanced parentheses" and exit the program. (You can use the return statement inside
    the main function to exit the program.) Here are some examples that you can use to test your
    program. In each pair, the top line is the input string, and the bottom line is what should be printed by
    your program.

    Infix: a*b+c*d/2 Postfix: ab*cd*2/+
    Infix: a&b+c*d Postfix: abcd*+&
    Infix: (a&b)+c*d Postfix: ab&cd*+
    Infix: A*((B+C)*D)/2 Postfix: ABC+D**2/
    Input: (a+b Output: ab+ERROR: Unbalanced parentheses
    Input: a+b) Output: ab+ERROR: Unbalanced parentheses
    Input: a*b+((c*d)/2 Output: ab*cd*2/ERROR:Unbalanced parentheses



    problems:
    • it works well with variables but ignores numbers.

    • i don't know how to implement a switch in place of what i already have

    • and the error thing too, i'm not sure how to detect that


    help

    p.s. this is due tomorrow at midnight (11/18 @ 11:59pm)
    Last edited by caramel; November 18th, 2005 at 04:47 PM.
    If you choke a smurf, what color does it turn?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured