1. 程式人生 > >函式指標陣列,函式指標對程式碼的優化:計算器

函式指標陣列,函式指標對程式碼的優化:計算器

  • 利用函式指標,函式指標陣列來優化計算器:
   void menu ( )
    {
       printf ("************************\n");
       printf ("******1.Add    2.Sub********\n");
       printf ("******3.Mul    4.Div********\n");
       printf ("******     0.  exit      ******\n");
     }
    
    Add (int x,int y)
    {
      return x+y;
    }
    Sub(int x,int y)
    {
      return x-y;
    }
    Mul (int x,int y)
    {
      return x*y;
    }
    Div (int x,int y)
    {
      return x/y;
    }
      
      int main ( )
      {
        int input =0;
        int x=0;
        int y=0;
        int ret =0;
          do 
          {
            menu ( );
            printf ("請選擇:>\n");
            scanf ("%d",input);
            switch (input )
            {
                       case 1:
               printf ("請輸入兩個數:>\n);
               scanf ("%d%d",&x,&y);
               ret=Add( x,y );
               printf ("%d\n",ret);
                       case 2:
               printf ("請輸入兩個數:>\n);
               scanf ("%d%d",&x,&y);
               ret=Sub( x,y );
               printf ("%d\n",ret);
                       case 3:
               printf ("請輸入兩個數:>\n);
               scanf ("%d%d",&x,&y);
               ret=Mul( x,y );
               printf ("%d\n",ret);
                       case 4:
               printf ("請輸入兩個數:>\n);
               scanf ("%d%d",&x,&y);
               ret=Div( x,y );
               printf ("%d\n",ret);
                       case 0:
               printf ("退出\n");
               break;
                        default :
               break;
           }
       }while (input );
       return 0;
     }

對函式選擇的優化:利用函式指標陣列
int main ( )
{
  int input =0
  int x=0;
  int y=0;
  int ret=0;
  int (*pfun[5])(int ,int )={Exit,Add,Sub,Mul,Div};
  do 
     {
      menu ( );
      printf ("請選擇:>\n");
      scanf ("%d",&input);
      if (input>=1&&input<=4)
        {
          printf ("請輸入兩個數:>\n");
          scanf ("%d%d",&x,&y0;
          ret =(pfun[input])(x,y);
          printf("%d\n",ret);
       }
    }while (input );
    return 0;
 }
      
> 對main 函式的優化:
void Calc (int (*pfun)(int ,int))
{
  int x=0;
  int y=0;
  int ret =0;
  printf("請輸入兩個數:>\n);
  scanf ("%d%d",&x,&y);
  ret =pfun(x,y);
  printf ("%d\n",ret);
}
> 
> int main ( )
> {
>   int input =0;
>   int x=0;
>   int y=0;
>   int ret=0;
>   do 
>    {
>       menu ( );
          printf ("請選擇:>\n");
          scanf ("%d",input);
          switch (input )
          {
            case 1:
            Calc (Add);
            break;
            case 2:
            Calc (Sub);
            break;
            case 3:
            Calc (Mul);
            break;
            case 4:
            Calc (Div);
            break;
            case 0:
            printf ("退出");
            break;
            default :
            break;
      }
      }while (input);
      return 0;
   }

#include <stdio.h>
int main ( )
{
  char arr1[]="abcdefg";
    char arr2[]="abcdefg";
      char *p1="abcdefg";
        char *p2="abcdefg";
     printf ("%p\n",arr1);
      printf ("%p\n",arr2);
       printf ("%p\n",p1);
        printf ("%p\n",p2);
}

結果為:
arr1[]和arr2[]的地址不相同,原因是“abcdefg”作為字串,存在兩個陣列的兩個不同的空間中,可以被改動。
p1和p2的地址相同,原因是“abcdefg”存放在同一塊地址中,不能被改動。