C语言(分支练习)

发布于:2024-12-06 ⋅ 阅读:(34) ⋅ 点赞:(0)

1.输⼊你的⾝⾼和体重,测试你的健康状况。

计算bmi的值, bmi = (体重(kg)/⾝⾼(m)的平⽅) 如果bmi ⼩于18.5,
则显⽰“偏瘦,注意加强营 养” 如果bmi 在18.5和23.9之间,则显⽰“体重指
数良好,注意保持” 如果bmi值⼤于23.9 ,则显⽰“你 有点偏胖,注意锻炼”

 #include<stdio.h>
 
int main()
 {
    double weight,height,bmi;
    //输入体重和身高
    printf("请输入体重(KG):");
    scanf("%lf",&weight);
    printf("请输入身高(m):");
    scanf("%lf",&height);
    
    //计算BMI值
    bmi=weight/(height*height);
    //根据bmi值显示相应的信息
    if(bmi<18.5)
    {
        printf("偏瘦,注意加强营养,BMI值为%lf\n",bmi);
    }else if(bmi>=18.5&&bmi<23.9)
    {
        printf("体重指数良好,注意保持,BMI值为%lf\n",bmi);
    }else
    {
        printf("你有点偏胖,注意锻炼,BMI值为%lf\n",bmi);
    }
    return 0;
    
}

在这里插入图片描述

2.西安市对安装“⼀⼾⼀表”的居⺠⽤⼾按阶梯式累进电价进⾏计收电费,具体⽅案是

⽉⽤电量低于50千⽡时部分,电价不调整,仍为0.538元/千⽡时;51〜200千
⽡时部分, 单价为0.568元/千 ⽡时;超过200千⽡时部分,电价为0.638元/千
⽡时。市区徐某家4⽉份 的⽤电量为96千⽡时,其电费计算 如下:基本电费
部分:96千⽡时×0.538元/千⽡时=51.65元; 超出50千⽡时的调价电费:(96
〜50)千⽡时 ×0.03元/千⽡时=1.38元;电费合计51.65+1.38=53.03元 。 “阶
梯式电价”机制可有效地抑制电⼒浪费现 象,引导居⺠节约⽤电,合理⽤
电,⽐纯粹“⼝号倡导型” 节约⽤电更加有效。 编写⼀个程序根据输⼊ 的
⽤电量,计算电费。

#include<stdio.h>
 int main()
 {
    double elec,bill=0.0;
    //输入用电量
    printf("输入用电量(千瓦时):")scanf("%lf",&elec);
    
    if(elec<=50)
    {
        bill=elec*0.538;
    }else if(elec<=200)
    {
        bill=50*0.538+(elec-50)*0.538;
    }else
    {
        bill=50*0.538+150*0.568+(elec-200)*0.638;
    }
    printf("电费合计:%.2lf元",bill);
    return 0;
 }

在这里插入图片描述

3.设计⼀个计算个⼈所得税的软件应纳税所得额 = ⼯资收⼊⾦额 - 各项社会保险费(650元) - 起征点(3500元)

应纳税额 = 应纳税所得额 x 税 率 - 速算扣除数
在这里插入图片描述

#include <stdio.h>
 int main()
 {
 double salary, tax;
 printf("please slary\n");
 scanf("%lf", &salary);
 if (salary <= 36000)
 {
 tax = salary * 0.03;
 printf("缴税%f元\n", tax);
 }
 else if (salary > 36000 && salary <= 144000)
 {
 tax = salary * 0.1 - 2520;
 printf("缴税%f元\n", tax);
 }
 else if (salary > 144000 && salary <= 300000)
 {
 tax = salary * 0.2 - 16920;
 printf("缴税%f元\n", tax);
 }
 else if (salary > 300000 && salary <= 420000)
 {
 tax = salary * 0.25 - 31920;
 printf("缴税%f元\n", tax);
 }
 else if (salary > 420000 && salary <= 660000)
 {
 tax = salary * 0.3 - 52920;
 printf("缴税%f元\n", tax);
 }
 else if (salary > 660000 && salary <= 960000)
 {
 tax = salary * 0.35 - 85920;
 printf("缴税%f元\n", tax);
 }
 else if (salary > 960000)
 {
 tax = salary * 0.4 - 181920;
 printf("缴税%f元\n", tax);
 }
 return 0;
 }

在这里插入图片描述

4. 设计TVM(地铁⾃动售票机)机软件。

