-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.c
46 lines (45 loc) · 1.03 KB
/
1.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
/*INPUT: Takes two floats and a character as input (operands and operator)
OUTPUT: Prints the result of the expression
REMARKS: implemented through multiple functions using function pointers
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int add(int a, int b)// adds a and b
{
return a+b;
}
int sub(int a, int b)// subtracts b from a
{
return a-b;
}
int multiply(int a, int b)// returns their product
{
return a*b;
}
float divide(int a, int b)// returns the quotient assuming b is not 0
{
return (float)a/(float)b;//returns float value.
}
int main(int argc, char **argv)
{
float a= atoi(argv[1]);
float b=atoi(argv[2]);
char operator=argv[3][0];
char oplist[]={'+','-','*'};// array of operators
int (*funcPoint[])(int,int)={add,sub,multiply};// function array
if (operator=='/')
{
printf("%f",divide(a,b));
return 0;
}
for(int i=0;i<4;i++)
{
if(oplist[i]==operator)
{
printf("%d",funcPoint[i](a,b));// chooses appropriate function form function array matching operator character
break;
}
}
return 0;
}