您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus 第2章 编程练习

2015-07-11 20:55 519 查看
1.

#include <iostream>
int main()
{
using namespace std;
cout << "betaojia" << endl;
cout << "beijing" << endl;
cin.get();
return 0;
}

2.
#include <iostream>
double fur2feet(double);
int main()
{
using namespace std;
double fur;
cin >> fur;
double feet = fur2feet(fur);
cout << feet << endl;
cin.get();
cin.get();
return 0;
}
double fur2feet(double t)
{
double f = t*220.0;
return f;
}

3.
#include <iostream>
void first();
void second();
int main()
{
first();
first();
second();
second();
return 0;
}
void first()
{
using namespace std;
cout << "Three blind mice" << endl;
}
void second()
{
using namespace std;
cout << "See how they run" << endl;
}

4.
#include <iostream>
int year2month(int);
int main()
{
using namespace std;
cout << "Enter your age: ";
int age;
cin >> age;
//cout << endl;
int month = year2month(age);
cout << month;
cin.get();
cin.get();
return 0;
}
int year2month(int y)
{
return y*12;
}5.
#include <iostream>
double C2F(double);
int main()
{
using namespace std;
cout << "Please enter a Celsius value: ";
double c;
cin >> c;
double f = C2F(c);
cout << c << " degree Celsius is " << f << " degree Fahrenheit.";
cin.get();
cin.get();
return 0;
}
double C2F(double a)
{
return 1.8 * a + 32.0;
}

6.
#include <iostream>
double lightyear2astronomicalunit(double);
int main()
{
using namespace std;
cout << "Enter the number of light years: ";
double l;
cin >> l;
double a = lightyear2astronomicalunit(l);
cout << l << " light years = " << a << " astromical units.";
cin.get();
cin.get();
return 0;
}
double lightyear2astronomicalunit(double a)
{
return 63240.0 * a;
}

7.
#include <iostream>
using namespace std;
int inputhour();
int inputmin();
int main()
{
int hour = inputhour();
int min = inputmin();
cout << "Time: " << hour << ":" << min;
cin.get();
cin.get();
return 0;
}
int inputhour()
{
cout << "Enter the number of hours: ";
int h;
cin >> h;
return h;
}
int inputmin()
{
cout << "Enter the number of minutes: ";
int m;
cin >> m;
return m;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: