Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use C++ only Purpose : This stage involves recursion and recursion-like r

ID: 3728613 • Letter: P

Question

Please use C++ only

Purpose :

This stage involves recursion and recursion-like reasoning in coding while also actively preparing a critical component necessary.

Description :

Computers don’t natively understand mathematical expressions in the way we prefer to write them. While we prefer “infix” notation, where the operators come between the values being operated upon, computers prefer either “prefix” or “postfix” notation, as these are unambiguous and easier for them to operate upon. As the project, at its core, is to write a mathematical calculator, we will need to convert math expressions to one or the other at some point. The requirement of this project stage is to implement the Shunting-Yard algorithm (http://en.wikipedia.org/wiki/Shunting-yard_algorithm) as it applies to the overall design of this project. You do not need to calculate any values – the goal is to simply turn any input  infix expression into a postfix expression, an analysis that will aid the later stages of this project. You may assume that there will always be spaces between any values and operators, as this can aid parsing and reduce the complexity of your input code.

Example input/output:

> 67 + ( 32 / 8 ) * 3 rt 8

67 32 8 / 3 8 rt * +

Please keep the prompt as you see it in this example – just a leading “>” to request input – as this will aid us in our grading efforts.

Some quick web searches can easily find you example code for this problem online. Keep in mind that their examples will not likely fully apply to our project spec (use of “rt” as an operator) and will likely need further modification, so whatever you choose to use, you’re responsible for knowing how it works and making any modifications necessary for this stage’s implementation. You will be reusing this code again in the project’s final stage. Secondly, while such online examples do exist, you may find it a worthwhile exercise to attempt your own implementation from scratch, as the necessary logic provides great programming practice. I personally believe you would be best served by referencing the Wikipedia article’s explanation and translating that into code.

Explanation / Answer

Please upvote if this helps

Code

#include <iostream>
#include <iterator>
#include <stack>
#include <sstream>
#include <vector>
#include <math.h>
using namespace std;

bool findIfDigit(const string &symbol);
int getPriority(const string &c);
bool isOperator(const string &c);

int main()
{ string infix;
cout<<" Enter the infix string";
cout<<" >>";
getline(cin,infix);
istringstream iss(infix);
//consider each element as token and store in the vector tokens removing spaces
vector<string> tokens;
while(iss)
{
string temp;
iss >>temp;
tokens.push_back(temp);
}
//define output vector
vector<string> postfix;
  
//define stack to store intermediate operators
stack<string> s;

//read in reverse order that is right to left
for(unsigned int i = 0; i < tokens.size(); i++)
{
if(findIfDigit(tokens[i]))
{
postfix.push_back(tokens[i]);
}
if(tokens[i] == "(")
{
s.push(tokens[i]);
}
if(tokens[i] == ")")
{
while(!s.empty() && s.top() != "(")
{
postfix.push_back(s.top());
s.pop();
}
s.pop();
}
if(isOperator(tokens[i]) == true)
{
while(!s.empty() && getPriority(s.top()) >= getPriority(tokens[i]))
{
postfix.push_back(s.top());
s.pop();
}
s.push(tokens[i]);
}
}
//pop any remaining operators from the stack and insert to outputlist
while(!s.empty())
{
postfix.push_back(s.top());
s.pop();
}

cout<<" Postfix expression : ";
for(unsigned int i = 0; i < postfix.size(); i++)
{
cout<<postfix[i]<<" ";
}
return 0;
}
bool findIfDigit(const string &symbol)
{
bool isNumber = false;
for(unsigned int i = 0; i < symbol.size(); i++)
{
if(!isdigit(symbol[i]))
{
isNumber = false;
}
else
{
isNumber = true;
}
}
return isNumber;
}
//get the priority , higher is the priority higher is the number returned
int getPriority(const string &c)
{
if(c == "^")
{
return 4;
}
if( c == "rt" )
{
return 3;
}
if(c == "*" || c == "/")
{
return 2;
}
if(c== "+" || c == "-")
{
return 1;
}
else
{
return 0;
}
}
//defining operators
bool isOperator(const string &c)
{
return (c == "+" || c == "-" || c == "*" || c == "/" || c == "^" || c == "rt");
}

Output