C++ Primer Plus 第2章 编程练习

本文提供了多个C++程序实例,包括字符串输出、单位转换、输入处理等实用功能,旨在帮助初学者理解C++基本语法和函数调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

#include <iostream>
int main()
{
	using namespace std;
	cout << "betaojia" << endl;
	cout << "beijing" << endl;
	cin.get();
	return 0;
}

2.

#include <iostream>
double fur2feet(double);
int main()
{
	using namespace std;
	double fur;
	cin >> fur;
	double feet = fur2feet(fur);
	cout << feet << endl;
	cin.get();
	cin.get();
	return 0;
}
double fur2feet(double t)
{
	double f = t*220.0;
	return f;
}

3.

#include <iostream>
void first();
void second();
int main()
{
	first();
	first();
	second();
	second();
	return 0;
}
void first()
{
	using namespace std;
	cout << "Three blind mice" << endl;
}
void second()
{
	using namespace std;
	cout << "See how they run" << endl;
}

4.

#include <iostream>
int year2month(int);
int main()
{
	using namespace std;
	cout << "Enter your age: ";
	int age;
	cin >> age;
	//cout << endl;
	int month = year2month(age);
	cout << month;
	cin.get();
	cin.get();
	return 0;
}
int year2month(int y)
{
	return y*12;
}
5.

#include <iostream>
double C2F(double);
int main()
{
	using namespace std;
	cout << "Please enter a Celsius value: ";
	double c;
	cin >> c;
	double f = C2F(c);
	cout << c << " degree Celsius is " << f << " degree Fahrenheit.";
	cin.get();
	cin.get();
	return 0;
}
double C2F(double a)
{
	return 1.8 * a + 32.0;
}

6.

#include <iostream>
double lightyear2astronomicalunit(double);
int main()
{
	using namespace std;
	cout << "Enter the number of light years: ";
	double l;
	cin >> l;
	double a = lightyear2astronomicalunit(l);
	cout << l << " light years = " << a << " astromical units.";
	cin.get();
	cin.get();
	return 0;
}
double lightyear2astronomicalunit(double a)
{
	return 63240.0 * a;
}

7.

#include <iostream>
using namespace std;
int inputhour();
int inputmin();
int main()
{
	int hour = inputhour();
	int min = inputmin();
	cout << "Time: " << hour << ":" << min;
	cin.get();
	cin.get();
	return 0;
}
int inputhour()
{
	cout << "Enter the number of hours: ";
	int h;
	cin >> h;
	return h;
}
int inputmin()
{
	cout << "Enter the number of minutes: ";
	int m;
	cin >> m;
	return m;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值