C++ Primer Plus_编程练习_第2章 开始学习C++
2.7 编程练习 100
2.7.1
#include<iostream>
using namespace std;
int main()
{
cout << "姓名:神迹小卒\n"
<< "地址:四川成都";
return 0;
}
/*>>
姓名:神迹小卒
地址:四川成都
*/
2.7.2
#include<iostream>
using namespace std;
int main()
{
double long_distance;
cout << "请输入一个以long为单位的距离: ";
cin >> long_distance;
cout << long_distance << " long ";
cout << "等于 ";
long_distance *= 220.0;
cout << long_distance << " 码" << endl;
return 0;
}
2.7.3
#include<iostream>
using namespace std;
void print1(void);
void print2(void);
int main()
{
print1();
print1();
print2();
print2();
return 0;
}
void print1(void)
{
cout << "Three blind mice" << endl;
}
void print2(void)
{
cout << "See how they run" << endl;
}
/*>>
Three blind mice
Three blind mice
See how they run
See how they run
*/
2.7.4
#include<iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "your months are:" << age * 12;
return 0;
}
/*>>
Enter your age:29
your months are:348
*/
2.7.5
#include <iostream>
using namespace std;
double temperature(double temp);
int main()
{
double celsius;
cout << "Please enter a Celsius value: ";
cin >> celsius;
cout << celsius << " degrees Celsius is ";
cout << temperature(celsius);
cout << " degrees Fahrenheit." << endl;
return 0;
}
double temperature(double temp)
{
return 1.8 * temp + 32.0;
}
2.7.6
#include <iostream>
using namespace std;
double transform(double temp);
int main()
{
double light_years;
cout << "Enter the number of light years: ";
cin >> light_years;
cout << light_years << " light years = ";
cout << transform(light_years);
cout << " astronomical units." << endl;
return 0;
}
double transform(double temp)
{
return temp * 63240.0;
}
2.7.7
#include <iostream>
using namespace std;
void show_time(int hour, int minute);
int main()
{
int hour, minute;
cout << "Enter the number of hours: ";
cin >> hour;
cout << "Enter the number of minutes: ";
cin >> minute;
show_time(hour, minute);
return 0;
}
void show_time(int hour, int minute)
{
cout << "Time: " << hour << ":" << minute << endl;
return;
}
还没有评论,来说两句吧...