-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClaculateViaSymbol.c
78 lines (65 loc) · 1.56 KB
/
ClaculateViaSymbol.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
#include<stdio.h>
#include<stdlib.h>
int add(int num1, int num2){
return num1+num2;
}
int sub(int num1,int num2){
return num1-num2;
}
int mul(int num1,int num2){
return num1*num2;
}
typedef int (*fptrOperation)(int, int);
fptrOperation evaluate(char opcode){
switch(opcode){
case '+':
return add;
case '-':
return sub;
case '*':
return mul;
// default:
// return NULL;
}
}
int compute(char opcode,int num1,int num2){
fptrOperation operation = evaluate(opcode);
return operation(num1,num2);
}
void main(){
int num1;
int num2;
int choice;
char opcode;
printf("\nPlease choose the operation you want to be performed");
printf("\n1.Add");
printf("\n2.Sub");
printf("\n3.Mul");
printf("\n4.Exit");
printf("\nYour Choice ");
scanf("%d",&choice);
if(choice<1 || choice>4){
printf("Please enter number as provided ");
getchar();
system("clear");
main();
}else if(choice==4){
exit(0);
}
else {
printf("\nPlease enter first number ");
scanf("%d",&num1);
printf("\nPlease enter second number ");
scanf("%d",&num2);
switch(choice){
case 1:
printf("\n%d + %d = %d\n",num1,num2,compute('+',num1,num2));
break;
case 2:
printf("\n%d - %d = %d\n",num1,num2,compute('-',num1,num2));
break;
case 3:
printf("\n%d * %d = %d\n",num1,num2,compute('*',num1,num2));
}
}
}