• 用计算人体基础代谢率BMR来对比C与C++输入输出的区别


    功能需求:

    人每时每刻都在消耗能量,使用BMR(Basic Metabolic Rate)来计算人全无活动(睡一整天)时所需的热量。下面介绍人每天至少需要多少热量(附热量计算公式)?女: BMR = 65.5 + ( 9.6 × 体重kg ) + ( 1.8 × 身高cm ) - ( 4.7 × 年龄years )男: BMR = 66 + ( 13.7 × 体重kg ) + ( 5 × 身高cm ) - ( 6.8 × 年龄years )请同学们编写程序计算你每天的基础代谢率BMR。

    1.C使用scanf,printf输入输出,包含头文件stdio.h

    以下是用C语言编写的计算基础代谢率(BMR)的代码示例:

    1. #include
    2. int main() {
    3. int gender;
    4. double weight, height, age, bmr;
    5. printf("请选择性别(1代表女性,2代表男性):");
    6. scanf("%d", &gender);
    7. printf("请输入体重(kg):");
    8. scanf("%lf", &weight);
    9. printf("请输入身高(cm):");
    10. scanf("%lf", &height);
    11. printf("请输入年龄(岁):");
    12. scanf("%lf", &age);
    13. if (gender == 1) {
    14. bmr = 65.5 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    15. } else if (gender == 2) {
    16. bmr = 66 + (13.7 * weight) + (5 * height) - (6.8 * age);
    17. } else {
    18. printf("无效的性别选择。\n");
    19. return 0;
    20. }
    21. printf("您每天的基础代谢率(BMR)为:%.2lf千卡。\n", bmr);
    22. return 0;
    23. }

     2.C++使用cin,cout输入输出,包含头文件iostream

    以下是用C++编写的计算基础代谢率(BMR)的代码示例:

    1. #include
    2. using namespace std;
    3. int main() {
    4. int gender;
    5. double weight, height, age, bmr;
    6. cout << "请选择性别(1代表女性,2代表男性):";
    7. cin >> gender;
    8. cout << "请输入体重(kg):";
    9. cin >> weight;
    10. cout << "请输入身高(cm):";
    11. cin >> height;
    12. cout << "请输入年龄(岁):";
    13. cin >> age;
    14. if (gender == 1) {
    15. bmr = 65.5 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    16. } else if (gender == 2) {
    17. bmr = 66 + (13.7 * weight) + (5 * height) - (6.8 * age);
    18. } else {
    19. cout << "无效的性别选择。" << endl;
    20. return 0;
    21. }
    22. cout << "您每天的基础代谢率(BMR)为:" << bmr << "千卡。" << endl;
    23. return 0;
    24. }

    请注意,这只是一个简单的示例代码,没有进行输入验证和错误处理。在实际应用中,你可能需要添加更多的代码来确保输入的有效性和程序的健壮性。

  • 相关阅读:
    hive葵花宝典:hive函数大全
    C#面:如何避免类型转换时的异常?
    Maven安装详解
    [Spring Cloud] Hystrix三大特性--降级,熔断,隔离
    ONNX Runtime 源码阅读:Graph::SetGraphInputsOutputs() 函数
    Eureka 服务端搭建入门与集群搭建
    【简单的留言墙】HTML+CSS+JavaScript
    java设计模式-创建型模式:1-工厂方法模式
    Javascript知识【jQuery选择器】
    【JavaEE初阶】线程安全的集合类
  • 原文地址:https://blog.csdn.net/airen3339/article/details/133924560