【C++ Primer Plus 编程练习】 3.7
3.7.1
编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。
#include <iostream> using namespace std; int main() { int a ; const int yinzi (12) ; cout << "你的身高:_____英寸。\b\b\b\b\b" ; cin >> a ; cout << "你的身高:" << a / yinzi << "英尺" << a % yinzi << "英寸。" << endl ; }
3.7.2
#include <iostream> using namespace std; int main() { double foot , inch , pound , meter , kilo , bmi ; const double f2i (12.0) , i2m (0.0254) , k2p (2.2) ; cout << "Enter your height and weight:" << endl << "___feet\b\b\b\b\b\b" ; cin >> foot ; cout << "____inches\b\b\b\b\b\b\b\b\b" ; cin >> inch ; cout << "_____pounds\b\b\b\b\b\b\b\b\b\b" ; cin >> pound ; meter = i2m * ( f2i * foot + inch ) ; kilo = pound / k2p ; bmi = kilo / ( meter * meter ) ; cout << "Your BMI is: " << bmi ; }
3.7.3
#include <iostream> using namespace std; int main() { double d , m , s ; cout << "Enter a latitude in degrees, minutes, and seconds:" << endl ; cout << "First, enter the degrees: " ; cin >> d ; cout << "Next, enter the minutes of arc: " ; cin >> m ; cout << "Finally, enter the seconds of arc: " ; cin >> s ; cout << d << " degrees, " << m << " minutes, " << s << " seconds = " << d + m / 60.0 + s / 60.0 /60.0 << " degrees" ; }
3.7.4
#include <iostream> using namespace std; int main() { long s , m , h , d ; const int ms (60) , hm (60) , dh (24) ; cout << "Enter the number of seconds: " ; cin >> s ; cout << s ; m = s / ms ; s %= ms ; h = m / hm ; m %= hm ; d = h / dh ; h %= dh ; cout << " seconds = " << d << " days, " << h << " hours, " << m << " minutes, " << s << " seconds" ; }