输⼊站数,计算费⽤,计费规则,6站2元,7-10站3元,11站以上为4元。
输⼊钱数,计算找零(找零时优先找回⾯额⼤的钞票),找零⽅式为各种⾯额
张数,可识别⾯额: 100,50,20,10,5,1

⽅法⼀

 #include <stdio.h>
 int Fare(int stations)
 {
 if (stations <= 6)
 {
 return 2;
 }
 else if (stations <= 10)
 {
 return 3;
 }
 else
 {
 return 4;
 }
 }
 int Fund100(int paid, int fare)
 {
    int Refund = paid - fare;
    if (Refund >= 100)
    {
        return Refund / 100;
    }
    else
    {
        return 0;
    }
 }
 int Fund50(int paid, int fare, int Refunded100)
 {
    int Refund = paid - fare - Refunded100 * 100;
    if (Refund >= 50)
    {
        return Refund / 50;
    }
    else
    {
        return 0;
    }
 }
 int Fund20(int paid, int fare, int Refunded100, int Refuned50)
 {
    int Refund = paid - fare - Refunded100 * 100 - Refuned50 * 
50;
    if (Refund >= 20)
    {
        return Refund / 20;
    }
    else
    {
        return 0;
    }
 }
 int Fund10(int paid, int fare, int Refunded100, int Refuned50, 
int Refuned20)
 {
    int Refund = paid - fare - Refunded100 * 100 - Refuned50 * 
50 - Refuned20 * 20;
    if (Refund >= 10)
    {
        return Refund / 10;
    }
    else
    {
        return 0;
    }
 }
 int Fund5(int paid, int fare, int Refunded100, int Refuned50, 
int Refuned20, int Refunded10)
 {
    int Refund = paid - fare - Refunded100 * 100 - Refuned50 * 
50 - Refuned20 * 20 - Refunded10 * 10;
    if (Refund >= 5)
    {
    **      return Refund / 5;
    }
    else
    {
        return 0;
    }
 }
 int Fund1(int paid, int fare, int Refunded100, int Refuned50, 
int Refuned20, int Refund10, int Refund5)
 {
    int Refund = paid - fare - Refunded100 * 100 - Refuned50 * 
50 - Refuned20 * 20 - Refund10 * 10 - Refund5 * 5;
    if (Refund >= 1)
    {
        return Refund;
    }
    else
    {
        return 0;
    }
 }
 
int main()
 {
    int stations, paid, fare;
    int Refund100, Refund50, Refund20, Refund10, Refund5, 
Refund1;
 
    printf("请输入站数:");
    scanf("%d", &stations);
 
    fare = Fare(stations);
    printf("费用:%d元\n", fare);
 
    printf("请输入支付金额:\n");
    scanf("%d", &paid);
    Refund100 = Fund100(paid, fare);
    Refund50 = Fund50(paid, fare, Refund100);
    Refund20 = Fund20(paid, fare, Refund100, Refund50);
    Refund10 = Fund10(paid, fare, Refund100, Refund50, 
Refund20);
    Refund5 = Fund5(paid, fare, Refund100, Refund50, Refund20, 
Refund10);
    Refund1 = Fund1(paid, fare, Refund100, Refund50, Refund20, 
Refund10, Refund5);
    // 输出找零结果
    printf("找零:\n");
    printf("100元: %d张\n", Refund100);
    printf("50元: %d张\n", Refund50);
    printf("20元: %d张\n", Refund20);
    printf("10元: %d张\n", Refund10);
    printf("5元: %d张\n", Refund5);
    printf("1元: %d张\n", Refund1);
 }**

在这里插入图片描述
⽅法⼆

#include <stdio.h>
 int Fare(int stations)
 {
 }
 if (stations <= 6)
 {
 }
 return 2;
 else if (stations <= 10)
 {
  return 3;
 }
 else
 {
  return 4;
 }

 void Refund(int paid, int fare, int money[], int num, int 
refund[])
 {
  int total = paid - fare;
 int ret = total;
 for (int i = 0; i < num; i++)
 {
 refund[i] = ret / money[i];
 ret %= money[i];
 }
 }

 int main()
 {
 int stations, paid, fare;
 int money[] = {100, 50, 20, 10, 5, 1};      
int refunds[num];                           
// 面额数组
int num = sizeof(money) / sizeof(money[0]); // 面额数量
// 找零数组
 // 输入站数
printf("请输入站数: ");
 scanf("%d", &stations);
 // 计算费用
fare = Fare(stations);
 printf("费用: %d元\n", fare);
 // 输入钱数
printf("请输入支付金额: ");
 scanf("%d", &paid);
 // 计算找零
Refund(paid, fare, money, num, refunds);
 // 输出找零结果
printf("找零:\n");
 for (int i = 0; i < num; i++)
 {
 	 printf("%d元: %d张\n", money[i], refunds[i]);
 }

 return 0;
 }

