-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.c
More file actions
46 lines (38 loc) · 764 Bytes
/
calc.c
File metadata and controls
46 lines (38 loc) · 764 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
if(argc != 4)
{
printf("arguments fail, example:\n$ ./calc.out 10 * 10\n");
return 1;
}
int result = -1;
int left = atoi(argv[1]);
int right = atoi(argv[3]);
char* operator = argv[2];
if(strcmp(operator, "x") == 0)
{
result = left * right;
}
if(strcmp(operator, ":") == 0)
{
result = left / right;
}
if(strcmp(operator, "+") == 0)
{
result = left + right;
}
if(strcmp(operator, "-") == 0)
{
result = left - right;
}
if(result == -1)
{
printf("arguments fail, invalid operator '%s' , expected: [ x , : , +, - ]\n", operator);
return 1;
}
printf("result: %d %s %d = %d\n", left, operator, right, result);
return 0;
}