Advertisement
Guest User

provaFprt.c

a guest
Oct 20th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. const int MODULO = 42;
  5. const int ARRLEN = 6;
  6.  
  7. float mul (int x, int y);
  8. float add (int x, int y);
  9. float sub (int x, int y);
  10. float dvs (int x, int y);
  11.  
  12. int main(void)
  13. {
  14.     int x,y = 0;
  15.     char op = 0;
  16.     char in = 0;
  17.    
  18.     /* array di puntatori a funzione */
  19.     float (*arrf [ARRLEN]) (int x, int y) =
  20.     {
  21.           mul, add, NULL, sub, NULL, dvs
  22.     };
  23.    
  24.     printf ("%s:\n", "Inserire una operazione tra due interi");
  25.    
  26.     if ( scanf (" %d%c$d", &x, &op, &y) != 3 )
  27.     {
  28.          printf ("Errore in lettura!\n");
  29.          return (2);
  30.     }
  31.      
  32.     if (op >= MODULO && op <= MODULO + ARRLEN)
  33.     {
  34.         printf ("Risultato: %f \n", arrf[(int)op % MODULO] (x,y));
  35.     }
  36.     else
  37.     {
  38.         printf ("Errore: simbolo errato!\n");
  39.         return (1);
  40.     }
  41.    
  42.     return 0;
  43. }
  44.  
  45. float mul (int x, int y)
  46. {
  47.     return x*y;
  48. }
  49.  
  50. float add (int x, int y)
  51. {
  52.     return x+y;
  53. }
  54.  
  55. float sub (int x, int y)
  56. {
  57.     return x-y;
  58. }
  59.  
  60. float dvs (int x, int y)
  61. {
  62.     if (y != 0)
  63.     {
  64.         return x/y;
  65.     }
  66.     else
  67.     {
  68.         printf ("Errore: divisione per 0!\n");
  69.         exit(1);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement