欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > cs106x-lecture2(下)(Autumn 2017)

cs106x-lecture2(下)(Autumn 2017)

2025/5/4 3:21:30 来源:https://blog.csdn.net/jm999999999/article/details/145535833  浏览:    关键词:cs106x-lecture2(下)(Autumn 2017)

打卡cs106x(Autumn 2017)-lecture2

1、BMI

Write a complete C++ program with a main function to calculate 2 people's body mass index (BMI), using the following formula:

BMI = weight / height2 * 703

The BMI rating groups each person into one of the following four categories:

BMICategory
below 18.5class 1
18.5 - 24.9class 2
25.0 - 29.9class 3
30.0 and upclass 4

Match the following example output:

This program reads data for two people
and computes their body mass index (BMI).Enter Person 1's information:
height (in inches)? 70.0
weight (in pounds)? 194.25
BMI = 27.8689, class 3Enter Person 2's information:
height (in inches)? 62.5
weight (in pounds)? 130.5
BMI = 23.4858, class 2BMI difference = 4.3831

You should break down your program into several functions, each of which helps solve the overall problem.

解答:

#include <iostream>
#include "console.h"
#include "simpio.h"using namespace std;void inputHeightAndWeight(double& h, double& w);
void calcuBmi(double& h, double& w, double& b);
void calcuClass(double& b, int& c);
void bmiDiff(double& b1, double& b2);int main() {cout << "This program reads data for two people" << endl;cout << "and computes their body mass index (BMI)." << endl << endl;double h1, w1, b1, h2, w2, b2;int c1, c2;for (int i = 1; i < 3; i++) {cout << "Enter Person " << i << "'s information:" << endl;if (i == 1) {inputHeightAndWeight(h1, w1);calcuBmi(h1, w1, b1);calcuClass(b1, c1);} else {inputHeightAndWeight(h2, w2);calcuBmi(h2, w2, b2);calcuClass(b2, c2);}}bmiDiff(b1, b2);return 0;
}void inputHeightAndWeight(double& h, double& w) {h = getDouble("height (in inches)? ");w = getDouble("weight (in pounds)? ");
}void calcuBmi(double& h, double& w, double& b) {b = w / (h * h) * 703;cout << "BMI = " << b << ", ";
}void calcuClass(double& b, int& c) {if (b < 18.5) {c = 1;} else if (b < 25.0) {c = 2;} else if (b < 30.0) {c = 3;} else {c = 4;}cout << "class " << c << endl << endl;
}void bmiDiff(double& b1, double& b2) {cout << "BMI difference = ";if (b1 > b2) {cout << b1 - b2;} else{cout << b2 - b1;}
}

2、stringMysteryAB

What is the output of the following code?

解答:

marty steFOOppa

3、nameDiamond

Write a function named nameDiamond that accepts a string as a parameter and prints it in a "diamond" format as shown below. For example, the call of nameDiamond("MARTY"); should print:

M
MA
MAR
MART
MARTYARTYRTYTYY

解答:

#include <iostream>
#include <string>
#include "console.h"
#include "simpio.h"using namespace std;void nameDiamond(string s) {int len = s.length();// 上半部分for (int i = 0; i < len; i++) {cout << s.substr(0, i + 1) << endl;}// 下半部分for (int i = 1; i < len; i++) {// 空格for (int j = 0; j < i; j++) {cout << " ";}cout << s.substr(i, len - i) << endl;}
}int main() {string name = getLine("Enter your name: ");nameDiamond(name);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词