USING “Data Abstraction and Problem Solving with C++”. I need help with this pro
ID: 3776110 • Letter: U
Question
USING “Data Abstraction and Problem Solving with C++”. I need help with this program. Another expert helped with this question, but did not give answer in correct language and corrections was incomplete. Here is the link. https://www.chegg.com/homework-help/questions-and-answers/using-data-abstraction-problem-solving-c-5th-edition-addison-wesley-2006-paperback-isbn-03-q15799157
I am confused on how to complete the program. Please help me out. They posted some of the program but not all in the comments section below.
Explanation / Answer
#include <STACK>
#include <iostream>
using namespace std;
int balanced(void);
#define MAXSIZE 50
char infix[MAXSIZE];
char stack[MAXSIZE/2];
int top=-1;
int balanced()
{
int i;
for(i=0;infix[i]!='';i++)
{
switch(infix)
{
case '{': top++;
stack[top]='{';
break;
case '[': top++;
stack[top]='[';
break;
case '(': top++;
stack[top]='(';
break;
case '}': if(stack[top]=='{')
top--;
break;
case ']': if(stack[top]=='[')
top--;
break;
case ')': if(stack[top]=='(')
top--;
break;
}
}
if(top==-1)
return 1;
return 0;
}
int GetResult( char * infix)
{
std::stack<int> myStack;
int nr1, nr2; int length = strlen(infix);
for (int i = 0; i < length; i++)
{
if (isdigit(infix[i])
{
myStack.push(infix[i] - '0');
}
else
{
switch(infix[i])
{
case '+':
nr1 = myStack.top();
myStack.pop();
nr2 = myStack.top();
myStack.pop();
myStack.push(nr2 + nr1);
break;
case '-':
nr1 = myStack.top();
myStack.pop();
nr2 = myStack.top();
myStack.pop();
myStack.push(nr2 - nr1);
break;
case '*':
nr1 = myStack.top();
myStack.pop();
nr2 = myStack.top();
myStack.pop();
myStack.push(nr2 * nr1);
break;
case '/':
nr1 = myStack.top();
myStack.pop();
nr2 = myStack.top();
myStack.pop();
myStack.push(nr2 / nr1);
break;
default:
break;
}
}
}
return myStack.top();
}
int main(int argc, char* argv[])
{
cout << "Enter the infix expression ";
cin >> "%s",infix;
char *infix = "a+b*c";
if(balanced()==1)
cout << "The expression is balanced ";
int res =getResult(infix);
cout << "%i", res;
return 0;
else
cout << "The expression is unbalanced ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.