Hey, Here is a Code that takes in integers then engueues operators and does the math reguired.
any suggestions or fixes to shorten the code or even to get it all into a stack Please let me know.
//Stack and Queue for calculation
#include <iostream>
#include <stack>
#include <queue>
usingnamespace std;
void evaluate(int);
void input(int);
stack <int> operands;
queue <char> operators;
void main()
{ //Main Program, Runs and asks person for Total number of integers
int Max_List; // for a baseline of how many int's and operators are going to be used
cout<< "Input the Total number of Integers you are going to use" << endl;
cin>> Max_List; // Reads in the baseline Number for totals
evaluate(Max_List); // goes to evaluate.
}
void evaluate(int max) //Evaluate conducts the input as well as evaluation.
{ // Input runs, once finished, evaluate starts back up.
input(max); // Input(size Max) basically initializes through the user for Evaluate to work.
int test = max; // Used for Decrease without effecting Max size.
int result, temp1, temp2;
char op; // Char is the Operators command. For use with the Queue operators
while(max > 0) // Runs while max Size is greater than zero. which means not empty.
{
if(!operands.empty() && max > 1) //If Statement operands are not empty, and Stack size is greater then one.
{
temp1 = operands.top(); //Makes Temp1 the top number in stack
operands.pop(); // gets rid of top number already used
temp2 = operands.top(); // makes temp2 the top number in stack
operands.pop(); // gets rid of top number already used
op = operators.front(); // Makes OP the next operator to be used
operators.pop(); // gets rid of the operator in use
switch (op) //Conducts the math and stores into result
{
case'/': result = temp1 / temp2; //Divide
break;
case'*': result = temp1 * temp2; // Multiply
break;
case'-': result = temp1 - temp2; // Subtract
break;
case'+': result = temp1 + temp2; // Plus
break;
case'%' : result = temp1 % temp2; // Remainder of division
break;
default: cout<< " Did not work, Error!!" << endl;
}
operands.push(result); // Puts the result back ontop of the stack
cout<< "Operation performed was " << temp1 << " " << op << " " << temp2 << endl;
cout << "Which equaled " << " = " << result << endl;
}
elseif (max == 1) //If the stack only has one number left, It is the Final result.
{
cout<< operands.top() << " Is your Final result" << endl; //So The Result is printed.
}
max --; // Decreases the Stack size by one every run.
}
}
void input(int max) // Is read by the evaluate function, then it performs to create a Stack and a Queue
{
int Num; // Variable to take in the Max numbers to be used
char Op; //Operator class Char
int opmaker = max-1; //OpMaker is the Max limit for constructing the Operator Queue
while(max > 0) // This loop makes the Stack for Integers
{
cout<< "Input Number to be calculated:" << endl;
cin>> Num;
operands.push(Num);
max --;
}
while(opmaker > 0) // This loop makes the Queue for Operators
{
cout<< "Input an operator to be used: " <<endl;
cin>> Op;
switch (Op) //Conducts the math and stores into result
{
case'/': operators.push(Op); //Divide
opmaker--; break;
case'*': operators.push(Op); // Multiply
opmaker--; break;
case'-': operators.push(Op);// Subtract
opmaker--; break;
case'+': operators.push(Op); // Plus
opmaker--; break;
case'%' : operators.push(Op); // Remainder of division
opmaker--; break;
default: cout<< " Enter a Mathmatical Operation" << endl; //The Switch Catch that tells the user to input
} // a operator in another way.
}
}