Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- const int MODULO = 42;
- const int ARRLEN = 6;
- float mul (int x, int y);
- float add (int x, int y);
- float sub (int x, int y);
- float dvs (int x, int y);
- int main(void)
- {
- int x,y = 0;
- char op = 0;
- char in = 0;
- /* array di puntatori a funzione */
- float (*arrf [ARRLEN]) (int x, int y) =
- {
- mul, add, NULL, sub, NULL, dvs
- };
- printf ("%s:\n", "Inserire una operazione tra due interi");
- if ( scanf (" %d%c$d", &x, &op, &y) != 3 )
- {
- printf ("Errore in lettura!\n");
- return (2);
- }
- if (op >= MODULO && op <= MODULO + ARRLEN)
- {
- printf ("Risultato: %f \n", arrf[(int)op % MODULO] (x,y));
- }
- else
- {
- printf ("Errore: simbolo errato!\n");
- return (1);
- }
- return 0;
- }
- float mul (int x, int y)
- {
- return x*y;
- }
- float add (int x, int y)
- {
- return x+y;
- }
- float sub (int x, int y)
- {
- return x-y;
- }
- float dvs (int x, int y)
- {
- if (y != 0)
- {
- return x/y;
- }
- else
- {
- printf ("Errore: divisione per 0!\n");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement