C距离计算错误

时间:2019-01-29 17:44:36

标签: c

我用C语言编写了一些代码,将您的距离转换为其他距离单位,但似乎混淆了数字并错误地计算了结果。

我使用Windows 10笔记本电脑,但是我认为这并不重要,因为我使用了在线编译器(GDB)编写代码。我真的不知道问题出在哪里。

#include <stdio.h>

int main ()
{
    float distance;
    char in_unit[20];

    printf("Enter your distance:\n");
    scanf("%f", &distance);

    printf("Enter the length unit of your distance.(C=centimeters, Ft=feet, Kg=kilometers, In=inches, M=miles, Mtrs=meters)\n");
    scanf("%s", in_unit);
    if (in_unit == "Cm");
    {
        printf("Your distance in feet is:%f", distance/30.48);
        printf("\nYour distance in kilometers is:%f", distance/100000);
        printf("\nYour distance in inches is:%f", distance/2.54);
        printf("\nYour distance in miles is:%f", distance/160934.4);
        printf("\nYour distance in meters is:%f", distance/100);
    }
    if (in_unit == "Ft")
    {
        printf("Your distance in centimeters is:%f", distance*30.48);
        printf("\nYour distance in kilometers is:%f", distance/3280.84);
        printf("\nYour distance in inches is:%f", distance*12);
        printf("\nYour distance in miles is:%f", distance/5280);
        printf("\nYour distance in meters is:%f", distance/3.281);
    }
    if (in_unit == "Kg")
    {
        printf("Your distance in centimeters is:%f", distance * 100000);
        printf("\nYour distance in feet is:%f", distance * 3280.84);
        printf("\nYour distance in inches is:%f", distance * 39370.079);
        printf("\nYour distance in miles is:%f", distance / 1.609);
        printf("\nYour distance in meters is:%f", distance * 1000);
    }
    if (in_unit == "In")
    {
        printf("Your distance in centimeters is:%f", distance*2.54);
        printf("\nYour distance in feet is:%f", distance/12);
        printf("\nYour distance in kilometers is:%f", distance/39370.079);
        printf("\nYour distance in miles is:%f", distance/63360);
        printf("\nYour distance in meters is:%f", distance/39.37);
    }
    if (in_unit == "M")
    {
       printf("Your distance in centimeters is:%f", distance*160934.4);
       printf("\nYour distance in feet is:%f", distance*5280);
       printf("\nYour distance in kilometers is:%f", distance*1.609);
       printf("\nYour distance in inches is:%f", distance*63360);
       printf("\nYour distance in meters is:%f", distance*1609.344);
    }
    if (in_unit == "Mtrs")
    {
       printf("Your distance in centimeters is:%f", distance*100);
       printf("\nYour distance in feet is:%f", distance*3.281);
       printf("\nYour distance in kilometers is:%f", distance/1000);
       printf("\nYour distance in inches is:%f", distance*39.37);
       printf("\nYour distance in miles is:%f", distance/1609.344);
    }
    return 0;
}

例如,这是运行之一:

Enter your distance:                                                                                                         
1
Enter the length unit of your distance. (C=centimeters, Ft=feet, Kg=kilometers, In=inches, M=miles, Mtrs=meters)              
Kg                                                                                                                           
Your distance in feet is:0.032808                                                                                            
Your distance in kilometers is:0.000010                                                                                      
Your distance in inches is:0.393701
Your distance in miles is:0.000006                                                                                           
Your distance in meters is:0.010000

0 个答案:

没有答案