C++ Primer Plus(第6版)编程练习第二章 VS2019运行

1.编写一个C++程序,它显示您的姓名和地址。

#include<iostream>
using namespace std;

int main()
{
	cout << "Name:Trump" << endl;
	cout<<"Address:The White House is located at No.1600,";
	cout << "Pennsylvania Avenue, downtown of Washington DC" << endl;

	return 0;
}

2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1long=200yard)

#include<iostream>
using namespace std;

int main()
{
	double dis;
	cout << "Please enter a distance in long." << endl;
	cin >> dis;
	cout << dis << " long = " << 200 * dis << " yard" << endl;

	return 0;
}

3.编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出……

#include<iostream>
using namespace std;
void print1();
void print2();

int main()
{
	print1();
	print1();
	print2();
	print2();

	return 0;
}

void print1()
{
	cout << "Three blind mice" << endl;
}
void print2()
{
	cout << "See how they run" << endl;
}

4.编写一个程序:让用户输入其年龄……

#include<iostream>
using namespace std;

int main()
{
	cout << "Enter your age:";
	unsigned int year;
	cin >> year;
	cout << year << " years includes " << year * 12 << " months." << endl;

	return 0;
}

5.编写一个程序,其中main()调用一个用户定义的函数……

#include<iostream>
using namespace std;
float fah(float cel);

int main()
{
	float cel;
	cout << "Please enter a Celsius value:";
	cin >> cel;	
	cout << cel << " degrees Celsius is " << fah(cel) << " degrees Fahrenheit." << endl;

	return 0;
}
float fah(float cel)
{
	float fah;
	fah = 1.8 * cel + 32.0;

	return fah;
}

6.编写一个程序,其中main()调用一个用户定义的函数(光年转换……)

//编程练习2.5编写一个C++程序,其中……
#include<iostream>
using namespace std;
double ast(float lighty);

int main()
{
	double lighty;
	cout << "Please enter the number of light years:";
	cin >> lighty;
	cout << lighty << " light years = " << ast(lighty) << " astronomical units." << endl;

	return 0;
}
double ast(float lighty)
{
	double ast;
	ast = 63240 * lighty;

	return ast;
}

7.编写一个程序,要求用户输入小时数何分钟数……

//编程练习2.5编写一个C++程序,其中……
#include<iostream>
using namespace std;
void Time(int h, int min);

int main()
{
	int h, min;
	cout << "Enter the number of hours:";
	cin >> h;
	cout << "Enter the number of minutes:";
	cin >> min;

	Time(h, min);

	return 0;
}
void Time(int h, int min)
{
	int hour, mins;
	hour = h%24 + min / 60;
	mins = min % 60;
	cout << "Time: " << hour << ":" << mins << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值