In: Computer Science
Consider simple infix expressions that consist of single-digit operands; the operators +, -, *, and /; and the parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. The class should have the following members:
Private members:
a string to store an infix expression for evaluation.
a private member function that checks if an infix expression is well formed. (A well formed expression consists of single-digit operands; the operators +, -, *, and /; and the parentheses.)
a private member function that checks for balanced brackets of an expression using the algorithm studied.
a private member function that converts an infix expression to a postfix expression using the algorithm studied.
a private member function that determines the precedence of an operator
a private member function that evaluates a postfix expression using the algorithm studied.
Public members:
a default constructor.
a function that sets the data member by an infix expression. The function must first check if the infix expression is well formed and balanced with brackets by calling private member function 2 and 3 before passing it to the data member. The function returns true if the operation is successfully performed. Otherwise it returns false to indicate the parameter expression is not valid.
a function that evaluates the expression stored in the data member. This function should not have any parameters. The function should first convert the infix expression to its postfix form and then evaluate the resulting postfix expression.
Other requirements
#The program to be written in C++. After submit the answer, can you double check and test run the code then show it for me? Please read the requirements carefully, and I want the answer have to be written in C++. Please!!
Use the link-based Stack with exceptions.
Write a main function to test your calculators. Your program should allow the user to evaluate additional expressions until the user wants to end the program.
You should not assume that the expression is well-formed. The user is asked to reenter an expression if the expression entered is not well-formed.
You should not assume that the parentheses in the expression are balanced. The user is asked to reenter an expression if the expression entered is not balanced. Use the algorithm discussed to solve this problem.
/*the following is the code.This code is only for single digit calculator .Plase refer any other sourse to get the more digit.
//c++ program to infix calculator
#include <iostream>
#include<stdbool.h>
#include<string.h>
using namespace std;
//Stack initailisation
typedef struct{
int top;
int arr[50];
}Stack;
Stack s;
//stack push and pop function
void push(char item)
{
if(s.top >= 49)
{
return;
}
/*if(s.arr[s.top]>=0 && s.arr[s.top]<=9)
{
s.arr[s.top]*=10;
s.arr[s.top]+=item;
}*/
else
{
s.top = s.top+1;
s.arr[s.top] = item;
}
}
int pop()
{
int item ;
if(s.top <0)
{
exit(1);
}
else
{
item = s.arr[s.top];
s.top = s.top-1;
return(item);
}
}
// declare of class
class calculator{
private:
char infix[50]; //expression string
bool valid(char exp[]) //cheak expression well formed
{
for(int i=0;i<strlen(exp);i++)
{
char ch=exp[i];
if((is_operator(ch))!=1 && ch!='(' && ch!=')'
&& isdigit(ch)!=1)
return false;
}
return true;
}
bool valid_bracket(char exp[]) //cheak weather brackets are
balanced
{
int flag=0;
for(int i=0;i<strlen(exp);i++)
{
if(exp[i]=='(')
flag++;
if(exp[i]==')')
flag--;
}
if(flag==0)
return true;
else
return false;
}
int precedence(char symbol) //precedence function
{
if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+'
|| symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
void infixTopostfix(char *postfix) //function to convert infix to
postfix
{
int i, j;
char item;
char x;
push('(');
strcat(infix,")");
i=0;
j=0;
item=infix[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
*(postfix+j) = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while((is_operator(x) == 1) && (precedence(x)>=
precedence(item)))
{
*(postfix+j) = x;
j++;
x =pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x =pop();
while(x != '(')
{
*(postfix+j) = x;
j++;
x =pop();
}
}
else
{
exit(1);
}
i++;
item = infix[i];
}
if(s.top>0)
{
exit(1);
}
*(postfix+j)= '\0';
}
int evaluate() //evaluatig postfix expression
{
char postfix[50];
infixTopostfix(postfix);
s.top=-1;
for(int i=0;i<strlen(postfix);i++)
{
char ch=postfix[i];
if(ch>=48 && ch<=57)
{
int x=ch-'0';
push(x);
}
if(ch=='*' || ch=='/' || ch=='+' || ch=='-')
{
int a,b,value;
a=pop();
b=pop();
switch (ch) {
case '*':
value=b*a;
break;
case '/':
value=b/a;
break;
case '+':
value=b+a;
break;
case '-':
value=b-a;
break;
}
push(value);
}
}
return pop();
}
public:
calculator() //default constructor
{
return;
}
int calc(char exp[]) //function that set datamember and
evaluates
{
char postfix[50];
if(valid(exp)==false){
printf("The entered expression is not well formed.\n");
return 0;
}
if(valid_bracket(exp)==false)
{
printf("The enterned expression's brackets are not
balanced.\n");
return 0;
}
strcpy(infix,exp);
int result=evaluate();
return result;
}
};
int main()
{
int choice;
s.top=-1;
char exp[50];
calculator cal;
a:printf("Choices\n1:Enter '1' to Evaluate a expression\n2:Enter
any other number Quit\n" );
printf("Enter your choice:");
scanf("%d",&choice);
if(choice==1)
{
int result;
printf("Enter the expression\n");
scanf("%s",exp);
result=cal.calc(exp);
if(result==0)
{
goto a;
}
printf("*************%s=%d*****************\n\n\n\n",exp,result);
goto a;
}
else
{
return 0;
}
}