C++ Primer Plus(第六版) 第二章 编程练习

编程环境:Visual Studio 2017

1:

//practice 1
#include<iostream>
using namespace std;

int main()
{
	cout << "Melody" << endl;
	cout << "Chengdu, China" << endl;
	//cin.get();
	return 0;
}

2: 

//practice 2
#include<iostream>
using namespace std;

int main()
{
	int ilong = 0;
	int iyard = 0;
	cout << "Please input a distance in long" << endl;
	cin >> ilong;
	iyard = 220 * ilong;
	cout << "The distance in yards is " << iyard << endl;
	//cin.get();
	//cin.get();
	return 0;
}

3: 

//practice 3
#include<iostream>
using namespace std;

void fun1()
{
	cout << "Three blind mice" << endl;
}

void fun2()
{
	cout << "See how they run" << endl;
}

int main()
{
	fun1();
	fun1();
	fun2();
	fun2();
	//cin.get();
	return 0;
}

4: 

//practice 4
#include<iostream>
using namespace std;

int main()
{
	int iyear = 0;
	int imonth = 0;
	cout << "Enter your age:";
	cin >> iyear;
	imonth = iyear * 12;
	cout << iyear << " years contain " << imonth << " months" << endl;
	//cin.get();
	//cin.get();
	return 0;
}

5: 

//practice 5
#include<iostream>
using namespace std;

double fun(double celsius)
{
	return (1.8*celsius + 32.0);
}
int main()
{
	double celsius = 0.0;
	double fahrenheit = 0.0;
	cout << "Please enter a Celsius value:";
	cin >> celsius;
	fahrenheit = fun(celsius);
	cout << celsius << " degress Celsius is " << fahrenheit << " degress Fahrenheit." << endl;
	//cin.get();
	//cin.get();
	return 0;
}

6: 

//practice 6
#include<iostream>
using namespace std;

double fun(double LightYears)
{
	return (63240 * LightYears);
}
int main()
{
	double LightYears = 0.0;
	double AstronomicalUnit = 0.0;
	cout << "Enter the number of light years:";
	cin >> LightYears;
	AstronomicalUnit = fun(LightYears);
	cout << LightYears << " light years = " << AstronomicalUnit << " astronomical units." << endl;
	cin.get();
	cin.get();
	return 0;
}

7:

//practice 7
#include<iostream>
using namespace std;

void ShowTime(int hour, int minute)
{
	cout << "Time:" << hour << ":" << minute << endl;
}

int main()
{
	int hour = 0;
	int minute = 0;
	cout << "Enter the number of hours:";
	cin >> hour;
	cout << "Enter the number of minutes:";
	cin >> minute;
	ShowTime(hour, minute);
	//cin.get();
	//cin.get();
	return 0;
}

才疏学浅,如有错误或者改进的建议还请大牛多多指出。

继续学习,慢慢更新。

猜你喜欢

转载自blog.csdn.net/qq_42212893/article/details/82263602