1. Write a C++ program that defines value-returning functions for the arithmetic
ID: 653623 • Letter: 1
Question
1. Write a C++ program that defines value-returning functions for the arithmetic integer binary operation plus, minus, multiply, and divide, You should alsp wrote a value-returning function for the unary operation negate. Use your function declarations to compare the folowing as a single expression:
-(((5-4*(8/2))+7)
Your main function should not declare a single variable, but it should print as output the result of computing this expression. Your implementation is correct if your output yields the value -11.
2. Use your function declarations in Problem 1 to define two additional integer functions for power and factorial. Use your new function declarations to compute as a single expression the following :
(5! + 4!)3
Your main function should not declare a single variable but it should print as output the result of computing this expression. Your implementation is correct if your output yields the value 2985984.
*Note that you should NOT declare and use any variables in your main function for both problems; all it does is to invoke all the functions defined in your program to compute the two expressions using a single expression for each.*
Explanation / Answer
1)
Program code:
// ExpressionEvaluator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
const int MAX = 50 ;
class infix
{
private :
char target[MAX], stack[MAX] ;
char *s, *t ;
int top ;
public :
infix( ) ;
void setexpr ( char *str ) ;
char* getTarget();
void push ( char c ) ;
char pop( ) ;
void convert( ) ;
int priority ( char c ) ;
void show( ) ;
} ;
infix :: infix( )
{
top = -1 ;
strcpy ( target, "" ) ;
strcpy ( stack, "" ) ;
t = target ;
s = "" ;
}
void infix :: setexpr ( char *str )
{
s = str ;
}
char* infix ::getTarget()
{
return target;
}
void infix :: push ( char c )
{
if ( top == MAX )
cout << " Stack is full " ;
else
{
top++ ;
stack[top] = c ;
}
}
char infix :: pop( )
{
if ( top == -1 )
{
cout << " Stack is empty " ;
return -1 ;
}
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
void infix :: convert( )
{
while ( *s )
{
if ( *s == ' ' || *s == ' ' )
{
s++ ;
continue ;
}
if ( isdigit ( *s ) || isalpha ( *s ) )
{
while ( isdigit ( *s ) || isalpha ( *s ) )
{
*t = *s ;
s++ ;
t++ ;
}
}
if ( *s == '(' )
{
push ( *s ) ;
s++ ;
}
char opr ;
if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' )
{
if ( top != -1 )
{
opr = pop( ) ;
while ( priority ( opr ) >= priority ( *s ) )
{
*t = opr ;
t++ ;
opr = pop( ) ;
}
push ( opr ) ;
push ( *s ) ;
}
else
push ( *s ) ;
s++ ;
}
if ( *s == ')' )
{
opr = pop( ) ;
while ( ( opr ) != '(' )
{
*t = opr ;
t++ ;
opr = pop( ) ;
}
s++ ;
}
}
while ( top != -1 )
{
char opr = pop( ) ;
*t = opr ;
t++ ;
}
*t = '' ;
}
int infix :: priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
void infix :: show( )
{
cout << target ;
}
class postfix
{
private :
int stack[MAX] ;
int top, nn ;
char *s ;
public :
postfix( ) ;
void setexpr ( char *str ) ;
void push ( int item ) ;
int pop( ) ;
void calculate( ) ;
void show(char ) ;
int getValue();
} ;
postfix :: postfix( )
{
top = -1 ;
}
void postfix :: setexpr ( char *str )
{
s = str ;
}
void postfix :: push ( int item )
{
if ( top == MAX - 1 )
cout << endl << "Stack is full" <<endl;
else
{
top++ ;
stack[top] = item ;
}
}
int postfix::getValue()
{
return nn;
}
int postfix :: pop( )
{
if ( top == -1 )
{
cout << endl << "Stack is empty" <<endl;
return NULL ;
}
int data = stack[top] ;
top-- ;
return data ;
}
void postfix :: calculate( )
{
int n1, n2, n3 ;
char ch, ch1;
while ( *s )
{
if ( *s == ' ' || *s == ' ' )
{
s++ ;
continue ;
}
if ( isdigit ( *s ) )
{
nn = *s - '0' ;
push ( nn ) ;
}
else
{
n1 = pop( ) ;
n2 = pop( ) ;
switch ( *s )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
n3 = n2 % n1 ;
break ;
case '$' :
n3 = (int)pow ((double) n2 , n1 ) ;
break ;
default :
cout << "Unknown operator"<<endl ;
system("pause") ;
}
push ( n3 ) ;
}
s++ ;
}
}
void postfix :: show(char c)
{
nn = pop ( ) ;
if(c=='-')
nn=-1*nn;
if(c==' ')
nn=nn;
cout << " Result is: " << nn<<endl ;
}
void main( )
{
char expr[MAX] ;
infix q ;
postfix p;
cout << " Enter an expression in infix form: " ;
cin.getline ( expr, MAX ) ;
q.setexpr ( expr ) ;
q.convert( ) ;
cout << " The postfix expression is: " ;
q.show( ) ;
char *str=q.getTarget();
int length=strlen(str);
string strng(str);
char st[1024];
strng=strng.substr(0,strng.length()-2);
strcpy(st, strng.c_str());
p.setexpr(st);
p.calculate();
if(str[strlen(str)-1]=='-')
p.show('-');
else
p.show(' ');
system("pause");
}
Sample Output:
Enter an expression in infix form: -(((5-4*(8/2))+7)
The postfix expression is: 5482/*-7+(-
Result is: 4
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.