-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalc.c
52 lines (44 loc) · 763 Bytes
/
scalc.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
#include <stdio.h>
#include <stdlib.h>
/*----------------------------------------------------------------------------*/
int
main(int argc, char *argv[])
{
int a;
int b;
int result;
char op;
if (argc != 4)
{
printf("Invalid parameters. Usage:\n");
printf("scalc <operand> <operator> <operand>\n");
printf("operand should be integers, and operator one of +, -, * or /.\n");
exit(1);
}
a = atoi(argv[1]);
op = argv[2][0];
b = atoi(argv[3]);
switch(op)
{
case '+':
result = a+b;
break;
case '-':
result = a-b;
break;
case '*':
result = a*b;
break;
case '/':
if (b != 0)
{
result = a/b;
break;
}
default:
printf("Invalid operation!\n");
exit(2);
}
printf("%d\n", result);
return 0;
}