daily pastebin goal
52%
SHARE
TWEET

mee4

a guest Feb 16th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4.  int add( int a, int b ) {
  5.      return a + b;
  6.  }
  7.  
  8.  int sub( int a, int b ) {
  9.      return a - b;
  10.  }
  11.  
  12.  int mul( int a, int b ) {
  13.      return a * b;
  14.  }
  15.  
  16.  int or( int a, int b ) {
  17.      return a | b;
  18.  }
  19.  
  20.  int and( int a, int b ) {
  21.      return a & b;
  22.  }
  23.  
  24.  int xor( int a, int b ) {
  25.      return a ^ b;
  26.  }
  27.  
  28. typedef int ( *op_ptr )( int, int );
  29.  
  30. typedef struct {
  31.  char name[11];
  32.  op_ptr func;
  33. } op;
  34.  
  35.  
  36. int main()
  37. {
  38.     int a, b;
  39.     uint8_t sel;
  40.    
  41.     op ops[] = {
  42.      { name: "Add", func: &add },
  43.      { name: "Sub", func: &sub },
  44.      { name: "Mul", func: &mul },
  45.      { name: "OR" , func: &or  },
  46.      { name: "AND", func: &and },
  47.      { name: "XOR", func: &xor }
  48.     };
  49.    
  50.     for ( uint8_t i = 0; i < 6; i++ ) {
  51.         printf( "%hhu. %s\n", i, ops[i].name );
  52.     }
  53.     printf( "\nSelection: " );
  54.     scanf( "%hhu", &sel );
  55.     getchar();
  56.     printf( "a=" );
  57.     scanf( "%d", &a );
  58.     getchar();
  59.     printf( "b=" );
  60.     scanf( "%d", &b );
  61.     getchar();
  62.      
  63.     if ( sel < 6 )
  64.         printf( "result: %d\n", ops[sel].func( a, b ) );
  65.     else
  66.         printf( "No Action" );
  67.        
  68.     return 0;
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top