C语言程序作业1

发布时间 2023-11-12 02:34:03作者: mystayx

5.9 使用if-else语句编程,将输入的百分制成绩score转换成相应的五分制成绩后输出。

90<=score<=100    输出:A 

80<=score<90        输出:B 

70<=score<80        输出:C

60<=score<70        输出:D 

  0<=score<60        输出:E

如果score不在0~100之间,输出:Input error!

测试用例:95、85、75、65、55,分别输出A、B、C、D、E

#include<stdio.h>
#include<stdlib.h>
int main()
{
int score;
scanf("%d",&score);
if(90<=score&&score<=100)
{
printf("A\n");
}
else if(80<=score&&score<90)
{
printf("B\n");
}
else if(70<=score&&score<80)
{
printf("C\n");
}
else if(60<=score&&score<70)
{
printf("D\n");
}
else if(0<=score&&score<60)
{
printf("E\n");
}
else
{
printf("Input error");
}
return 0;
}

5.10 从键盘输入某年某月(包括闰年),用switch语句编程输出该年的该月拥有的天数。要求考虑闰年以及输入月份不在合法范围内的情况。已知闰年的2月有29天,平年的2月有28天。

测试用例:

    输入:2023 2        输出:28天

    输入:2020 2        输出:29天

#include<stdio.h>
#include<stdlib.h>
int main()
{
int year,month;
scanf("%d%d",&year,&month);
switch (month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
printf("31天\n");
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
{
printf("29天\n");
}
else
{
printf("28天\n");
}
break;
case 4:case 6:case 9:case 11:
printf("30天\n");
break;
default:printf("Input error!\n");
}
return 0;
}

 

5.11 设faHeight为其父身高,moHeight为其母身高,身高预测公式为

            男性成人时身高 = (faHeight + moHeight) * 0.54 cm

            女性成人时身高 = (faHeight * 0.923 + moHeight) / 2.0 cm

    此外,如果喜爱体育锻炼,那么可增加身高2%;如果有良好的卫生饮食习惯,那么可增加身高1.5%。

    请编程从键盘输入用户的性别(用字符型变量sex存储,输入字符F表示女性,输入字符M表示男性)、父母身高(用实型变量存储,faHeight为其父身高,moHeight为其母身高)、是否喜爱体育锻炼(用字符型变量sports存储,输入字符Y表示喜爱,输入字符N表示不喜爱)、是否有良好的饮食习惯等条件(用字符型变量diet存储,输入字符Y表示良好,输入字符N表示不好),利用给定公式和身高预测方法对身高进行预测。

测试用例:

    输入:M 180 160 Y Y        输出:190

    输入: F 180 160 Y Y        输出:169

 

#include<stdio.h>
int main(void)
{
    char sex, sports, diet;
    int faHeight, moHeight;
    double myHeight;
    scanf("%c %d %d %c %c", &sex, &faHeight, &moHeight, &sports, &diet);
    if (sex == 'F') 
    {
       myHeight = (faHeight * 0.923 + moHeight) / 2.0;
        if (sports == 'Y', diet == 'Y')
            myHeight = myHeight + (myHeight * 0.02) + (myHeight * 0.015);
        else if (sports == 'Y', diet == 'N')
          myHeight = myHeight + (myHeight * 0.02);
        else if (sports == 'N', diet == 'Y')
           myHeight = myHeight + (myHeight * 0.015);
        else (sports == 'N', diet == 'N');
          myHeight =myHeight * 1;           
    }
    else
    {
        myHeight = (faHeight + moHeight) * 0.54;
        if (sports == 'Y', diet == 'Y')
           myHeight = myHeight + (myHeight * 0.02) + (myHeight * 0.015);
        else if (sports == 'Y', diet == 'N')
            myHeight = myHeight + (myHeight * 0.02);
        else if (sports == 'N', diet == 'Y')
            myHeight = myHeight + (myHeight * 0.015);
        else(sports == 'N', diet == 'N');
      myHeight = myHeight * 1;
    }    
        printf("%.0lf\n", myHeight);
    return 0;
}
#include<stdio.h>
//#include<stdlib.h>
int main()
{
char sex, sports, diet;
float myHeight, faHeight, moHeight;
//printf("Are you a boy(M) or a girl(F)? ");
scanf(" %c",&sex);
//printf("Please input your father's height(cm): ",&faHeight);
scanf("%f",&faHeight);
//printf("Please input your mother's height(cm): ",&moHeight);
scanf("%f",&moHeight);
//printf("Do you like sports(Y/N)? ");
scanf(" %c",&sports);
//printf("Do you have a good habit of diet(Y/N)? ");
scanf(" %c",&diet);
if(sex=='M' || sex=='m')
myHeight = (faHeight + moHeight) * 0.54;
else
myHeight = (faHeight * 0.923 + moHeight) / 2.0;
if(sports == 'Y' || sports == 'y')
myHeight = myHeight * (1 + 0.02);
if(diet == 'Y' || diet == 'y')
myHeight = myHeight * (1 + 0.015);
printf("%.0f\n",myHeight);
//system("pause");
return 0;
}

 

5.12 体型判断:

            体指数 t = 体重 w /(身高 h)2        (w单位为千克,h单位为米)

    当 t < 18 时,为低体重;

    当 t 介于 18 和 25 之间时,为正常体重;

    当 t 介于 25 和 27 之间时,为超重体重;

    当 t >=27 时,为肥胖。

    请使用 if语句 或 else- if语句编程,从键盘输入你的身高h 和 体重w,根据上述给定的公式计算体指数t,然后判断你的体重属于何种类型。

测试每一个分支,分别输出低体重、正常体重、超重体重、肥胖

#include <stdio.h>
int main() {
    double t,h,w;              
    scanf("%lf %lf",&h,&w);
    t=w/(h*h);
    if(t<18)
        printf("低体重\n");
    else if (t>=18 && t<25)
        printf("正常体重\n");
    else if (t>=25 && t<27)
        printf("超重体重\n");
   else
        printf("肥胖\n");
    return 0;
}