首页 > 解决方案 > 我怎样才能让用户选择单位

问题描述

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

#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; 
}

标签: c

解决方案


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

#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-else if-else梯子。因此,您可以swtich-case从下面的代码中删除语句并将其替换为以下代码:

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!");
}

推荐阅读