【问题标题】:How make this code shorter and accurate this code is just a basic code how to make it more efficient [closed]如何使这段代码更短更准确这段代码只是一个基本代码如何使它更高效[关闭]
【发布时间】:2021-03-13 14:43:30
【问题描述】:
#include<stdio.h>
 int main(){
double a, pi = 3.14, r, c, d, h;
printf("Program to calculate area of circle and volume of cylinder in cm^2 and cm^3 respectively\n");
printf("select what you want to find?\n1.Area of circle\n2.Volume of cylinder\n");
scanf("%lf", &c);
if (c == 1)
{
    printf("Select unit\n1.CM\n2.Meter\n ");
    scanf("%lf", &d);
    if (d == 1)
    {
        printf("enter radius in cm\n");
        scanf("%lf", &r);
        a =pi * r * r;
        printf("the area of circle is \n%lf cm^2", a);
    }
    if (d == 2)
    {
        printf("enter radius in meter\n");
        scanf("%lf", &r);
        a = pi *r * r*10000;
        printf("the are of circle is \n%lf cm^3", a);
    }
}
else
{
    if (c == 2)
    {
        printf("select unit\n1.cm\n2.meter\n");
        scanf("%lf", &d);
    }
    if (d == 1)
    {
        printf("enter radius in cm\n");
        scanf("%lf", &r);
        printf("enter height in cm\n");
        scanf("%lf", &h);
        a = pi*r * r * h;
        printf("the volume of cylinder is\n%lf cm^3", a);
    }
    if (d == 2)
    {
        printf("enter radius in meter \n");
        scanf("%lf", &r);
        printf("enter the height of cylinder in meter \n");
        scanf("%lf",&h);
        a = pi *r * r * h*1000000;
        printf("the volume of cylinder is\n%lf cm^3", a);
    }
}
    return 0;
}

我怎样才能使这段代码更准确和更简短请也解释一下关于更高效代码的概念这个代码只是一个基本代码如果你有一些关于如何使这个代码更先进和有意义的建议,请告诉我to c语言代码请给,因为我刚开始学习c语言,如果有任何错误请告诉我。

【问题讨论】:

  • 您已将一个区域除以100 以进行单位转换。或许您应该除以1000 以获得一个体积,但是将 cm 转换为 m 两者都是错误的,其中 100 是 linear 比率.
  • 不要发布完整的代码。将其缩小为minimal reproducible example
  • 修复了缩进,立即显示问题
  • @pmg,最好讨论一下pitomorrow
  • 忘记float 并使用double ...永远...

标签: c if-statement


【解决方案1】:

逻辑 if-else 块的缩进放错了位置。也没有输入高度变量。

#include<stdio.h>
int main(){
    float a, pi = 3.14, r, c, d, h;
    printf("Program to calculate area of circle and volume of cylinder \n");
    printf("select what you want to find?\n1.Area of circle\n2.Volume of cylinder\n");
    scanf("%f", &c);
    if (c == 1)
    {
        printf("Select unit\n1.CM\n2.Meter\n ");
        scanf("%f", &d);
        if (d == 1)
        {
            printf("enter radius in cm\n");
            scanf("%f", &r);
            a = (pi * r * r) / 100;
            printf("the area of circle is \n%f cm", a);
        }
        if (d == 2)
        {
            printf("enter radius in meter\n");
            scanf("%f", &r);
            a = pi * r * r;
            printf("the are of circle is \n%f meter", a);
        }
    }
    else #This block is indented.
    {
        if (c == 2)
        {
            printf("select unit\n1.cm\n2.meter\n");
            scanf("%f", &d);
        }
        if (d == 1)
        {
            printf("enter radius in cm\n");
            scanf("%f", &r);
            printf("enter height in cm\n");
            scanf("%f", &h);
            a = (pi * r * r * h) / 100;
            printf("the volume of cylinder is\n%f cm", a);
        }
        if (d == 2)
        {
            printf("enter radius in meter \n");
            scanf("%f", &r);
            printf("enter the height of cylinder in meter \n");
            scanf("%f", &h); #This is added
            a = pi * r * r * h;
            printf("the volume of cylinder is\n%f meter", a);
        }
    }
    
    return 0;
}

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2013-03-11
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多