File tree 4 files changed +164
-0
lines changed
4 files changed +164
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include "calc.h"
2
+
3
+ /**
4
+ * get_op_func - selects the correct function to perform
5
+ * the operation asked by the user.
6
+ *
7
+ * @s: operator.
8
+ *
9
+ * Return: pointer to function.
10
+ */
11
+ int (* get_op_func (char * s ))(int , int )
12
+ {
13
+ op_t ops [] =
14
+ {
15
+ {"+" , op_add },
16
+ {"-" , op_sub },
17
+ {"*" , op_mul },
18
+ {"/" , op_div },
19
+ {"%" , op_mod },
20
+ {NULL , NULL }
21
+ };
22
+ int i ;
23
+
24
+ i = 0 ;
25
+ while (ops [i ].op != NULL )
26
+ {
27
+ if (* s == * ops [i ].op )
28
+ return (ops [i ].f );
29
+ i ++ ;
30
+ }
31
+ return (NULL );
32
+ }
Original file line number Diff line number Diff line change
1
+ #include "calc.h"
2
+
3
+ /**
4
+ * main - main for calc program.
5
+ *
6
+ * @argc: number of arguments.
7
+ * @argv: arguments values.
8
+ *
9
+ * Return: Always success (0), (1) otherwise.
10
+ */
11
+ int main (int argc , char * * argv )
12
+ {
13
+ int num1 , num2 , result ;
14
+ char * operator ;
15
+
16
+ if (argc != 4 )
17
+ {
18
+ printf ("Error\n" );
19
+ exit (98 );
20
+ }
21
+
22
+ num1 = atoi (argv [1 ]);
23
+ num2 = atoi (argv [3 ]);
24
+ operator = argv [2 ];
25
+
26
+ if (get_op_func (operator ) == NULL )
27
+ {
28
+ printf ("Error\n" );
29
+ exit (99 );
30
+ }
31
+
32
+ if ((* operator == '/' || * operator == '%' ) && num2 == 0 )
33
+ {
34
+ printf ("Error\n" );
35
+ exit (100 );
36
+ }
37
+ result = (* get_op_func (operator ))(num1 , num2 );
38
+ printf ("%d\n" , result );
39
+ return (0 );
40
+ }
Original file line number Diff line number Diff line change
1
+ #include "calc.h"
2
+
3
+ /**
4
+ * op_add - add two numbers.
5
+ *
6
+ * @a: num1
7
+ * @b: num2
8
+ *
9
+ * Return: num1 + num2
10
+ */
11
+ int op_add (int a , int b )
12
+ {
13
+ return (a + b );
14
+ }
15
+
16
+ /**
17
+ * op_sub - subtract two numbers.
18
+ *
19
+ * @a: num1
20
+ * @b: num2
21
+ *
22
+ * Return: num1 - num2
23
+ */
24
+ int op_sub (int a , int b )
25
+ {
26
+ return (a - b );
27
+ }
28
+
29
+ /**
30
+ * op_mul - multiply two numbers.
31
+ *
32
+ * @a: num1
33
+ * @b: num2
34
+ *
35
+ * Return: num1 * num2
36
+ */
37
+ int op_mul (int a , int b )
38
+ {
39
+ return (a * b );
40
+ }
41
+
42
+ /**
43
+ * op_div - divide two numbers.
44
+ *
45
+ * @a: num1
46
+ * @b: num2
47
+ *
48
+ * Return: num1 / num2
49
+ */
50
+ int op_div (int a , int b )
51
+ {
52
+ return (a / b );
53
+ }
54
+
55
+ /**
56
+ * op_mod - modules a number.
57
+ *
58
+ * @a: num1
59
+ * @b: num2
60
+ *
61
+ * Return: num1 % num2
62
+ */
63
+ int op_mod (int a , int b )
64
+ {
65
+ return (a % b );
66
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef CALC_H
2
+ #define CALC_H
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+
6
+ /**
7
+ * struct op - Struct op
8
+ *
9
+ * @op: The operator
10
+ * @f: The function associated
11
+ */
12
+ typedef struct op
13
+ {
14
+ char * op ;
15
+ int (* f )(int a , int b );
16
+ } op_t ;
17
+
18
+ int op_add (int , int );
19
+ int op_sub (int , int );
20
+ int op_mul (int , int );
21
+ int op_div (int , int );
22
+ int op_mod (int , int );
23
+
24
+ int (* get_op_func (char * s ))(int , int );
25
+
26
+ #endif /* CALC_H */
You can’t perform that action at this time.
0 commit comments