第二章 编程练习
1. 编写一个C++程序,它显示您的姓名和地址
1.1 基础版
#include <iostream>
using namespace std;
int main()
{
cout << "姓名:小郭\n" << "地址:出门右拐右拐右拐再右拐";
return 0;
}
1.2 请求输入版
#include <iostream>
using namespace std;
int main()
{
char name[100]; //声明字符串数组
char address[100]; //声明字符串数组
cout << "请输入您的姓名:";
cin >> name;
cout << "请输入您的地址:";
cin >> address;
cout << "姓名:" << name << endl;
cout << "地址:" << address << endl;
return 0;
}
//运行后输出如下
请输入您的姓名:小郭 //手动输入
请输入您的地址:出门右拐右拐右拐再右拐 //手动输入
姓名:小郭
地址:出门右拐右拐右拐再右拐
//但是不接受空格
请输入您的姓名:Jeff Guo //手动输入
请输入您的地址:姓名:Jeff //手动输入
地址:Guo
1.3 请求输入,允许带空格版
#include <iostream>
using namespace std;
int main()
{
char name[100];
char address[100];
cout << "请输入您的姓名:";
cin.get(name, 100); //使用cin.get()读取一行
cin.get(); //使用cin.get()读取换行符
cout << "请输入您的地址:";
cin.get(address, 100);
cin.get();
cout << "姓名:" << name << endl;
cout << "地址:" << address << endl;
return 0;
}
//运行后输出如下
请输入您的姓名:Jeff Guo //手动输入
请输入您的地址:go out and turn right 3 times //手动输入
姓名:Jeff Guo
地址:go out and turn right 3 times
2. 编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转化为码(一long为220码)
2.1 基础版
#include <iostream>
using namespace std;
int main()
{
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 main()
{
int length_long;
int length_yd;
cout << "请输入long为单位的距离:";
cin >> length_long;
length_yd = long_to_yd(length_long);
cout << length_long << "long是" << length_yd << "码" << endl;
return 0;
}
int long_to_yd(int length_long)
{
int length_yd;
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\n";
}
3.2 for循环版
#include <iostream>
using namespace std;
void show1(); //函数原型
void show2(); //函数原型
int main()
{
for (int i=0; i<2; i++)
show1();
for (int i=0; i<2; i++)
show2();
return 0;
}
void show1()
{
cout << "Three blind mice" << endl;
}
void show2()
{
cout << "See how they run\n";
}
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 << year << " year is " << month << " month" << endl;
return 0;
}
4.2 函数+while版
#include <iostream>
using namespace std;
int year_to_month(int year);
int main()
{
int year;
int month;
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";
return 0;
}
int year_to_month(int year)
{
int month;
const int factor = 12;
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;
float celsius_to_fahrenheit(float celsius);
int main()
{
float celsius;
float fahrenheit;
cout << "Please enter a Celsius value: ";
cin >> celsius;
cout << celsius << " degrees celsius is " << celsius_to_fahrenheit(celsius) << " degrees fahrenheit" << endl;
return 0;
}
float celsius_to_fahrenheit(float celsius)
{
float fahrenheit;
fahrenheit = celsius * 1.8 + 32.0;
return fahrenheit;
}
5.2 函数+while版
#include <iostream>
using namespace std;
float celsius_to_fahrenheit(float celsius);
int main()
{
float celsius;
float fahrenheit;
cout << "Please enter a Celsius value: ";
while (cin >> celsius && celsius!='q')
{
cout << celsius << " degrees celsius is " << celsius_to_fahrenheit(celsius) << " degrees fahrenheit" << endl;
cout << "Please enter a Celsius value: ";
}
cout << "end";
return 0;
}
float celsius_to_fahrenheit(float celsius)
{
float fahrenheit;
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_years_to_astronomical_units(double light_years);
int main()
{
double light_years;
cout << "nter the number of light years: ";
cin >> light_years;
double astronomical_units = light_years_to_astronomical_units(light_years); //初始化
cout << light_years << " light years = " << astronomical_units << " astronomical units." << endl;
return 0;
}
double light_years_to_astronomical_units(double light_years)
{
double astronomical_units;
astronomical_units = light_years * 63240;
return astronomical_units;
}
7. 编写一个程序,要求用户输入小时数和分钟数吗,在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
7.1 基础版
#include <iostream>
using namespace std;
int main()
{
int hour;
int minute;
cout << "Enter the number of hours: ";
cin >> hour;
cout << "Enter the number of minutes: ";
cin >> minute;
cout << "Time: " << hour << ":" << minute;
return 0;
}