【问题标题】:How can I pull this off in C?我怎样才能在 C 中实现这一点?
【发布时间】:2014-12-25 14:04:45
【问题描述】:

我有一个模拟 BMI(身体质量指数)计算器的程序,它接收来自 10 个用户的输入,询问每个用户的体重和身高,然后程序计算 BMI 并在格式化的输出中打印计算结果.

类似这样的:

                  USER         BMI         STATUS
                  1
                  :
                  10

所以我想到了这个,我会先接受用户的输入,然后尝试以上述格式的方式显示输入的计算。这是我试图提出的方法,它一直询问用户的体重和身高,直到第 10 个用户。

#include <stdio.h>
#include <math.h>

int main(void) {
    
    float weight_value = 0.0f;
    float user_height = 0.0f;
    float BMI = 0.0f;
    char BMI_Status[30];
    
    int i;
    
    for ( i = 1; i <= 10; i++)
    
    {
        printf("Please enter your weight: "); 
        scanf("%f", &weight_value);  //Reads in the user's weight
        printf("Now enter your height: ");
        scanf("%f", &user_height); //Reads in the user's height
        
        BMI = weight_value / (user_height * user_height);
        
    }
    
    return 0;
}

BMI 状态是一个字符串,显示计算出的 BMI 值的不同状态,当 BMI 值在给定范围内时,这些状态为 "Underweight", "Normal", "Overweight"

上面的代码 sn-p 仍然不完整,因为我仍然发现很难添加其余代码。以下是我试图解决的问题。

  1. 我如何在输入 10 个输入后打印出 BMI 值?

    我尝试使用嵌套的 for 循环来打印值,但这是不合逻辑的,因为现在外部 for 循环的每次迭代都会输出 BMI,所以我猜需要一种方法来制作scanf() 在 for 循环块之后保存 BMI 值。

  2. 如何让状态字符串出现?

    我知道 C 中没有 string 数据类型,所以我必须声明一个 char 数组来保存状态字符串,但这些是不同状态的不同字符串,必须打印出来对于每一行。我想为此设置if else if else 条件,但我无法让char 数组工作。

我已经成功编写了一段代码,在每次迭代时打印出用户的 BMI,但这不是它应该这样做的方式。

我想到的另一件事是将 BMI 值保存在浮点数数组中,然后以上述格式显示数组中的值,但我仍然对我打算如何执行此操作持怀疑态度。

【问题讨论】:

  • 您的问题很难想象。我建议向我们展示您希望它看起来像的输出。
  • 在扫描时将值插入数组中是可行的方法。
  • @LeatherFace 我已经通过展示第一段之后的输出应该如何做到这一点。

标签: c arrays for-loop


【解决方案1】:

这里显示了使用数组存储值的完整代码。您使用 if else 来显示一个人的超重程度的想法很好,所以我实现了它。

#include <stdio.h>
#include <math.h>


int main(void) {

    float weight_value[10];  //two arrays of ten elements each
    float user_height[10];

    float BMI = 0.0f;
    char BMI_Status[30];

    int i;

    for ( i = 0; i < 10; i++)   //the array starts from zero in C
    {
        printf("Please enter your weight: "); 
        scanf("%f", &weight_value[i]);  //store the values in the array
        printf("Now enter your height: ");
        scanf("%f", &user_height[i]); //store the values in the array
    }
    printf("USER     BMI\t\tSTATUS\n");

    for( i = 0; i < 10; i++)  //pass the array once again and calculate the needed values
    {
        BMI = weight_value[i] / (user_height[i] * user_height[i]);
        printf("%d\t%f\t", i+1, BMI);
        if(BMI < 30)
            printf("Underweight\n");
        else if(BMI < 50)
            printf("Normal\n");
        else
            printf("Overweight\n");
    }
    return 0;
}

【讨论】:

  • 编辑:很抱歉之前的评论,我已经删除了。我在不知不觉中注释掉了我要打印值的部分。
  • 问题:为什么要包含 stdbool.h?
  • @user3629249 我从命令行编译,所以我只是复制一个已经创建的 .c 文件并编辑它的内容,所以对于这个文件,我必须在不知不觉中包含 stdbool.h 头文件之一文件。 ;p
【解决方案2】:

已编辑:跳到最后

您需要先将它们保存在某个地方,然后才能读取它们以显示它,您需要使用一个数组来存储所有 10 个输入,此时每次用户输入数据时,它都会重写变量,包含最后一个用户的数据。

它需要看起来像这样:

