Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何让用户选择设备_C - Fatal编程技术网

C 如何让用户选择设备

C 如何让用户选择设备,c,C,我有一个转换器,但它可以转换程序中的所有单位。如何让打开项目的用户选择要转换的单位类型 #include<stdio.h> #include<conio.h> int main() { float m, f, l, g, cm, inch; printf("Type meter : "); scanf("%f",&m); f = 3.2808399 * m; printf("feets: %f",f);

我有一个转换器,但它可以转换程序中的所有单位。如何让打开项目的用户选择要转换的单位类型

#include<stdio.h> 
#include<conio.h> 

int main() 
{ 
    float m, f, l, g, cm, inch; 
    printf("Type meter : "); 
    scanf("%f",&m); 
    f = 3.2808399 * m; 
    printf("feets: %f",f); 

    printf("\nType gallons : "); 
    scanf("%f",&g); 
    l = 3.78541178 * g; 
    printf("litres: %f",l); 

    printf("\ninches : "); 
    scanf("%f", &inch); 
    cm = 2.54 * inch; 
    printf("cm: %f", cm); 

    return 0; 
}
#包括
#包括
int main()
{ 
浮子m、f、l、g、厘米、英寸;
printf(“类型仪表:”);
scanf(“%f”、&m);
f=3.2808399*m;
printf(“feets:%f”,f);
printf(“\n加仑:”);
scanf(“%f”、&g);
l=3.78541178*g;
printf(“升:%f”,升);
printf(“\ninches:”);
扫描频率(“%f”和英寸);
厘米=2.54*英寸;
printf(“厘米:%f”,厘米);
返回0;
}

就编程的复杂性、可移植性、优化(内存/时间)和许多其他方面而言,下面的代码肯定不是最好的代码,但它应该能让您继续前进。 我添加了注释来解释代码。几乎所有带有这些
printf
s的代码都是不言自明的

#include<stdio.h>

int main(void)
{
    // We need just 3 variables
    // This one is for getting the user option
    int choice = 0;
    // We need these to float variables for user input and an output
    float Input = 0.0, Output = 0.0;

    // Following code till `while(1)` is optional.
    printf("\nThis is a converter with a fixed set of functions.");
    printf("\nNote: This converter does support floating point inputs.");
    printf("\nNote: Floating point inputs and outputs are truncated to 2 digits after decimal place.");
    printf("\nNote: Press any key to acknowledge!");
    getchar();

    // To get user input multiple times, you'll need to loop
    while(1)
    {
        printf("\n\nFollowing functions are supported, enter a suitable choice form the list below.");
        printf("\nPress `1` for Converting Metres to Feet.");
        printf("\nPress `2` for Converting Gallons to Litres.");
        printf("\nPress `3` for Converting Inches to Centimetres.");
        printf("\nPress `0` for Exiting the program.");

        printf("\nEnter your Option :  ");
        scanf("%d", &choice);

        // Lets implement a switch-case statement to get the job done
        switch(choice)
        {
            case 1:
                    printf("Enter input value (in Metres) : "); 
                    scanf("%f",&Input); 
                    Output = 3.2808399 * Input; 
                    printf("%0.2f Metres is equal to %0.2f Feets", Input, Output); 

                break;

            case 2:
                    printf("Enter input value (in Gallons) : "); 
                    scanf("%f",&Input); 
                    Output = 3.78541178 * Input; 
                    printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output); 

                break;

            case 3:
                    printf("Enter input value (in Inches) : "); 
                    scanf("%f",&Input); 
                    Output = 2.54 * Input; 
                    printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output); 

                break;

            case 0:
                    printf("Thank you. The program will exit now!\n\n");
                    return 0;
                break;
            // This default case should take care of the invalid set of choices entered by user 
            default:
                printf("Option you entered is either invalid or is not supported as of now!");
        }
    }

    return 0;
}

虽然这段代码可以解决这个问题,但最好添加精化,并向可能不理解这段代码的人解释它是如何工作的。
#include<stdio.h>

int main(void)
{
    // We need just 3 variables
    // This one is for getting the user option
    int choice = 0;
    // We need these to float variables for user input and an output
    float Input = 0.0, Output = 0.0;

    // Following code till `while(1)` is optional.
    printf("\nThis is a converter with a fixed set of functions.");
    printf("\nNote: This converter does support floating point inputs.");
    printf("\nNote: Floating point inputs and outputs are truncated to 2 digits after decimal place.");
    printf("\nNote: Press any key to acknowledge!");
    getchar();

    // To get user input multiple times, you'll need to loop
    while(1)
    {
        printf("\n\nFollowing functions are supported, enter a suitable choice form the list below.");
        printf("\nPress `1` for Converting Metres to Feet.");
        printf("\nPress `2` for Converting Gallons to Litres.");
        printf("\nPress `3` for Converting Inches to Centimetres.");
        printf("\nPress `0` for Exiting the program.");

        printf("\nEnter your Option :  ");
        scanf("%d", &choice);

        // Lets implement a switch-case statement to get the job done
        switch(choice)
        {
            case 1:
                    printf("Enter input value (in Metres) : "); 
                    scanf("%f",&Input); 
                    Output = 3.2808399 * Input; 
                    printf("%0.2f Metres is equal to %0.2f Feets", Input, Output); 

                break;

            case 2:
                    printf("Enter input value (in Gallons) : "); 
                    scanf("%f",&Input); 
                    Output = 3.78541178 * Input; 
                    printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output); 

                break;

            case 3:
                    printf("Enter input value (in Inches) : "); 
                    scanf("%f",&Input); 
                    Output = 2.54 * Input; 
                    printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output); 

                break;

            case 0:
                    printf("Thank you. The program will exit now!\n\n");
                    return 0;
                break;
            // This default case should take care of the invalid set of choices entered by user 
            default:
                printf("Option you entered is either invalid or is not supported as of now!");
        }
    }

    return 0;
}
if(choice == 0)
{
    printf("Thank you. The program will exit now!\n\n");
    return 0;    
}
else if(choice == 1)
{
    printf("Enter input value (in Metres) : "); 
    scanf("%f",&Input); 
    Output = 3.2808399 * Input; 
    printf("%0.2f Metres is equal to %0.2f Feets", Input, Output); 
}
else if(choice == 2)
{
    printf("Enter input value (in Gallons) : "); 
    scanf("%f",&Input); 
    Output = 3.78541178 * Input; 
    printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output); 
}
else if(choice == 3)
{
    printf("Enter input value (in Inches) : "); 
    scanf("%f",&Input); 
    Output = 2.54 * Input; 
    printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output); 
}
else
{
    // This should take care of the invalid set of choices entered by user 
    printf("Option you entered is either invalid or is not supported as of now!");
}