Question: This code (in C) only outputs 0.00000, change the code without changing the general structure of the code. #include float s_exp(float sub_exp, char op); char
This code (in C) only outputs 0.00000, change the code without changing the general structure of the code.
#include
float s_exp(float sub_exp, char op);
char get_op();
float get_num();
float m_exp(float sub_exp, char op);
int main(void)
{
char cont = 'Y';
while (cont == 'Y' || cont == 'y')
{
printf("Enter a simple arithmetic expression: ");
float result = s_exp(get_num(),get_op());
printf(" The result is %f ", result);
printf("Do you want to continue? Y/N ");
scanf("%c",&cont);
//Enter Y or y to continue, otherwise N or n to quit
}
}
//returns the value of the expression
float s_exp(float sub_exp, char op)
{
if (op == ' ')
return sub_exp;
else if (op == '+')
return s_exp(sub_exp + get_num(), get_op());
else if (op == '-')
return s_exp(sub_exp - get_num(), get_op());
else if (op == '*')
return s_exp(sub_exp * get_num(), get_op());
else if (op == '/')
return s_exp(sub_exp / get_num(), get_op());
else
{
printf("Invalid operator. ");
return 0;
}
}
float m_exp(float sub_exp, char op) {
if (op == ' ')
return sub_exp;
else if (op == '*')
return s_exp(sub_exp * get_num(), get_op());
else if (op == '/')
return s_exp(sub_exp / get_num(), get_op());
else
{
printf("Invalid operator. ");
return 0;
}
}
// return the next operator of the expression.
char get_op()
{
char op = ' ';
while (op == ' ')
scanf("%c", &op);
return op;
}
//return the next numeric value of the expression.
float get_num()
{
float num;
scanf("%f", &num);
return num;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
