Question

Please use C++ only Purpose : This stage involves recursion and recursion-like reasoning in coding while...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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<<"\nEnter the infix string";
cout<<"\n>>";
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<<"\n Postfix expression : \n";
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

Enter the infix string
>>67 + ( 32 / 8 ) * 3 rt 8
Postfix expression : 
67 32 8 / 3 8 rt * + 
Add a comment
Know the answer?
Add Answer to:
Please use C++ only Purpose : This stage involves recursion and recursion-like reasoning in coding while...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • PLEASE HURRY! I only have 30 minutes for these questions. thank you! Following is an incorrect...

    PLEASE HURRY! I only have 30 minutes for these questions. thank you! Following is an incorrect pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced: declare a character stack while ( more input is available) read a character if ( the character is a) push it on the stack pop a character off the stack print "unbalanced" and exit else if ( the character is a ')' and the stack is not empty)...

  • The second project involves completing and extending the C++ program that evaluates statements of an expression...

    The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study. The statements of that expression language consist of an arithmetic expression followed by a list of assignments. Assignments are separated from the expression and each other by commas. A semicolon terminates the expression. The arithmetic expressions are fully parenthesized infix expressions containing integer literals and variables. The valid arithmetic operators are +, –, *, /. Tokens...

  • Coding in C. Please only use stdio.h (which would mean no malloc or anything like that)...

    Coding in C. Please only use stdio.h (which would mean no malloc or anything like that) (30 pts) Write a sorting program using an array. First read n integers and store them in an array. Then use any sorting algorithm (you can choose any one) to sort these numbers in ascending order. Turn in your code and input/result together. For example, if your input is 10 -9 5 1000 -9 1 20 -100 then your output should be -100 -9...

  • In C++, Please help me compute the following flowchart exactly like this requirement: Use while loops...

    In C++, Please help me compute the following flowchart exactly like this requirement: Use while loops for the input validations required: number of employees(cannot be less than 1) and number of days any employee missed(cannot be a negative number, 0 is valid.) Each function needs a separate flowchart. The function charts are not connected to main with flowlines. main will have the usual start and end symbols. The other three functions should indicate the parameters, if any, in start; and...

  • Fix this C++ below so does not use any loops at all. Use recursion instead. here...

    Fix this C++ below so does not use any loops at all. Use recursion instead. here is some guides given. Create a directory to hold assignment 3. Copy files hailstone.cpp, Makefile and dotestassignment 2 to the assignment 3 directory. Edit the comments at the top of hailstone.cppto say that this is assignment 3. You should be able to keep the same contracts, 'next' function and 'main' function from assignment 2, unless they contained errors that needed to be fixed. If...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT