diff --git a/calculator/calculator.c b/calculator/calculator.c index 945a0e3..82e239b 100644 --- a/calculator/calculator.c +++ b/calculator/calculator.c @@ -1,4 +1,5 @@ #include +#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: diff --git a/calculator/utility.c b/calculator/utility.c new file mode 100644 index 0000000..c7e92ba --- /dev/null +++ b/calculator/utility.c @@ -0,0 +1,67 @@ +#include +#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); +} diff --git a/calculator/utility.h b/calculator/utility.h index e69de29..e407d8e 100644 --- a/calculator/utility.h +++ b/calculator/utility.h @@ -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);