Stack Operands and Calculate

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//Stack and Queue for calculation
#include <iostream>
#include <stack>
#include <queue>
using namespace 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;
            }                         
	        else if (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.
	}
}
Topic archived. No new replies allowed.