第2章 开始学习 C++
//ex2.1--display your name and address
#include <iostream>
using namespace std;
int main(void)
{
cout<<"Name:"<<"Du Yong!\n";
cout<<"Address:"<<"甘肃定西!\n";
return 0;
}
//ex2.2--convert the furlong units to yard units
#include <iostream>
using namespace std;
double fur2yd(double);
int main()
{
cout<<"enter the distance measured by furlong units:";
double fur;
cin>>fur;
cout<<"convert the furlong to yard"<<endl;
double yd;
yd=fur2yd(fur);
cout<<fur<<" furlong is "<<yd<<" yard"<<endl;
return 0;
}
double fur2yd(double t)
{
return 220*t;
}
//2.3--每个函数被调用两次
#include <iostream>
using namespace std;
void mice()
{
cout<<"Three blind mice\n";
}
void run()
{
cout<<"See how they run\n";
}
int main()
{
mice();
mice();
run();
run();
return 0;
}
//2.4--输入年龄,计算包含多少个月
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int Month(int m)
{
return m*12;
}
int main()
{
int age;
cout<<"Enter your age:";
cin>>age;
cout<<"Your age include "<<Month(age)<<" months!"<<endl;
return 0;
}
//2.5--convert the Celsiue value to Fahrenheit value.
#include <iostream>
using namespace std;
double Convert(double m)
{
double Fahrenheit;
Fahrenheit=m*1.8+32.0;
return Fahrenheit;
}
int main(void)
{
double Celsius;
cout<<"Please enter a Celsius value:";
cin>>Celsius;
cout<<Celsius<<" degrees Celsius is "<<Convert(Celsius)<<" degrees Fahrenheit.\n";
return 0;
}
//2.6--自定义函数实现光年转换为天文单位
#include <iostream>
using namespace std;
double Convert(double years)
{
return years*63240;
}
int main(void)
{
double LightYears;
cout<<"Enter the number of light years:";
cin>>LightYears;
cout<<LightYears<<" light years = "<<Convert(LightYears)<<" astronomical units.\n";
return 0;
}
//2.7--输入小时和分钟,输出时间
#include <iostream>
using namespace std;
void Show(int hour, int minute)
{
cout<<"Time: "<<hour<<":"<<minute<<endl;
}
int main(void)
{
int hour,minute;
cout<<"Enter the number of hours:";
cin>>hour;
cout<<"Enter the number of minutes:";
cin>>minute;
Show(hour,minute);
return 0;
}