eclipse 带有 switch 语句的交互式菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15469064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:12:29  来源:igfitidea点击:

interactive menu with switch statement

ceclipse

提问by uplearnedu.com

Hi I'm trying to make an interactive menu with switch statement in C. Though I'm unsure of how to trigger a function that has certain arguments. I'm a total beginner and I'm stumped how to do this. The function in the switch statement needs the arguments though I would like the function to ask for the numbers. I'm doing this as an assignment and cannot provide the actual code so I made this mock up. Thank you for your help.

嗨,我正在尝试使用 C 语言中的 switch 语句制作一个交互式菜单。虽然我不确定如何触发具有某些参数的函数。我是一个完全的初学者,我很难过如何做到这一点。switch 语句中的函数需要参数,尽管我希望函数要求输入数字。我把它作为一项任务来做,不能提供实际的代码,所以我做了这个模型。感谢您的帮助。

Here is an example of code I might use.

这是我可能会使用的代码示例。

#include <stdio.h>

void printMenu()
{
    int choice;

    do
    {
        printf("Main Menu:\n");
        printf("1) do this\n");
        scanf("%d", &choice);

        switch (choice)
        {
            case 1:
                function(); /* though this needs the arguments */
                break;
        }
    } while (choice != 7);

    int main(void)
    {
        printMenu();
        return 0;
    }

    void function(int number1, float number2)
    {
        /*calculation*/
        printf("enter your numbers");
        /* Not sure how to read the numbers in here */
        printf("%d + %d = %d", number1, number2, number1 + number2);
        return;
    }

回答by AusCBloke

If you want the switchto be as minimal as possible then just call another function which takes in input and then calls the function...

如果您希望switch尽可能小,那么只需调用另一个接收输入的函数,然后调用该函数...

case 1:
   read_input_and_function()
   break;

...

void read_input_and_function(void)
{
   printf("Enter your numbers: ");
   /* scanf number1, number2 */
   function(number1, number2);
}

回答by Barath Ravikumar

The function in the switch statement needs the arguments though I would like the function to ask for the numbers.

switch 语句中的函数需要参数,尽管我希望函数要求输入数字。

How about asking the arguments first , and then calling the function. This way the two arguments can be declared once and be used in other functions of the same switch , but be defined according to the chosen case.

如何先询问参数,然后调用函数。这样,两个参数可以声明一次,并在同一 switch 的其他函数中使用,但根据选择的情况进行定义。

void function1(int, float);

   void printMenu()
   {

     int choice = 0 , num1 = 0;
     float num2 = 0;

     do
      {
          printf("Main Menu:\n");
          printf("1) do this\n");
          scanf("%d", &choice);

          switch (choice)
          {
              case 1:
                  printf("\nEnter number 1\n");
                  scanf("%d",&num1);
                  printf("\nEnter number 2\n");
                  scanf("%f",&num2);
                  function1(num1,num2);
                  break;
          }
      } while (choice != 7);
    }

回答by ricdnts

#include <stdio.h>
#include <stdlib.h>

#define Pi 3.14159216

/*  
*small program of how to create a menu
*/

main ()
{
float degree,radians;
int input;
/*degrees to radians */
float degreesToRadians (float deg)
{
return ((Pi * deg) / 180.0);
}

/*radians to degrees*/
float radiansToDegrees (float rad)
{
 return rad * (180 / Pi);
}
void menu ()
{
 printf ("\n");
 printf ("1. degrees\n");
 printf ("2. radians\n");
 printf ("3. quit\n");
do
switch (input) {
case 1:
          printf ("\n");
          printf ("\n");

          printf (" Enter value of degrees: ");
          scanf ("%f", &degree);
          printf ("RADIANS = %f \n\n", degreesToRadians (degree));
          menu (); 
          break;

case 2:
          printf ("\n");
          printf ("\n");

          printf (" Enter value of radians: ");
          scanf ("%f", &radians);
          printf ("DEGREES = %f \n\n", radiansToDegrees (radians));
          menu (); 
          break;
case 3:
          printf (" quiting app \n");
          exit (0);
          break;
default:
      printf ("wrong option\n");
      break;
  }
while (input != 3);
    getchar ();   
   }
 }
menu ();
}