-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirthmeticCalculator.c
90 lines (72 loc) · 2.45 KB
/
AirthmeticCalculator.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define LIMIT 1024
int childFunction (char *inputExpression) {
int N1, N2;
float DivN;
char operator;
char result[LIMIT];
write(1, "I am a child working for my parent\n", sizeof("I am a child working for my parent\n"));
sscanf(inputExpression, "%d %c %d", &N1, &operator, &N2);
if ((sscanf(inputExpression, "%d %c %d", &N1, &operator, &N2)) != 3) {
exit(50);
}
if (operator == '/' && N2 == 0) {
exit(100);
}
switch (operator) {
case '+':
sprintf(result, "%d %c %d = %d\n\n", N1, operator, N2, N1 + N2);
break;
case '/':
DivN = N1 / N2;
sprintf(result, "\n%d %c %d = %.3f\n\n", N1, operator, N2, DivN);
break;
case '*':
sprintf(result, "\n%d %c %d = %d\n\n", N1, operator, N2, N1 * N2);
break;
case '-':
sprintf(result, "%d %c %d = %d\n\n", N1, operator, N2, N1 - N2);
break;
default:
exit(200);
break;
}
printf("%s", result);
}
int main(int argc, char *argv[]) {
char inputExpression[LIMIT];
int pID, childStatus;
write(1, "This program makes simple arithmetics.\n", sizeof("This program makes simple arithmetics.\n"));
while (1) {
write(1, "Enter an arithmetic statement, e.g., 34 + 132 > ", sizeof("Enter an arithmetic statement, e.g., 34 + 132 > "));
scanf("%1024s", inputExpression);
//Exit Condition
if ((strcmp ("exit", inputExpression)) == 0) {
exit(0);
}
pID = fork();
if (pID == -1) {
write (1, "Forking not possible.\n", sizeof("Forking not possible\n"));
exit(0);
}
else if (pID == 0) {
childFunction(inputExpression);
}
else {
write (1, "\n\nCreated a child to make your operation, waiting...\n\n", sizeof("\n\nCreated a child to make your operation, waiting...\n\n"));
wait(&childStatus);
if (WEXITSTATUS(childStatus) == 50)
write(1, "Wrong Statement.\n", sizeof("Wrong Statement.\n"));
else if (WEXITSTATUS(childStatus) == 100)
write(1, "Division by Zero.\n", sizeof("Division by Zero.\n"));
else if (WEXITSTATUS(childStatus) == 200)
write(1, "Wrong Operator.\n", sizeof("Wrong Operator.\n"));
}
}
exit(0);
}