i need to write a program which deals with functions in c language by prompting
ID: 3631412 • Letter: I
Question
i need to write a program which deals with functions in c language by prompting the user for a mathematical expression. It will then print the result of that expression which will support the following operators–+, -, x, X, *, /, m, and M. m will stand for minimum. M will stand for maximum.i need to declare, define and call the following functions:
// Returns the result of value_1 + value_2
double add(double value_1, double value_2);
// Returns the result of value_1 - value_2
double subtract(double value_1, double value_2);
// Returns the result of value_1 * value_2
double multiply(double value_1, double value_2);
// Returns the result of numerator / denominator
double divide(double numerator, double denominator);
// Returns the minimum number between value_1 and value_2
double minimum(double value_1, double value_2);
// Returns the maximum number between value_1 and value_2
double maximum(double value_1, double value_2);
i need to Use a switch statement when deciding which expression to evaluate
All output will be displayed with 3 digits of precision
3 different operators will be supported for multiplication
No output will occur in the function definitions
When reading the operator in C with scanf you will need to read in a single character. In order to allow for the leading white space, your scanf call should look like
scanf(" %c", &binary_operator);
examples for outputs:
Please enter an expression (ex: 3.23 + 12): 4.3 + 2.7
4.300 + 2.700 = 7.000
Please enter an expression (ex: 3.23 + 12): 2 - 5.8
2.000 - 5.800 = -3.800
Please enter an expression (ex: 3.23 + 12): 19.1 x 2
19.100 x 2.000 = 38.200
Please enter an expression (ex: 3.23 + 12): 20 / 6
20.000 / 6.000 = 3.333
Please enter an expression (ex: 3.23 + 12): 5 h 9
Illegal expression.
Please enter an expression (ex: 3.23 + 12): 4.3 m 1.01
4.300 m 1.010 = 1.010
Please enter an expression (ex: 3.23 + 12): 4.3 M 1.01
4.300 M 1.010 = 4.300
Explanation / Answer
#include // Returns the result of value_1 + value_2 double add(double value_1, double value_2) { return value_1+value_2;} // Returns the result of value_1 - value_2 double subtract(double value_1, double value_2) { return value_1-value_2;} // Returns the result of value_1 * value_2 double multiply(double value_1, double value_2) { return value_1*value_2;} // Returns the result of numerator / denominator double divide(double numerator, double denominator) { return numerator/denominator;} // Returns the minimum number between value_1 and value_2 double minimum(double value_1, double value_2) { if(value_1value_2) return value_1; else return value_2;} int main(){ double value1,value2; char operater; printf("Please enter an expression (ex: 3.23 + 12)"); scanf("%f",&value1); scanf("%c",&operater); scanf("%f",&value2); printf("%c",operater); switch(operater) { case '+': printf("%4f+%f= %f",value1,value2,add(value1,value2)); break; case '-': printf("%f %c %f= %f",value1,operater,value2,subtract(value1,value2)); break; case '*': printf("%f %c %f= %f",value1,operater,value2,multiply(value1,value2)); break; case '/': printf("%f / %f= %f",value1,value2,divide(value1,value2)); break; case 'm': printf("%f m %f= %f",value1,value2,minimum(value1,value2)); break; case 'M': printf("%f M %f= %f",value1,value2,maximum(value1,value2)); break; default: printf("wrong input"); break; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.