In C language In this assignment a mathematical calculator o

In C++ language

In this assignment, a mathematical calculator of single digit operations should be implemented. The calculator operates only on postfix expressions, following the algorithm seen in class. However the program should accept any single digit expression, either following postfix or infix or postfix. The program has three main functionalities:

- Evaluate a postfix expression

- Evaluate an infix expression (after converting it to a postfix notation, and using the postfix evaluation algorithm)

- Compare two expressions: two expressions are equal if they evaluate to the same resulting value, otherwise they are considered different The following requirement apply: • Calculator should accept any single digit integer expression, no matter how long it is. • Use a dynamic integer stack, which header file is shown in page 1081 of textbook. • A abstract class “Expression” should be designed, with (at least) one pure virtual method called “evaluate”. • Two derived class should be designed: “PostfixExpression” and InfixExpression”.

• PostfixExpression class should encapsulate all the operations needed for such class. In particular, it should contain a

o Constructor with the string postfix expression representation as an argument

o A destructor o An “evaluate” member function that returns the calculation of such expression

o An “isEqual” method that accepts one PostfixExpression object to be compared to. This method will return true if the current PostfixExpression object evaluates to the same value as the argument passed.

o You can define any other class/structure/method you need.

• InfixExpression class should encapsulate all the operations needed for such class. In particular, it should contain a

o Constructor with the string Infix expression representation as an argument

o A destructor

o A “convertToPostfix” member function, which should produce a PostfixExpression object.

o An “evaluate” member function that returns the calculation of such expression, but only based on the postfix expression calculator.

o An “isEqual” method that accepts one InfixExpression object to be compared to. This method will return true if the current InfixExpression object evaluates to the same value as the argument passed.

o You can define any other class/structure/method you need.

The program should produce a shell once executed, and offer several operations. The example bellow shows the functionalities to implement, as well as an example execution:  Enter postfix expression 362*/,  Enter infix expression 9/2

Solution

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
struct exprNode
{
int data;
exprNode *next;
}*p = NULL, *top = NULL, *save = NULL, *ptr;
void pushNumber(int data)
{
p = new exprNode;
p->data = data;
p->next = NULL;
if (top == NULL)
{
top = p;
}
else
{
save = top;
top = p;
p->next = save;
}
}
char popNumber()
{
if (top == NULL)
{
cout<<\"stack underflow\";
}
else
{
ptr = top;
top = top->next;
return(ptr->data);
delete ptr;
}
}
int main()
{
char data[30];
int val1, val2;
cout<<\"enter expression\ \";
cin>>data;
for (int i = 0; i < strlen(data); i++)
{
if (data[i] >= 48 && data[i] <= 57)
pushNumber(data[i]-\'0\');
else if (data[i] >= 42 && data[i] <= 47)
{
val1=popNumber();
val2=popNumber();
switch(data[i])
{
case \'+\':
pushNumber(val1+val2);
break;
case \'-\':
pushNumber(val1-val2);
break;
case \'*\':
pushNumber(val1*val2);
break;
case \'/\':
pushNumber(val1/val2);
break;
}
}
}
cout<<\"Result is: \"<<popNumber()<<endl;
}

In C++ language In this assignment, a mathematical calculator of single digit operations should be implemented. The calculator operates only on postfix expressi
In C++ language In this assignment, a mathematical calculator of single digit operations should be implemented. The calculator operates only on postfix expressi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site