Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:43:25+00:00 2026-06-15T08:43:25+00:00

Writing a program that uses a linked list stack to convert an equation in

  • 0

Writing a program that uses a linked list stack to convert an equation in infix notation to postfix notation. The stack portion of the program is its own class and is in its own header file, and is implemented correctly (able to compile and run the test main provided by my professor). I’m currently working on the actual function to convert an infix string to a postfix string in an inherited class. I have the following code, which I think has done the shunting yard algorithm incorrectly somewhere since I get odd output.

template <class myType>
void infixToPostfix<myType>::convertToPostfix()
{
    linkedStack<char> newS; //creating a new char stack
    string output; //creating the postfix output string

    for(int i = 0; i < infx.length(); i++) //cycle through the entire string
    {
        if (isgraph(infx[i])) //skip spaces
        {
            if (isalpha(infx[i])) //if the char is a letter (caps or uncaps)
            {
                output += infx[i]; //append it to output
            }
            else
            {
                if (newS.isEmptyStack()) newS.push(infx[i]);
                else if (precedence(infx[i], newS.top()) //check if the current char has higher precedence than the top of the stack, or if the stack is empty
                {
                    newS.push(infx[i]); //push the current char onto the stack
                }
                else if (infx[i] == ')') //check if the current char is a closing paren
                {
                    for(;;)
                    {
                        if (newS.isEmptyStack()) break;
                        if (newS.top() != '(') //check if the top of the stack isn't an open paren
                        {
                            output += newS.top(); //append the top of the stack to the output
                            newS.pop(); //pop the top of the stack off
                        }
                        else //the top of the stack is a (
                        {
                            newS.pop(); //pop the ( off the stack
                            break; //break out of the for loop
                        }
                    }
                }
                else //the current char doesn't have higher precedence than the top of the stack
                {
                    output += newS.top(); //append to the top of the stack to output
                    newS.pop(); //pop off the top of the stack
                    newS.push(infx[i]); //put the current char onto the top of the stack
                }
            }
        }
    }

    while (!newS.isEmptyStack()) //not sure if this works, assuming we're at the end of the line at this point, and if there's anything on the stack we need to append it to the output
    {
        output += newS.top();
        newS.pop();
    }

    pfx = output; //setting pfx to the output (pfx is the class variable for the postfix output)

}

My function to show the postfix string is as follows

template <class myType>
void infixToPostfix<myType>::showPostfix()
{
    cout << "Postfix Expression: " << pfx << endl;
}

I get the following output when I run the program.

silverbox@silverbox-VirtualBox:~/Dropbox/CS202/ass13$ ./a.out testinput1.txt
Infix Expression: A + B - C
-ostfix Expression: AB+C
Infix Expression: A + B * C
*+stfix Expression: ABC
Infix Expression: A * B + C / D
/+stfix Expression: AB*CD
Infix Expression: (A + B) * C
*+(tfix Expression: AB)C
Infix Expression: A * (B + C) / D
/+(tfix Expression: A*BC)D
Infix Expression: (A + B) * (C - D)
-(+(fix Expression: AB)*CD)
Infix Expression: A * (B + C / D)
/+(tfix Expression: A*BCD)
Infix Expression: A + ((B + C) * (E - F) - G) / (H - I)
-(--(+( Expression: A+(BC)*EF)G)/HI)
silverbox@silverbox-VirtualBox:~/Dropbox/CS202/ass13$ 

I honestly don’t understand why what looks like odd bits of the postfix expression get pushed over onto the string in my cout. Any hints/help?

Edit: My output is as follows after making the change suggested by ymett. I’m now attempting to figure out where I went wrong in attempting to handle parentheses.

silverbox@silverbox-VirtualBox:~/Dropbox/CS202/ass13$ ./a.out testinput1.txt
Infix Expression: A + B - C
Postfix Expression: AB+C-
Infix Expression: A + B * C
Postfix Expression: ABC*+
Infix Expression: A * B + C / D
Postfix Expression: AB*CD/+
Infix Expression: (A + B) * C
Postfix Expression: AB)C*+(
Infix Expression: A * (B + C) / D
Postfix Expression: A*BC)D/+(
Infix Expression: (A + B) * (C - D)
Postfix Expression: AB)*CD)-(+(
Infix Expression: A * (B + C / D)
Postfix Expression: A*BCD)/+(
Infix Expression: A + ((B + C) * (E - F) - G) / (H - I)
Postfix Expression: A+(BC)*EF)G)/HI)-(--(+(
silverbox@silverbox-VirtualBox:~/Dropbox/CS202/ass13$ 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:43:26+00:00Added an answer on June 15, 2026 at 8:43 am

    You haven’t shown how infx is filled, but it would appear that it has a carriage return (CR, ‘\r’, 0x0d, 13) at the end.

    Your condition infx[i] != ' ' should be replaced by a condition that checks for any blank character, ie !isblank(infx[i]). Better yet, instead of checking for characters you don’t want, check for characters you do want; if you don’t have a list use isgraph(infx[i]) (any character with a visible representation, ie not a control characters or a space).

    The condition infx[i] >= 65 && infx[i] <= 122 isn’t good either. Firstly you should use character literals rather than numbers, ie infx[i] >= 'A' && infx[i] <= 'z'. Secondly you have included characters between 'Z' and 'a' which are not letters, so it should be (infx[i] >= 'A' && infx[i] <= 'Z') || (infx[i] >= 'a' && infx[i] <= 'z'). Also you are assuming letters are contiguous in the character set which is true for ASCII but not for all character sets (although perhaps we don’t have to worry about that). Better use the tools the language gives you, and write isalpha(infx[i]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that uses prints a hex dump of its input.
I'm writing a program that uses a Hashtable of linked lists to count the
I'm writing a program that uses regular expressions to make comparisons against the entire
I'm writing a python program that uses scons to build an .exe and then
I'm planning on writing a program for Linux that uses text to speech and
I'm writing a custom text-to-speech program that uses SAPI 5, and one problem I'm
I'm writing a program that for performance reasons uses shared memory (sockets and pipes
I'm writing an MPI program (Visual Studio 2k8 + MSMPI) that uses Boost::thread to
I'm writing a program that uses a mean filter to smooth an image. right
I'm writing a program that uses QUdpSocket for transmitting data over the network. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.