Implement Add, Subtract, Multiply and Divide Functionality

main
sgoudham 3 years ago
parent de8ce1cd43
commit dccfd530b5

@ -1,4 +1,5 @@
#include <stdio.h>
#include "utility.h"
int main() {
char operator;
@ -13,26 +14,23 @@ int main() {
break;
}
printf("Enter First Operand: ");
scanf("%lf", &firstOperand);
printf("Enter Second Operand: ");
scanf("%lf", &secondOperand);
getOperands(&firstOperand, &secondOperand);
switch (operator) {
case '+':
add(firstOperand, secondOperand);
break;
case '-':
subtract(firstOperand, secondOperand);
break;
case '*':
multiply(firstOperand, secondOperand);
break;
case '/':
divide(firstOperand, secondOperand);
break;
default:

@ -0,0 +1,67 @@
#include <stdio.h>
#include "utility.h"
const char operators[] = {'+', '-', '*', '/'};
char enumToString(MathOperator mathOperator) {
return operators[mathOperator];
}
void println(double firstOperand, MathOperator mathOperator, double secondOperand, double result) {
printf("%0.2lf %c %0.2lf = %0.2lf\n\n", firstOperand, enumToString(mathOperator), secondOperand, result);
}
double doOperation(double firstOperand, double secondOperand, MathOperator mathOperator) {
double result = 0.0;
switch (mathOperator) {
case ADD:
result = firstOperand + secondOperand;
break;
case SUBTRACT:
result = firstOperand - secondOperand;
break;
case MULTIPLY:
result = firstOperand * secondOperand;
break;
case DIVIDE:
result = firstOperand / secondOperand;
break;
default:
printf("Sorry don't recognise the given operator!\n");
break;
}
return result;
}
void getOperands(double *firstOperand, double *secondOperand) {
printf("Enter First Operand: ");
scanf("%lf", firstOperand);
printf("Enter Second Operand: ");
scanf("%lf", secondOperand);
}
void add(double firstOperand, double secondOperand) {
double result = doOperation(firstOperand, secondOperand, ADD);
println(firstOperand, ADD, secondOperand, result);
}
void subtract(double firstOperand, double secondOperand) {
double result = doOperation(firstOperand, secondOperand, SUBTRACT);
println(firstOperand, SUBTRACT, secondOperand, result);
}
void multiply(double firstOperand, double secondOperand) {
double result = doOperation(firstOperand, secondOperand, MULTIPLY);
println(firstOperand, MULTIPLY, secondOperand, result);
}
void divide(double firstOperand, double secondOperand) {
double result = doOperation(firstOperand, secondOperand, DIVIDE);
println(firstOperand, DIVIDE, secondOperand, result);
}

@ -0,0 +1,14 @@
typedef enum {
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE
} MathOperator;
extern const char operators[];
void add(double firstOperand, double secondOperand);
void subtract(double firstOperand, double secondOperand);
void multiply(double firstOperand, double secondOperand);
void divide(double firstOperand, double secondOperand);
void getOperands(double *firstOperand, double *secondOperand);
Loading…
Cancel
Save