C++ Primer Plus 习题答案 第二章

//2.7.1 
# include <iostream>
int main()
{
	using namespace std;
	cout << "Xingming"<<endl;
	cout <<"address"<<endl;
	return 0;
 } 

//2.7.2
# include <iostream>
int main()
{
	using namespace std;
	
	int ilong;
	int ma;
	cout<<"enter the length in ilong:";
	cin>>ilong;
	ma=ilong*220;
	cout<<ma<<"ma"<<endl;
	return 0;
}

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


//2.7.4
# include <iostream>
using namespace std;
int mouth_year(int n);
int main()
{
	cout<<"Enter your age:";
	int age;
	cin>>age;
	int mouth;
	mouth=mouth_year(age);
	cout<<"There are "<<mouth<<" mouths."<<endl;
	return 0;
}
int mouth_year(int n)
{
	return 12*n;
}


//2.7.5
# include <iostream>
using namespace std;
int change(int n);
int main()
{
	cout<<"Please enter a Celsius value :";
	int Celsius;
	cin>>Celsius;
	int Fahrenheit;
	Fahrenheit=change(Celsius);
	cout <<Celsius<<" degree Celsius is "<<Fahrenheit<<" degree Fahrenheit"<<endl;
	return 0;
}
int change(int n)
{
	return 1.8*n+32;
}

//2.7.6
# include <iostream>
using namespace std;
double change(double n);
int main()
{
	cout <<"Enter the number of light years:";
	double light_year;
	cin>>light_year;
	double unit;
	unit=change(light_year);
	cout<<light_year<<" light_year = "<< unit <<" astronomical units"<<endl;
	return 0;
	
}
double change(double n)
{
	return 63240*n;
}


//2.7.7
# include <iostream>
using namespace std;
int main()
{
	cout << "Enter the number of hours:";
	int hour;
	cin>>hour;
	cout << "Enter the number of minutes:";
	int minutes;
	cin>>minutes;
	cout<<"Time "<<hour <<" : "<< minutes<<endl;
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/weixin_40473591/article/details/81104500