-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.c
91 lines (84 loc) · 1.5 KB
/
Calc.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
91
/*
* Simple C Terminal Calculator
*
* Author: mboy1011
* */
#include <stdio.h>
#include <stdlib.h>
#include "calc.h"
int main(){
char lp;
do{
system("cls");
int c,res;
printf("+==============+\n");
printf("| Calculator |\n");
printf("| 1. Add |\n");
printf("| 2. Sub |\n");
printf("| 3. Mul |\n");
printf("| 4. Div |\n");
printf("| 5. Exit |\n");
printf("+==============+\n");
printf("Choose: ");
scanf("%d",&c);
switch(c){
case 1:
res = input(1);
printf("Result: %d\n",res);
break;
case 2:
res = input(2);
printf("Result: %d\n",res);
break;
case 3:
res = input(3);
printf("Result: %d\n",res);
break;
case 4:
res = input(4);
printf("Result: %d\n",res);
break;
case 5:
exit(0);
break;
default:
printf("Invalid Choice!\n");
break;
}
printf("Do you want to try again? [Y/n] ");
scanf(" %c",&lp); // PUT whitespace before if you are using Characters unlike all the others.
printf("\n");
}while(lp == 'Y' || lp == 'y');
}
int input(int c){
int res,a,b;
printf("1st Digit: ");
scanf("%d",&a);
printf("2nd Digit: ");
scanf("%d",&b);
if(c==1){
res = add(a,b);
return res;
}else if(c==2){
res = sub(a,b);
return res;
}else if(c==3){
res = mul(a,b);
return res;
}else if(c==4){
res = qot(a,b);
return res;
}
}
int mul(int a, int b){
return a*b;
}
int qot(int a, int b){
return a/b;
}
int add(int a, int b){
return a+b;
}
int sub(int a, int b){
return a-b;
}