C语言计算器的3种实现方法代码

  #include

  int Add(int x, int y)

  {

  return x + y;

  }

  int Sub(int x, int y)

  {

  return x - y;

  }

  int Mul(int x, int y)

  {

  return x * y;

  }

  int Div(int x, int y)

  {

  return x / y;

  }

  void menu()

  {

  printf("***************************

  ");

  printf("*** 1. Add 2. Sub ***

  ");

  printf("*** 3. Mul 4. Div ***

  ");

  printf("*** 0. Exit ***

  ");

  printf("***************************

  ");

  }

  int main()

  {

  int input = 0;

  int x = 0;

  int y = 0;

  int ret = 0;

  do

  {

  menu();

  printf("请选择:>");

  scanf("%d", &input);

  switch (input)

  {

  case 1:

  printf("请输入:>");

  scanf("%d %d", &x, &y);

  ret = Add(x, y);

  printf("%d

  ", ret);

  break;

  case 2:

  printf("请输入:>");

  scanf("%d %d", &x, &y);

  ret = Sub(x, y);

  printf("%d

  ", ret);

  break;

  case 3:

  printf("请输入:>");

  scanf("%d %d", &x, &y);

  ret = Mul(x, y);

  printf("%d

  ", ret);

  break;

  case 4:

  printf("请输入:>");

  scanf("%d %d", &x, &y);

  ret = Div(x, y);

  printf("%d

  ", ret);

  break;

  case 0:

  printf("退出计算器

  ");

  break;

  default:

  printf("输入错误,请重新输入

  ");

  break;

  }

  } while (input);

  }