i have the program, i just need to evaluate and be able to cout the result somew
ID: 3546393 • Letter: I
Question
i have the program, i just need to evaluate and be able to cout the result someway .
Can some one please help out real quick? thanks!
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Determine if character is one of the four standard operators.
bool isOperator(char character) {
if (character == '+' || character == '-' || character == '*' || character == '/') {
return true;
}
return false;
}
// If the character is not an operator or a parenthesis, then it is assumed to be an operand.
bool isOperand(char character) {
if (!isOperator(character) && character != '(' && character != ')') {
return true;
}
return false;
}
// Return 0 if equal, -1 if op2 is less than op1, and 1 if op2 is greater than op1.
int compareOperators(char op1, char op2)
{
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
{
return -1;
}
else if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/'))
{
return 1;
}
return 0;
}
int main()
{
stack<char> opStack;
string postFixString = "";
char input[100];
// Collect input
cout << "Please enter an expression: ";
cin >> input;
// Get a pointer to our character array.
char *cPtr = input;
// Loop through the array (one character at a time) until we reach the end of the string.
while (*cPtr != '')
{
if (isOperand(*cPtr))
{
postFixString += *cPtr;
}
else if (isOperator(*cPtr))
{
while (!opStack.empty() && opStack.top() != '(' && compareOperators(opStack.top(),*cPtr) <= 0)
{
postFixString += opStack.top();
opStack.pop();
}
opStack.push(*cPtr);
}
else if (*cPtr == '(')
{
opStack.push(*cPtr);
}
else if (*cPtr == ')')
{
while (!opStack.empty())
{
if (opStack.top() == '(')
{ opStack.pop(); break;
}
postFixString += opStack.top();
opStack.pop();
}
}
// Advance our pointer to next character in string.
cPtr++;
}
while (!opStack.empty())
{
postFixString += opStack.top();
opStack.pop();
}
cout << "Postfix is: " << postFixString << endl;
cout << "7" << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Determine if character is one of the four standard operators.
bool isOperator(char character) {
if (character == '+' || character == '-' || character == '*' || character == '/') {
return true;
}
return false;
}
// If the character is not an operator or a parenthesis, then it is assumed to be an operand.
bool isOperand(char character) {
if (!isOperator(character) && character != '(' && character != ')' && character != ' ' && character != '') {
return true;
}
return false;
}
// Return 0 if equal, -1 if op2 is less than op1, and 1 if op2 is greater than op1.
int compareOperators(char op1, char op2)
{
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
{
return -1;
}
else if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/'))
{
return 1;
}
return 0;
}
int evaluat(string postFix)
{
stack<int> intStack;
for (int i = 0; i < (int)postFix.size(); i++)
{
char ch = postFix[i];
if (isOperand(ch))
{
int v = ch - '0';
while ( (i+1)<(int)postFix.size() && isOperand(postFix[i+1]))
{
i++;
v = v * 10 + postFix[i] - '0';
}
intStack.push(v);
}
else if (isOperator(ch))
{
int v1 = intStack.top();
intStack.pop();
int v2 = intStack.top();
intStack.pop();
if (ch == '+')
intStack.push(v2 + v1);
else if (ch == '-')
intStack.push(v2 - v1);
else if (ch == '*')
intStack.push(v2 * v1);
else if (ch == '/')
intStack.push(v2 / v1);
}
}
return intStack.top();
}
int main()
{
stack<char> opStack;
string postFixString = "";
char input[100];
// Collect input
cout << "Please enter an expression: ";
cin >> input;
// Get a pointer to our character array.
char *cPtr = input;
// Loop through the array (one character at a time) until we reach the end of the string.
while (*cPtr != '')
{
if (isOperand(*cPtr))
{
postFixString += *cPtr;
while (*(cPtr+1) != '' && isOperand(*(cPtr+1)))
{
cPtr++;
postFixString += *cPtr;
}
postFixString += " ";
}
else if (isOperator(*cPtr))
{
while (!opStack.empty() && opStack.top() != '(' && compareOperators(opStack.top(),*cPtr) <= 0)
{
postFixString += opStack.top() ;
postFixString += " ";
opStack.pop();
}
opStack.push(*cPtr);
}
else if (*cPtr == '(')
{
opStack.push(*cPtr);
}
else if (*cPtr == ')')
{
while (!opStack.empty())
{
if (opStack.top() == '(')
{ opStack.pop(); break;
}
postFixString += opStack.top();
postFixString += " ";
opStack.pop();
}
}
// Advance our pointer to next character in string.
cPtr++;
}
while (!opStack.empty())
{
postFixString += opStack.top();
postFixString += " ";
opStack.pop();
}
cout << "Postfix is: " << postFixString << endl;
cout << evaluat(postFixString) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.