...
float weight_value[10];
float user_height[10];
float BMI =[10];
char BMI_Status[30][3];



for ( int i = 1; i <= 10; i++)

{
    printf("Please enter your weight: "); 
    scanf("%f", &weight_value[i]);  //Reads in the user's weight
    printf("Now enter your height: ");
    scanf("%f", &user_height[i]); //Reads in the user's height

    BMI[i] = weight_value / (user_height * user_height);

}
...

然后显示你将需要这样的循环

...
printf("user \t weight \t height \t BMi"); 

for ( int i = 1; i <= 10; i++)

{
    printf("%d \t%f \t%f \t%s ", i,user_weight[i],user_height[i],BMI[i]);

}
...

我只是重新阅读了问题并意识到我看错了,但是最后一点显示了如何从数组中读取它们并显示它们。

【讨论】:

    【解决方案3】:

    以下代码将存储 4 个用户的体重和身高,并针对任意数量的阈值(在本例中设置为 2)打印他们的 BMI。

    您可以通过将重量和高度存储到数组中(在第一个循环中)来解决问题 1。

    然后,您可以通过首先定义一组阈值和相应的状态字符串来显示 BMI 状态字符串。在第二个循环中,为每个用户计算并检查 BMI 值是否低于给定阈值并打印其相应的状态字符串。

    #include <stdio.h>
    #include <math.h>
    #include <stdbool.h>
    
    #define USERS 4
    #define THRESHOLDS 2
    
    int main(void) {
    
        float weight[USERS];
        float height[USERS];
        float BMI = 0.0f;
        char * BMI_Status[THRESHOLDS+1] = {"Normal","Overweight","Obese"};
        float thresholds[THRESHOLDS] = {10.0,20.0};
    
        int i,j;
    
        for ( i = 0; i < USERS; i++)
    
        {
            printf("Please enter your weight: ");
            scanf("%f", &(weight[i]));  //Reads in the user's weight                                                                                                                                                                             
            printf("Now enter your height: ");
            scanf("%f", &(height[i])); //Reads in the user's height                                                                                                                                                                              
    
        }
    
        for ( i = 0; i < USERS; i++)
    
        {
            BMI = weight[i]/(height[i]*height[i]);
            printf("%d\t%f\t",i,BMI);
            for ( j = 0 ; j < THRESHOLDS ; j++ ){
                if ( BMI < thresholds[j]){
                    break;
                }
            }
            printf("%s\n",BMI_Status[j]);
        }
    
    
        return 0;
    }
    

    【讨论】:

      【解决方案4】:
      1. 在 for 循环中添加 if 语句
      float BMI[10];
      int j;
      
      for ( i = 1; i <= 10; i++) 
      {
        printf("Please enter your weight: "); 
        scanf("%f", &weight_value);  //Reads in the user's weight
        printf("Now enter your height: ");
        scanf("%f", &user_height); //Reads in the user's height
        
        BMI[i-1] = weight_value / (user_height * user_height);
        if (i==10)
            for (j=0; j<10; j++)
                 printf ("%d\n", BMI[j]); 
      }
      
      1. 使用char **achar *a[] 例如:
      char s[12] = "Hello World!"; 
      char *p; 
      p = &s[0];
      
      char *BMI_Status[30];             //declare 30 char* 
      BMI_Status[0] = "Fat"; 
      BMI_Status[1] = "Thin";
      ...
      //output
      printf ("%s", BMI_Status[0]);            //Fat
      

      【讨论】:

      • 谢谢 Kir,我只是需要澄清一些事情,我是否必须将指针声明为 C 中字符串的 char 数组?
      • 在 C 和 C++ 中,String 是一个 char 指针(指向 char 数组的起始地址),所以如果你想在 C 中使用 string,你应该声明char* s。您可以使用char *s = (char*)malloc(sizeof(char)*size) 安全地分配动态大小的字符串。
      • 当您使用printf ("%s", s); 时,结果从s[0] 开始到s[n]=\0。如果您声明char *s = "Fat"。其实有Fat\0知道在哪里停止打印。
      • 但是当我以这种方式声明字符串时,我的编译器会出现这个错误:expected expression before '{' token,我声明了一个这样的 char 数组:char *BMI_Status[20]; BMI_Status[1] = {"Underweight"};
      • 我修改了上面的代码。 (修剪{ } 因为{" "} 表示char**。但BMI_Status[0] 是char*
      猜你喜欢
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 2021-10-01
      • 2020-02-07
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多