English 中文(简体)
• 如何让一系列点子打电话?
原标题:how to make a array of pointer to call func pointer?
  • 时间:2011-11-09 10:06:50
  •  标签:
  • c

i) 点码

#include <stdio.h>

int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);

int (*p[4]) (int x, int y);

int main(void)
{
  int result;
  int i, j, op;

  p[0] = sum; /* address of sum() */
  p[1] = subtract; /* address of subtract() */
  p[2] = mul; /* address of mul() */
  p[3] = div; /* address of div() */

  printf("Enter two numbers: ");
  scanf("%d %d", &i, &j);

  printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide
");
  do {
    printf("Enter number of operation: ");
    scanf("%d", &op);
  } while(op<0 || op>3);

  result = (*p[op]) (i, j);
  printf("%d", result);

  return 0;
}

int sum(int a, int b)
{
  return a + b;
}

int subtract(int a, int b)
{
  return a - b;
}

int mul(int a, int b)
{
  return a * b;
}

int div(int a, int b)
{
  if(b) 
      return a / b;
  else 
      return 0;
}

职能范围代码:

#include <stdio.h>

int sum(int, int);
int product(int, int);
int subtract(int, int);

int main()
{
   int i = 0;
   int a = 10;
   int b = 5;
   int result = 0;
   int (*pfun[3])(int, int);

   pfun[0] = sum;
   pfun[1] = product;
   pfun[2] = subtract;

   for( i = 0 ; i < 3 ; i++)
   {
     result = pfun[i](a, b);
     printf("
result = %d", result);
   }

   result = pfun[1](pfun[0](a, b), pfun[2](a, b));
   printf("

The product of the sum and the subtract = %d
",result);
}

int sum(int x, int y)
{
   return x + y;
}

int product(int x, int y)
{
   return x * y;
}

int subtract(int x, int y)
{
   return x - y;
}

现在如何将这一方案结合起来。 这样一阵指指点器和点码的点数可能会有不同的增量? 任何建议。

问题回答

你不仅需要向职能点储存各种论点(例如,你可以使用<条码>un<>/代码>),而且还需要确保你以正确的理由来说明这些职能,而且鉴于你的设计,这是一个比方的骗局。

I suggest to use a stack instead. All your functions would only take the stack as an argument:

void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);

可以这样说:

typedef void callback_t(stack_t *);

callback_t *p[] =
{
    sum,
    subtract,
    product,
    /* ... */
};

Then for instance sum would be implemented as such:

void sum(stack_t *stack)
{
    if (depth(stack) < 2)
        perror("Not enough arguments in stack!");
    int b = popstack(stack);
    int a = popstack(stack);
    int c = a + b;
    pushstack(stack, c);
}

但是,将采用这样的方式:

void neg(stack_t *stack)
{
    if (depth(stack) < 1)
        perror("Not enough arguments in stack!");
    int a = popstack(stack);
    pushstack(stack, -a);
}

每个职能部门决定它们需要多少论据。 打电话的人不需要知道。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签