我怎样才能在C中解决这个问题?

时间:2014-10-29 18:05:27

标签: c arrays for-loop

我有一个模拟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"

上面的代码片段仍然不完整,因为我仍然发现很难添加其余的代码。以下是我要解决的问题。

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

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

  2. 如何显示状态字符串?

    我知道C中没有string数据类型,所以我必须声明一个char数组,其中包含状态字符串,但这些是不同状态的不同字符串,它有为每一行打印出来。我想到了if else if else条件,但我无法使char数组生效。

  3. 我已经成功编写了一个代码,可以在每次迭代时打印出用户的BMI,但这并不是它应该如何完成的。

    我想到的另一件事是将BMI值保持在浮点数数组中,然后以上面的格式化方式显示数组中的值,但我仍然对我打算如何做这一点持怀疑态度。

4 个答案:

答案 0 :(得分:2)

这里显示使用数组存储值的完整代码。你使用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;
}

答案 1 :(得分:1)

以下代码将存储4个用户的重量和高度,并将其BMI打印为任意数量的阈值(在这种情况下设置为2)。

您可以通过将重量和高度存储到数组中(在第一个循环中)来解决问题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;
}

答案 2 :(得分:1)

编辑:跳到最后

你需要先将它们保存到某个地方然后才能显示它,你需要使用一个数组来存储所有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 :(得分:1)

  1. 在for-loop
  2. 中添加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[] 例如:
    2. 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