C ++ Primer Plus program of the second chapter exercises

2.1 displays the name and address

#include<iostream>
using namespace std;
int main(){
 cout<<"姓名:小明 地址:中国"<<endl; 
 return 0;
} 

2.2 to 220 yards long conversion units of

#include<iostream>
using namespace std;
int convert(int llong)
{
 return llong*220;
} 
int main()
{
 int llong;
 cin>>llong;
 cout<<"以long为单位进行转换\n"<<"有"<<llong<<"个long,"<<"有"<<convert(llong)<<"码"<<endl;
 return 0;
}

2.3 3 Custom Function

// 2.3 使用3个自定义函数
#include<iostream>
using namespace std;
void fun1()
{
	cout<<"Three blind mice"<<endl;
	return ;
} 

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

int main()
{
	fun1();
	fun1();
	fun2();
	fun2();
	return 0;
}

Enter the age of 2.4 months include display

//2.4 输入年龄显示包含几个月

#include<iostream>
using namespace std;

int main()
{
	int year;
	cin>>year;
	cout<<"年龄是:"<<year<<"岁,一共有"<<year*12<<"月"; 
	return 0;
} 

2.5 Celsius to Fahrenheit temperature values ​​converted value

// 2.5 摄氏温度值转换为华氏温度值

#include<iostream>
using namespace std;

float convert(float she){
	return 1.8*she+32.0;
}
int main()
{
	float she;
	cout<<"输入摄氏温度值:";
	cin>>she;
	cout<<"转换后的华氏温度:"<<convert(she)<<endl; 
	return 0;
} 

2.6 light-year astronomical unit conversion

//2.6 光年 天文单位转换
//错误提醒:不要顺手就给变量定float和int型要充分考虑所需的数据类型 
#include <iostream>
using namespace std;

double convert(double light)
{
	return light*63240;
} 
int main() 
{
	double light;
	cout<<"输入光年:";
	cin>>light;
	cout<<"天文单位"<<convert(light)<<endl;
	return 0;
	 
}

2.7 shows the hours and minutes

// 2.7 显示小时和分钟数

#include<iostream>
using namespace std;

void display(double hour,double minu)
{
	cout<<"Time:"<<hour<<":"<<minu;
}
int main()
{
	double hour,minu;
	cout<<"Enter the number of hours:";
	cin>>hour;
	cout<<"Enter the number of minutes:";
	cin>>minu;
	display(hour,minu);
	return 0;
} 

to sum up

  1. Error Reminder: Do not easily give variable and fixed float int type, we must fully consider the required data types, such as is sometimes required double rather than int, or float.

  2. The second chapter is only a rough look, because it feels like to know are some of the things that did not seriously look at, do not know will not hit the face, do not know is not behind will be very important and requires special attention.

  3. Know why the # include, cin cout included, know why use using namespace std; this namespace. (In fact, understanding is not very deep)

  4. Good thick book! Continue to learn!

Released nine original articles · won praise 0 · Views 1804

Guess you like

Origin blog.csdn.net/weixin_46021869/article/details/104027147