男性,女性理想体重计算的C程序

问题描述 投票:0回答:3

我根据身高和性别编写了以下程序来检查男性和女性的理想体重:

#include<stdio.h>
#include<conio.h>

int main()
{
    int age;
    float height, mminima = 48, fminima = 45, iw;
    char gender;

    printf("Please Enter your Age,Height(in CM) and Gender\n");

    scanf("%d\n%f\n%c", &age, &height, &gender);

    if((gender = 'M') && (height < 152.4))
    {
         iw = mminima - (152.4 - height) * 1.1;
         printf("\nYour idle Weight should be= %f", iw);
    }
    else
    {
         iw = mminima + (height - 152.4) * 1.1;
         printf("\nYour Idle Wight should be= %f", iw);
    }

    if((gender = 'F') && (height < 152.4))
    {
        iw = fminima - (152.4 - height) * 1.1;
        printf("\nYour Idle weight should be= %f", iw);
    }
    else
    {
        iw = fminima + (height - 152.4) * 1.1;
        printf("\nYour Idle weight should be= %f", iw);
    }

    getch();
    return 0;
}

但是如果声明没有比较性别,那么输出总是显示我对男性和女性的理想体重。为什么?我哪里错了?请帮忙!!

c
3个回答
1
投票

应该纠正几个问题:

1)错误的比较:

if(gender='M') // always true; should be if(gender=='M')
if((gender = 'F') // always true; should be if(gender=='M')

2)程序的逻辑是错误的,如果高度高于Ideal Weight,它将为女性和男性打印152.4

3)应该封装在函数中的大量重复代码。

4)错误的性别符号缺乏错误处理。

5)需要正确的拼写和打印输出。正确:Idle Wigh

6)没有必要打印重量作为74.3456789 .后的一位数就足够了。

7)常量幻数应该在main之外定义,例如:

#define MAGIC_HEIGHT 152.4

8)使用switchcase可以提高程序的清晰度:

例:

#include<stdio.h>
#include<conio.h>

#define M_MIN         48.0
#define F_MIN         45.0
#define FACTOR         1.1
#define MAGIC_HEIGHT 152.4

void print_ideal_weight(float iw)
{
    printf("\nYour Ideal weight should be = %.1f", iw);
}

float calculate_ideal_weight(float m, float height)
{
    float iw;

    if(height < MAGIC_HEIGHT){

        iw = m - (MAGIC_HEIGHT - height) * FACTOR;
    }
    else{
        iw = m + (height - MAGIC_HEIGHT) * FACTOR;
    }            
    return iw;
}

int main()
{
    int age;
    float height, iw;
    char gender;

    printf("Please Enter your Age, Height(in CM) and Gender(F/M): \n");

    scanf("%d\n%f\n%c", &age, &height, &gender);

    switch (gender)
    {
        case 'M':
            iw = calculate_ideal_weight(M_MIN,height);
            print_ideal_weight(iw);
        break;

        case 'F':
            iw = calculate_ideal_weight(F_MIN,height);
            print_ideal_weight(iw);
        break;

        default:
            printf("Unknown gender entered!\n");
        break;
    }

    getch();
    return 0;
}

输出:

Please Enter your Age, Height(in CM) and Gender(F/M):                                                             
25                                                                                                                
176                                                                                                               
M                                                                                                                 

Your Ideal weight should be = 74.0  

4
投票

你的if语句qazxsw poi要求两个条件都是真的......然后你的qazxsw poi部分将被执行而不管性别。

如果高度高于152.4,则无论性别如何,if((gender='M')&&(height<152.4))都将运行

你应该把这两个条件分开:

else

如前所述,您应该将else更改为if(gender=='M') { if (height<152.4) { } else { } } if(gender=='F') { if (height<152.4) { } else { } }


2
投票

代码中的逻辑是错误的。首先你有一个错字

gender='F'

gender=='F'是一项任务,将永远返回true。

其次,如果其中任何一个条件都是假的,那就是if((gender='M')&&(height<152.4)) 部分 - 即当gender='M'else时。你需要将你的gender分成不同的条件。首先检查'F',然后一旦你确认它等于if,那么你可以检查gender,如下所示:

'M'

然后你想做一个height检查if(gender=='M') { if(height<152.4) { iw=mminima-(152.4-height)*1.1; printf("\nYour idle Weight should be= %f",iw); } else { iw=mminima+(height-152.4)*1.1; printf("\nYour Idle Wight should be= %f",iw); } } 等于else if像这样:

gender

因为'F'只能是两个有效值中的一个,所以如果它匹配一个,它就不能匹配另一个。你可以添加一个最终的else if(gender=='F') { if(height<152.4) .... 来处理gender既不是else也不是gender并报告错误的情况。

© www.soinside.com 2019 - 2024. All rights reserved.