C++ Primer Plus(第6版)习题(第二章)
1..编写一个C++程序,它显示您的姓名和地址。
#include<iostream>
using namespace std; int main()
{
string name,address;
cout << "Please enter your name and address:";
cin >> name >> address;
cout << "Your name is "<< name
<< " and your address is "<< address
<< endl; return ;
}
2.编写一个C++程序,要求用户输入一个以 long 为单位的距离,然后将它转换为码 。
(1 long = 220 码)
#include<iostream>
using namespace std; int main()
{
cout << "Please enter a distance in long: ";
double long;
cin >> long;
double yard = long * ;
cout << long << " long = "<< yard << " yard."<< endl; return ;
}
3.编写一个C++程序,使用3个用户定义的函数(包括main()),并生产下面的输出:
Three blind mice
Three blind mice
See how they run
其中一个函数要调用两次,生产前两行;另一个函数也被调用两次,生产其余输出。
#include<iostream>
void str_1();
void str_2(void);
using namespace std; int main()
{
str_1();
str_1();
str_2();
str_2(); return ;
} void str_1()
{
cout << "Three blind mice."<< endl;
}
void str_2(void)
{
cout << "See how they run."<< endl;
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下:
Enter you age:29
#include<iostream>
using namespace std; int main()
{
cout << "Please enter your age: ";
int age;
cin >> age;
int contains = age * ;
cout << "Your age is "<< age
<< " You've been through "<< contains
<< " months."<< endl; return ;
}
5.编写程序,其中的main()调用一个用户定义的函数(以摄氏温度为参考,并返回相应的华氏温度)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value:20
20 degress Celsius is 68 degress Fahenheit.
转换公式:华氏温度 = 1.8X摄氏温度 + 32.
#include<iostream>
double fah(double);
using namespace std; int main()
{
cout << "Please enter a Celsius value:";
double cel;
cin >> cel;
fah(cel);
cout << cel << " degrees Celsius is "
<< fah(cel) << " degrees Fahrenheit."
<< endl; return ;
} double fah(double n)
{
double fah = n * 1.8 + 32.0;
return fah;
}
6.编写程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years:4.2
4.2 light years = 265608 astronmical units.
转换公式:1 光年 = 63240 天文单位
#include<iostream>
double astronomical(double);
using namespace std; int main()
{
cout << "Enter the number of light years:";
double light_year;
cin >> light_year;
astronomical(light_year);
cout << light_year << " light years = "
<< astronomical(light_year)
<< " astronomical units."
<< endl; return ;
} double astronomical(double n)
{
double ast = n * ;
return ast;
}
7.编写程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个 void函数,后者以下面格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28
Time:9:28
#include<iostream>
void time(double,double);
using namespace std; int main()
{
cout << "Enter the number of hours: ";
double hour;
cin >> hour;
cout << "Enter the number of minutes: ";
double minute;
cin >> minute; time(hour,minute);
return ;
}
void time(double h,double m)
{
cout << "Time: "<< h <<":"<< m << endl;
}
最新文章
- css3径向渐变详解-遁地龙卷风
- 初识makefile
- Eclipse自动补全+常用快捷键
- mark元素
- 在Seismic.NET下用最少的语句写出一个剖面显示程序
- oracle SGA详解
- 通过HtmlEmail 发送邮件
- JPA2 关于 PagingAndSortingRepository
- HDU 3920 Clear All of Them I(DP + 状态压缩 + 贪心)
- Template 模式
- python自动化Traceback (most recent call last):报错
- di
- git 回退各种场景操作
- 阿里云 windows 2008 使用Thinkphp5 captcha验证码不显示问题
- dot.js模板实现分离式
- 《剑指offer》 合并两个排序的链表
- loadrunner笔记(一):下载、安装loadrunner和负载压力测试概念说明
- simhash文章排重
- Unity 3D Shader流水线
- No.1_NABCD模型分析
热门文章
- 【深入学习linux】在linux系统下怎么编写c语言程序并运行
- vmware ubuntu16 启动蓝屏屏幕闪
- 请问在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句?
- Ubuntu宝塔面板设置网站 Apache Server API为Apache 2.0 Handler模式
- row_number() over()分组排序功能 partition by 用于给结果集分组
- PRINT_INITA整体偏移值传变量
- notepad++之个性化配置
- dockerfile的常用基础镜像——java镜像
- POWER 740硬盘更换步骤
- CentOS下安装Tomcat