在这里插入图片描述

5.出租⻋费计算程序,起步公⾥数3公⾥,⻋费8元,超出3公⾥,每公⾥1.8元,不⾜⼀公⾥,按照1公⾥计 算。 如果输⼊⼀个钱数,计算最多能坐多少公⾥,该如何实现?

#include <stdio.h>
 
// 计算最多能乘坐的公里数
double calculateMaxKilometers(double paid)
 {
    double baseFare = 8.0;            // 起步价
    double baseKilometers = 3.0;      // 起步价覆盖的公里数
    double additionalFarePerKm = 1.8; // 超出起步价后的每公里费用
 
    // 如果支付金额小于起步价,则最多能乘坐的公里数为起步价覆盖的公里if (paid <= baseFare)
    {
        return baseKilometers;
    }
 
    // 计算超出起步价后能乘坐的额外公里数
    double additionalPaid = paid - baseFare;
    double additionalKilometers = additionalPaid / 
additionalFarePerKm;
 
    // 返回总公里数
    return baseKilometers + additionalKilometers;
 }
 
int main()
 {
    double paid;
    int maxKilometers;
 
    // 输入支付金额
    printf("请输入支付金额: ");
    scanf("%lf", &paid);
 
    // 计算最多能乘坐的公里数
    maxKilometers = (int)calculateMaxKilometers(paid);
 
    // 输出结果
    printf("最多能乘坐 %d 公里\n", maxKilometers);
 
    return 0;
 }

在这里插入图片描述

6. 通过键⼊输⼊⼀个年份和⽉份,输出显⽰该⽉有多少天。(考虑润年情况) 闰年计算规则:(year % 400==0) || (year % 4 == 0 && year % 100 != 0)

#include <stdio.h>
 
int isLeapYear(int year)
 {
    // 判断是否为闰年
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 
!= 0);
 }
 
int main()
 {
    int year, month, days;
 
    // 输入年份和月份
    printf("请输入年份:");
    scanf("%d", &year);
    printf("请输入月份:");
    scanf("%d", &month);
 
    // 根据月份和闰年情况计算天数
    switch (month)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        days = 31; // 这些月份有31天
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        days = 30; // 这些月份有30天
        break;
    case 2:
        if (isLeapYear(year))
        {
            days = 29; // 闰年的二月有29天
        }
        else
               {
            days = 28; // 平年的二月有28天
        }
        break;
    default:
        printf("无效的月份输入。\n");
        return 1; // 以非零值退出表示错误
    }
 
    // 输出天数
    printf("%d年%d月有%d天。\n", year, month, days);
 
    return 0;
 }

在这里插入图片描述

7. 实现⼀个简单的计算器,要求根据输⼊的运算符实现相应的运算。(scanf()输⼊有误,在其前或者后增加 getchar())

#include <stdio.h>
 
int main()
 {
    double num1, num2, result;
    char operator;
 
    // 提示用户输入第一个数字
    printf("请输入第一个数字: ");
    scanf("%lf", &num1);
 
    // 提示用户输入运算符
    printf("请输入运算符 (+, -, *, /): ");
    scanf(" %c", &operator); // 注意%c前的空格,用于吸收任何前置空
白字符
 
    // 提示用户输入第二个数字
    printf("请输入第二个数字: ");
    scanf("%lf", &num2);
 
    // 根据运算符执行对应的运算
    switch (operator)
    {
    case '+':
        result = num1 + num2;
        break;
    case '-':
        result = num1 - num2;
         break;
    case '*':
        result = num1 * num2;
        break;
    case '/':
        // 检查除数是否为零,防止除零错误
        if (num2 != 0)
        {
            result = num1 / num2;
        }
        else
        {
            printf("错误: 除数不能为零\n");
            return 1; // 非零返回值表示程序异常终止
        }
        break;
    default:
        // 如果输入的运算符不是预期的四个之一,输出错误信息
        printf("错误: 无效的运算符\n");
        return 1; // 非零返回值表示程序异常终止
    }
 
    // 输出运算结果
    printf("运算结果: %.2lf %c %.2lf = %.2lf\n", num1, operator, 
num2, result);
 
    // 程序正常结束,返回零
    return 0;
 }

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到