01: // example of selection of a function without func pointers: a switch-case or multiple if() are required
02: #include<stdio.h>
03: #include<stdlib.h>
04: 
05: float sum(float a, float b)
06: {
07:   return a + b;
08: }
09: 
10: float sub(float a, float b)
11: {
12:   return a - b;
13: }
14: 
15: float mult(float a, float b)
16: {
17:   return a * b;
18: }
19: 
20: float divi(float a, float b)
21: {
22:   return a / b;
23: }
24: 
25: 
26: 
27: 
28: int main(int argc, char **argv){
29: 
30:   float op1, op2;
31:   printf("Enter two numbers: ");
32:   scanf("%f%f", &op1, &op2);
33: 
34:   int oper;
35:   printf("Enter your choiche (0 = sum, 1 = sub, 2 = mult, 3 = div): ");
36:   scanf("%d", &oper);
37: 
38:   float res;
39:   switch(oper)
40:   {
41:     case 0:
42:       res = sum(op1, op2);
43:       break;
44:     case 1:
45:       res = sub(op1, op2);
46:       break;
47:     case 2:
48:       res = mult(op1, op2);
49:       break;
50:     case 3:
51:       res = divi(op1, op2);
52:       break;
53:   }
54: 
55:   printf("The result is %g\n", res);
56: 
57: 
58: 
59:   return 0;
60: }
61: 
62: 


Se avete commenti o osservaƶioni su questa pagina
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it