//C++ primer plus(第六版)第二章 复习题
2.7 编程练习
1. 编写一个C++程序,它显示您的姓名和地址
1.1 基础版
#include <iostream>
int main()
{
using namespace std;
cout << "name: Lychee;\naddress: Suzhou garden." << endl;
}
1.2 请求输入版
#include <iostream>
int main()
{
using namespace std;
char name[100]; //声明字符串数组
char address[100];
cout << "请输入您的姓名: ";
cin >> name;
cout << "请输入您的地址: ";
cin >> address;
cout << "姓名:" << name << "\n地址:" << address << endl;
return 0;
}
1.3 请求输入+允许带空格
#include <iostream>
int main()
{
using namespace std;
char name[100];
char address[100];
cout << "请输入您的姓名:";
cin.get(name, 100);
cin.get();
cout << "请输入您的地址:";
cin.get(address, 100);
cin.get();
cout << "姓名:" << name << endl;
cout << "地址:" << address << endl;
return 0;
}
2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转化为码(一long为220码)
2.1 基础版
#include <iostream>
int main()
{
using namespace std;
int length_long;
int length_yd;
const int factor_long_to_yd = 220;
cout << "请输入一个以long为单位的距离:";
cin >> length_long;
length_yd = length_long * factor_long_to_yd;
cout << length_long << " long = " << length_yd << " 码" << endl;
return 0;
}
2.2 函数版
#include <iostream>
using namespace std;
int long_to_yd(int); //函数原型
int length_long;
int length_yd;
int main()
{
cout << "请输入一个以long为单位的距离:";
cin >> length_long;
length_yd = long_to_yd(length_long);
cout << length_long << " long = " << length_yd << " 码";
return 0;
}
int long_to_yd(int length_long)
{
const int factor_long_to_yd = 220;
length_yd = length_long * factor_long_to_yd;
return length_yd;
}
3.编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行; 另一个函数也被调用两次,生成其余的输出。
3.1 基础版
#include <iostream>
using namespace std;
void show1();
void show2();
int main()
{
show1();
show1();
show2();
show2();
return 0;
}
void show1()
{
cout << "Three blind mice" << endl;
}
void show2()
{
cout << "See how they run" << endl;
}
3.2 for循环版
#include <iostream>
using namespace std;
void show1();
void show2();
int main()
{
for (int i = 1; i < 3; i++) {
show1();
}
for (int i = 1; i < 3; i++) {
show2();
}
return 0;
}
void show1()
{
cout << "Three blind mice" << endl;
}
void show2()
{
cout << "See how they run" << endl;
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age: 29
4.1 基础版
#include <iostream>
using namespace std;
int main()
{
int year;
int month;
const int factor = 12;
cout << "Enter your age:";
cin >> year;
month = year * factor;
cout << "Your age in months is:" << month << " month" << endl;
return 0;
}
4.2 函数+while版
#include <iostream>
using namespace std;
int year_to_month(int); //函数原型
int main()
{
int year;
cout << "Enter your age:";
while (cin >> year && year != 'q') {
cout << year << " year is " << year_to_month(year) << " month" << endl;
cout << "Enter your age:"; //达成循环
}
cout << "end"; //输入'q'时输出end
return 0;
}
int year_to_month(int year)
{
const int factor = 12;
int month = year * factor;
return month;
}
5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
5.1 基础版
#include <iostream>
using namespace std;
int main()
{
float celsius;
float fahrenheit;
const float factor1 = 1.8;
const float factor2 = 32.0;
cout << "Please enter a Celsius value:";
cin >> celsius;
fahrenheit = celsius * factor1 + factor2;
cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit" << endl;
return 0;
}
5.2 函数版
#include <iostream>
using namespace std;
float cel_to_fah(float); //函数原型
float celsius;
float fahrenheit;
int main()
{
cout << "Please enter a Celsius value:";
cin >> celsius;
//函数调用
cout << celsius << " degrees Celsius is " << cel_to_fah(celsius) << " degrees Fahrenheit" << endl;
return 0;
}
float cel_to_fah(float celsius) //函数定义
{
fahrenheit = celsius * 1.8 + 32.0;
return fahrenheit;
}
5.3 函数+while版
#include <iostream>
using namespace std;
float cel_to_fah(float); //函数原型
float celsius;
float fahrenheit;
int main()
{
cout << "Please enter a Celsius value:";
while (cin >> celsius && celsius != 'q')
{
//函数调用
cout << celsius << " degrees Celsius is " << cel_to_fah(celsius) << " degrees Fahrenheit" << endl;
cout << "Please enter a Celsius value:";
}
cout << "end";
return 0;
}
float cel_to_fah(float celsius) //函数定义
{
fahrenheit = celsius * 1.8 + 32.0;
return fahrenheit;
}
6.编写一个程序,其main()调用一个用户定义的界面(以光年为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并返回结果:(1光年= 63240 天文单位)
Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.
6.1 基础版
#include <iostream>
using namespace std;
double light_year;
double astronomical_units;
int main()
{
cout << "Enter the number of the light years:";
cin >> light_year;
const int factor = 63240;
astronomical_units = factor * light_year;
cout << light_year << " light year = " << astronomical_units << " astronomical units.";
return 0;
}
6.2 函数版
#include <iostream>
using namespace std;
double light_to_astronomical(double);
double light_year;
double astronomical_units;
int main()
{
cout << "Enter the number of the light years:";
cin >> light_year;
astronomical_units = light_to_astronomical(light_year);
cout << light_year << " light year = " << astronomical_units << " astronomical units.";
return 0;
}
double light_to_astronomical(double light_year)
{
const int factor = 63240;
astronomical_units = factor * light_year;
return astronomical_units;
}
6.3 函数+while版
#include <iostream>
using namespace std;
double light_to_astronomical(double);
double light_year;
double astronomical_units;
int main()
{
cout << "Enter the number of the light years:";
while (cin >> light_year && light_year != 'q') {
astronomical_units = light_to_astronomical(light_year);
cout << light_year << " light year = " << astronomical_units << " astronomical units." << endl;
cout << endl;
cout << "Enter the number of the light years:";
}
cout << "end";
return 0;
}
double light_to_astronomical(double light_year)
{
const int factor = 63240;
astronomical_units = factor * light_year;
return astronomical_units;
}
7。编写一个程序,要求用户输入小时数和分钟数吗,在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include <iostream>
using namespace std;
void time(void);
int hours;
int minutes;
int main()
{
time();
return 0;
}
void time()
{
cout << "Please the number of hours:";
cin >> hours;
cout << "Please the number of minutes:";
cin >> minutes;
cout << "Time:" << hours << ":" << minutes;
}
以上内容是参考其他博主发布的答案进行的个人练习
参考链接:https://blog.csdn.net/weixin_42411044/article/details/85106794