C++Primer Plus 第六版 第二章编程练习--代码答案

1.

#include<iostream>
using namespace std;

int main() {
	string s1, s2;
	cout << "enter you name: ";
	cin >> s1;
	cout << " Enter your address: ";
	cin >> s2;
	cout << s1 << " live in " << s2 << endl;
	return 0;
}

2.

#include<iostream>
using namespace std;

int transform(int);

int main() {
	int longMetre;
	cout << "Enter the longMetre: ";
	cin >> longMetre;
	int size = transform(longMetre);
	cout << longMetre << " long= " << size << " size." << endl;
	return 0;
}

int transform(int n) {
	return 220 * n;
}

3.

#include<iostream>
using namespace std;

void blind() {
	cout << "three blind mice" << endl;
}
void run() {
	cout << "See how they run" << endl;
}
int main() {
	blind();
	blind();
	run();
	run();
	return 0;
}

4.

#include<iostream>
using namespace std;

int main() {
	cout << "Enter your age: ";
	int age;
	cin >> age;
	cout << "you have pass " << 12 * age << " mouths" << endl;
	return 0;
}

5.

#include<iostream>
using namespace std;
 
double transform(double Celsius) {
    return 1.8 * Celsius + 32.0;
};
int main() {
    double Celsius;
    cout << "Enter a Celsius value: ";
    cin >> Celsius;
    cout << Celsius << " degrees Celsius is " << transform(Celsius) << " degrees Fahrenheit." << endl;
    return 0;
}

6.

#include<iostream>
using namespace std;

double convert(double lightYear);

int main() {
	double light_year;
	cout << "Enter the number of light years: ";
	cin >> light_year;
	cout << light_year << " light years = " << convert(light_year) << " astronomical inots." << endl;
	return 0;
}

double convert(double lightYear) {
	return 63240 * lightYear;
}

7.

#include<iostream>
using namespace std;

void showTime(int hour, int minutes);

int main() {
	int x, y;
	cout << "Enter the number of hours: ";
	cin >> x;
	cout << "Enter the munber of minutes: ";
	cin >> y;
	showTime(x, y);
	return 0;
}

void showTime(int hours, int minutes) {
	cout << "Time: " << hours << " : " << minutes << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值