函数指针数组,函数指针对代码的优化:计算器

  • 利用函数指针,函数指针数组来优化计算器:
   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 
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”存放在同一块地址中,不能被改动。

你可能感兴趣的:(函数指针数组,函数指针对代码的优化:计算器)