微信公众号开发

C语言身高预测小程序(利用if、else、else if)(代码原创)

本文主要是介绍C语言身高预测小程序(利用if、else、else if)(代码原创),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

#include  <stdio.h>
#include <stdlib.h>
int main(void)
{
 char s;         //孩子的性别
 char sp;   //是否喜欢体育运动
 char d;       //是否有良好的饮食习惯
 float h;      //我的身高
 float fh;   //父亲的身高
 float mh;   //母亲的身高
 printf("Are you a boy(M) or a girl(F)?\n");
 scanf(" %c",&s);
 if (!(s=='m' || s=='M' || s=='f' || s=='F'))
    printf("\nInput sex error!");
 else
 {
   if(s=='m'||s=='M')
   {
        printf("Please input your father's height(cm):\n");
        scanf(" %f",&fh);
        printf("Please input your mother's height(cm):\n");
        scanf(" %f",&mh);
        h=(fh+mh)*0.54;
        printf("Do you like sports(Y/N)?\n");
        scanf(" %c",&sp);
        if (!(sp=='y' || sp=='Y' || sp=='n' || sp=='N'))
        {
            printf("\nInput sports error!");
        }
        else
        if (sp=='y' || sp=='Y')
        {
            h=1.02*h;
        }
        printf("Do you have a good habit of diet(Y/N)?\n");
        scanf(" %c",&d);
        if (!(d=='y' || d=='Y' || d=='n' || d=='N'))
        {
            printf("Input diet error!\n");
        }
        else
        if (d=='y' || d=='Y')
        {
            h=h*1.015;
            printf("Your future height will be %f(cm)\n",h);
        }
    }
    else
    {
        if(s=='f'||s=='F')
        printf("Please input your father's height(cm):\n");
        scanf(" %f",&fh);
        printf("Please input your mother's height(cm):\n");
        scanf(" %f",&mh);
        h=(fh*0.923+mh)/2;
        printf("Do you like sports(Y/N)?\n");
        scanf(" %c",&sp);
        if (!(sp=='y' || sp=='Y' || sp=='n' || sp=='N'))
        {
            printf("\nInput sports error!");
        }
        else
        if (sp=='y' || sp=='Y')
        {
            h=1.02*h;
        }
        printf("Do you have a good habit of diet(Y/N)?\n");
        scanf(" %c",&d);
        if (!(d=='y' || d=='Y' || d=='n' || d=='N'))
        {
            printf("Input diet error!\n");
        }
        else
        if (d=='y' || d=='Y')
        {
            h=h*1.015;
            printf("Your future height will be %f(cm)\n",h);
        }
    }
    }
    return 0;
}

 

 

这篇关于C语言身高预测小程序(利用if、else、else if)(代码原创)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!