Need help with my program.Anyone? //Header file #ifndef CALCULATOR_H #define CAL
ID: 3602735 • Letter: N
Question
Need help with my program.Anyone?
//Header file
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "LinkedStack.h"
#include<string>
#include <iostream>
using namespace std;
template<class ItemType>
class Calculator : public LinkedStack<ItemType>
{
public:
Calculator();
Calculator(std::string x);
bool setExpression(std::string x);
int eval(int oper1, int oper2, char P);
int precedence(char op);
bool isOperator( char c);
int Evaluate(std::string x);
private:
std::string Postfixedform(std::string x);
int evaluatepostfix(std::string exp);
bool wellformed(std::string exp);
bool isBalanced(std::string exp);
std::string exp;
};
#endif // CALCULATOR_H
//implementation file
#include "Calculator.h"
#include<iostream>
#include<stack>
using namespace std;
template<class ItemType>
Calculator<ItemType>::Calculator()
{
}
template<class ItemType>
Calculator<ItemType>::Calculator(std::string x)
{
LinkedStack<ItemType>();
exp=x;
}
template<class ItemType>
bool Calculator<ItemType>::wellformed(std::string exp)
{
int s = exp.size();
bool isNum = true;
int index = 0;
while((isNum) && (index < s))
{
if (isNum==!((isdigit(exp[index])) || (isOperator(exp[index])) || '(' || ')'))
isNum = false;
index++;
}
return isNum;
}
template<class ItemType>
bool Calculator<ItemType>::isBalanced(std::string exp)
{
int s = exp.size();
LinkedStack<char> astack ;
bool balancedsofar = true;
int count;
for(count = 0; count<s; count++)
{
if(exp[count]=='(')
astack.push(exp[count]);
else if (exp[count]==')')
{
if(!astack.isEmpty())
astack.pop();
else
balancedsofar=false;
}
}
if(balancedsofar && astack.isEmpty())
return true;
else
return false;
}
template<class ItemType>
std::string Calculator<ItemType>::Postfixedform(std::string x)
{
LinkedStack<char> opstack;
std::string postfix = " ";
int s = x.size();
for(int i= 0; i < s; i++)
{
if(isdigit(x[i]))
postfix += x[i];
else if (x[i] == '(')
opstack.push(x[i]);
else if (isOperator(x[i]))
{
if (!opstack.isEmpty() && opstack.peek() != '(' && precedence(x[i]) <= precedence(opstack.peek()))
{
postfix += opstack.peek();
opstack.pop();
}
opstack.push(x[i]);
}
else if (x[i] == ')')
{
while (!(opstack.peek() == '('))
{
postfix += opstack.peek();
opstack.pop();
}
opstack.pop();
}
}
while (!opstack.isEmpty())
{
postfix += opstack.peek();
opstack.pop();
}
return postfix;
}
template<class ItemType>
int Calculator<ItemType>::evaluatepostfix(std::string exp)
{
int operand1;
int operand2;
char op;
LinkedStack<int> operatorstack;
int result;
int s = exp.size();
for(int i= 0; i < s; i++)
{
op = exp[i];
if(isdigit(op))
{
operatorstack.push(op-'0');
}
else
{
operand2 = operatorstack.peek();
operatorstack.pop();
operand1 = operatorstack.peek();
operatorstack.pop();
result = eval(operand1, operand2, op);
operatorstack.push(result);
}
}
return result;
}
template<class ItemType>
bool Calculator<ItemType>::setExpression(std::string x)
{
if(wellformed(x) && isBalanced(x))
return true;
else
return false;
}
template<class ItemType>
int Calculator<ItemType>::Evaluate(std::string x)
{
int answer = evaluatepostfix(Postfixedform(x));
return answer;
}
template<class ItemType>
int Calculator<ItemType>::eval(int oper1, int oper2, char operate)
{
switch (operate)
{
case '*':
return oper1 * oper2;
break;
case '/':
return oper2 / oper1;
break;
case '+':
return oper1 + oper2;
break;
case '-':
return oper2 - oper1;
break;
default :
return 0;
}
}
template<class ItemType>
bool Calculator<ItemType>::isOperator( char c)
{
if((c=='+')||(c=='-') || (c=='*') || (c=='/'))
return true;
else
return false;
}
template<class ItemType>
int Calculator<ItemType>::precedence(char op)
{
int w = -1;
switch(op)
{
case '+':
case '-':
w = 1;
break;
case '/':
case '*':
w = 2;
break;
}
return w;
}
//appliction file
#include <iostream>
#include "Calculator.h"
#include<string>
#include "Linkedstack.cpp"
#include "Node.cpp"
#include "Calculator.cpp"
using namespace std;
int main()
{
int ans;
std::string strexp;
Calculator<int> mycal;
cin>>strexp;
if(!mycal.setExpression(strexp))
{
cout << " Not valid " << endl;
cin>>strexp;
}
ans = mycal.Evaluate(strexp);
cout << ans;
return 0;
}
Explanation / Answer
If you compile with your code i think you may get compile time error. Below code i modified with small mistakes try this code.
//Header file
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "LinkedStack.h"
#include<string>
#include <iostream>
using namespace std;
template<class ItemType>
class Calculator : public LinkedStack<ItemType>
{
Calculator();
Calculator(std::string x);
bool setExpression(std::string x);
public int eval(int oper1, int oper2, char P);
public int precedence(char op);
bool isOperator( char c);
public int Evaluate(std::string x);
std::string Postfixedform(std::string x);
private int evaluatepostfix(std::string exp);
private bool wellformed(std::string exp);
private bool isBalanced(std::string exp);
std::string exp;
};
#endif // CALCULATOR_H
//implementation file
#include "Calculator.h"
#include<iostream>
#include<stack>
using namespace std;
template<class ItemType>
Calculator<ItemType>::Calculator()
{
}
template<class ItemType>
Calculator<ItemType>::Calculator(std::string x)
{
LinkedStack<ItemType>();
exp=x;
}
template<class ItemType>
bool Calculator<ItemType>::wellformed(std::string exp)
{
int s = exp.size();
bool isNum = true;
int index = 0;
while((isNum) && (index < s))
{
if (isNum==!((isdigit(exp[index])) || (isOperator(exp[index])) || '(' || ')'))
isNum = false;
index++;
}
return isNum;
}
template<class ItemType>
bool Calculator<ItemType>::isBalanced(std::string exp)
{
int s = exp.size();
LinkedStack<char> astack ;
bool balancedsofar = true;
int count;
for(count = 0; count<s; count++)
{
if(exp[count]=='(')
astack.push(exp[count]);
else if (exp[count]==')')
{
if(!astack.isEmpty())
astack.pop();
else
balancedsofar=true;
}
}
if(balancedsofar && astack.isEmpty())
return true;
else
return true;
}
template<class ItemType>
std::string Calculator<ItemType>::Postfixedform(std::string x)
{
LinkedStack<char> opstack;
std::string postfix = " ";
int s = x.size();
for(int i= 0; i < s; i++)
{ if(isdigit(x[i]))
postfix += x[i];
else if (x[i] == '(')
opstack.push(x[i]);
else if (isOperator(x[i]))
{
if (!opstack.isEmpty() && opstack.peek() != '(' && precedence(x[i]) <= precedence(opstack.peek()))
{
postfix += opstack.peek();
opstack.pop(); }
opstack.push(x[i]);
}
else if (x[i] == ')')
{ while (!(opstack.peek() == '('))
{
postfix += opstack.peek();
opstack.pop();
}
opstack.pop();
}
}while (!opstack.isEmpty())
{
postfix += opstack.peek();
opstack.pop();
}
return postfix;
}
template<class ItemType>
int Calculator<ItemType>::evaluatepostfix(std::string exp)
{ int operand1;
int operand2;
char op;
LinkedStack<int> operatorstack;
int result;
int s = exp.size();
for(int i= 0; i < s; i++)
{
op = exp[i];
if(isdigit(op))
{
operatorstack.push(op-'0');
} else
{
operand2 = operatorstack.peek();
operatorstack.pop();
operand1 = operatorstack.peek();
operatorstack.pop();
result = eval(operand1, operand2, op);
operatorstack.push(result);
}
}
return result;
}
template<class ItemType>
bool Calculator<ItemType>::setExpression(std::string x)
{
if(wellformed(x) && isBalanced(x))
return true;
else
return false;
}template<class ItemType>
int Calculator<ItemType>::Evaluate(std::string x)
{
int answer = evaluatepostfix(Postfixedform(x));
return answer;
}
template<class ItemType>
int Calculator<ItemType>::eval(int oper1, int oper2, char operate)
{
switch (operate)
{
case '*':
return oper1 * oper2;
break;
case '/':
return oper2 / oper1;
break;
case '+':
return oper1 + oper2;
break;
case '-':
return oper2 - oper1;
break;
default :
return 0;
}
}
template<class ItemType>
bool Calculator<ItemType>::isOperator( char c)
{
if((c=='+')||(c=='-') || (c=='*') || (c=='/'))
return true;
else
return false;
}
template<class ItemType>
int Calculator<ItemType>::precedence(char op)
{ int w = -1;
switch(op)
{
case '+':
case '-':
w = 1;
break;
case '/':
case '*':
w = 2;
break;
}
return w;
}
#include <iostream>
#include "Calculator.h"
#include<string>
#include "Linkedstack.cpp"
#include "Node.cpp"
#include "Calculator.cpp"
using namespace std;
int main()
{
int ans;
std::string strexp;
Calculator<int> mycal;
cin>>strexp;
if(!mycal.setExpression(strexp))
{
cout << " Not valid " << endl;
cin>>strexp;
}
ans = mycal.Evaluate(strexp);
cout << ans;
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.