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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*Ask the user to enter the option to pick between infix, prefix, or postfix.
2.Allow the user to enter a mathematical expression per the option above.
3.Allow the user to enter the option to pick between infix, prefix, or postfix.
4.Ensure that the user does not pick the same exact option again.
5.Validate that the expression entered is correct per the option picked in step 1.
6.Evaluate the expression entered per the option selected in step 1.
7.Display the value produced in the above step.
8.Convert the expression to be represented per the option picked in step 3.
9.Display the new expression after being converted in the above step.
10.Evaluate the expression after being converted.
11.Display the value produced in the above step.
*/
#include <cstdlib>
#include <stack>
#include <string>
#include <iostream> // ***** added
/*
void getEquation(stack<string> & equation);
void getConversion(stack<string> equation, int choice);
void getEvaluation(int choice, stack<string> & equation, int choice2);
void getValueConvert(int num1, int num2, int choice2, string operation);
*/
void getEquation( std::stack<std::string> & equation); // ****
void getConversion(std::stack<std::string> equation, int choice); // ****
void getEvaluation(int choice, std::stack<std::string> & equation, int choice2); // ****
void getValueConvert(int num1, int num2, int choice2, std::string operation); // ****
//put the voids for each method here
using namespace std;
int main()
{
stack<string> equation;
getEquation(equation);
return 0;
}
void getEquation(stack<string> & equation)
{
string num1, num2, operation;
int choice;
cout << "1. Infix. " << endl;
cout << "2. Prefix. " << endl;
cout << "3. Postfix. " << endl;
cin >> choice;
switch(choice)
{
case 1:
{
cout << "Enter the first number" << endl;
cin >> num1;
cout << "Enter the operation. " << endl;
cin >> operation;
cout << "Enter the second number. " << endl;
cin >> num2;
equation.push(num1);
equation.push(operation);
equation.push(num2);
}
break;
case 2:
{
cout << "Enter the operation" << endl;
cin >> operation;
cout << "Enter number one. " << endl;
cin >> num1;
cout << "Enter the second number. " << endl;
cin >> num2;
equation.push(operation);
equation.push(num1);
equation.push(num2);
}
break;
case 3:
{
cout << "Enter the first number. " << endl;
cin >> num1;
cout << "Enter the second number. " << endl;
cin >> num2;
cout << "enter the operation. " << endl;
cin >> operation;
equation.push(num1);
equation.push(num2);
equation.push(operation);
}
break;
default:
getEquation(equation);
}
getConversion(equation, choice);
}
void getConversion(stack<string> equation, int choice)
{
int choice2;
cout << "What type of equation do you want to convert??. " << endl;
cout << "1. Infix. " << endl;
cout << "2. Prefix. " << endl;
cout << "3. Postfix. " << endl;
cin >> choice2;
while(choice == choice2)
{
cout << "No. " << endl;
cin >> choice2;
}
getEvaluation(choice, equation, choice2);
}
void getEvaluation(int choice, stack<string> & equation, int choice2)
{
int num1 = 0, num2 = 0 ; // ****
string operation;
switch(choice)
{
case 1:
{
num2 = stoi(equation/*.pop()*/ .top() ); // ****
operation = equation/*.pop()*/ .top(); // ****
num1 = stoi(equation/*.pop()*/ .top() ); // ****
}
break;
case 2:
{
num2 = stoi(equation/*.pop()*/ .top() ); // ****
num1 = stoi(equation/*.pop()*/ .top() ); // ****
operation = equation/*.pop()*/ .top(); // ****
}
break;
case 3:
{
operation = equation/*.pop()*/ .top(); // ****
num2 = stoi(equation/*.pop()*/ .top() ); // ****
num1 = stoi(equation/*.pop()*/ .top() ); // ****
}
break;
}
double answer;
if(operation == "+" )
{
answer = num1 + num2;
}
else if(operation == "-")
{
answer = num1 - num2;
}
else if(operation == "*")
{
answer = num1 * num2;
}
else if(operation == "/")
{
answer = num1 / num2;
}
cout << answer << endl;
getValueConvert(num1, num2, choice2, operation);
}
void getValueConvert(int num1, int num2, int choice2, string operation)
{
switch(choice2)
{
case 1:
cout << num1 << operation << num2 << endl;
break;
case 2:
cout << operation << num1 << num2 << endl;
break;
case 3:
cout << num1 << num2 << operation << endl;
break;
}
double answer;
if(operation == "+" )
{
/*t*/ answer = num1 + num2; // ******
}
else if(operation == "-")
{
answer = num1 - num2;
}
else if(operation == "*")
{
answer = num1 * num2;
}
else if(operation == "/")
{
answer = num1 / num2;
}
cout << answer << endl;
}
|