-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.c
74 lines (56 loc) · 1.76 KB
/
calculator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <f2fs_fs.h>
# define PLUS '+'
# define MINUS '-'
# define MUL '*'
# define DIV '/'
double add(double fristAddend, double secondAddend);
double sub(double minuend, double subtrahend);
double multi(double firstFactor, double secondFactor);
double divide(double dividend, double divisor);
bool isValidArgs(int argc, const char *const *argv);
void doCalculation(const char *const *argv, double firstArg, double secondArg);
int main(int argc, char const *argv[]) {
if (isValidArgs(argc, argv)) {
double firstArg = atof(argv[1]);
double secondArg = atof(argv[3]);
doCalculation(argv, firstArg, secondArg);
}
return 0;
}
bool isValidArgs(int argc, const char *const *argv) {
return argc > 3 && isdigit(*argv[1]) && isdigit(*argv[3]);
}
void doCalculation(const char *const *argv, double firstArg, double secondArg) {
char operator = *argv[2];
switch (operator) {
case PLUS:
printf("%.2f\n", add(firstArg, secondArg));
break;
case MINUS:
printf("%.2f\n", sub(firstArg, secondArg));
break;
case MUL:
printf("%.2f\n", multi(firstArg, secondArg));
break;
case DIV:
printf("%.2f\n", divide(firstArg, secondArg));
break;
default:
printf("Invalid operation argument!:\n");
}
}
double add(double fristAddend, double secondAddend) {
return (fristAddend + secondAddend);
}
double sub(double minuend, double subtrahend) {
return minuend - subtrahend;
}
double multi(double firstFactor, double secondFactor) {
return firstFactor * secondFactor;
}
double divide(double dividend, double divisor) {
return dividend / divisor;
}