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;
}

C++ Primer Plus(第6版)习题(第二章)的更多相关文章

  1. 《C++Primer》第五版习题详细答案--目录

    作者:cosefy ps: 答案是个人学习过程的记录,仅作参考. <C++Primer>第五版习题答案目录 第一章:引用 第二章:变量和基本类型 第三章:字符串,向量和数组 第四章:表达式

  2. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

  3. 《C++Primer》第五版习题解答--第四章【学习笔记】

    [C++Primer]第五版习题解答--第四章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/11 第四章:表达式 练习4. ...

  4. 《C++Primer》第五版习题答案--第六章【学习笔记】

    <C++Primer>第五版习题答案--第六章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/16 第六章:函数 ...

  5. 《C++ Primer》 第四版 第7章 函数

    <C++ Primer> 第四版 第7章 函数 思维导图笔记 超级具体.很具体,图片版,有利于复习查看 http://download.csdn.net/detail/onlyshi/94 ...

  6. 《C++Primer》第五版习题答案--第二章【学习笔记】

    C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...

  7. 《C++Primer》第五版习题答案--第三章【学习笔记】

    [C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...

  8. C语言程序设计:现代方法(第2版)第二章全部习题答案

    前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解 ...

  9. 《C++Primer》第五版习题答案--第一章【学习笔记】

    C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...

随机推荐

  1. A Full Hardware Guide to Deep Learning深度学习电脑配置

     https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149( 欢迎关注博 ...

  2. 【Python】解析Python中类的使用

    目录结构: contents structure [-] 类的基本使用 专有方法 继承 单重继承 多重继承 砖石继承 1.类的基本使用 下面是类使用的一个简单案例, class person: &qu ...

  3. git冲突处理-Please move or remove them before you can merge

    参考:https://www.cnblogs.com/wenlj/p/5866356.html https://my.oschina.net/lixiaoyan/blog/1821947 #### 将 ...

  4. C#实体类null自动转空字符串

    C#实体类null自动转空字符串 using System.ComponentModel.DataAnnotations; [DisplayFormat(ConvertEmptyStringToNul ...

  5. CentOS6非root用户下安装及配置CDH5.3.0

    #install lsb packagesudo yum install -y redhat-lsb #install net-tools package sudo yum install -y ne ...

  6. MySQL 使用 ON UPDATE CURRENT_TIMESTAMP 自动更新 timestamp (转)

    原文地址: https://blog.csdn.net/heatdeath/article/details/79833492 `create_time` timestamp not null defa ...

  7. Manytasking optimization MATP

    Manytasking Jmetal代码反向解析1_MATP测试函数集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 这是我在写Manytask optimization时的笔记,代码地址可 ...

  8. bootstrap datetimepicker 添加清空按钮

    <div class="ys-datetimepicker"> <input class="form-control" size=" ...

  9. Postgres-XL集群ERROR :Failed to get pooled connections原因说明

    集群说明 6台服务器.其中1台(rt67-1)运行GTM,其余5台均运行1个GTM_PROXY.1个Coordinator node.3个Data node.每个服务器连接到3组网络中,每个Data ...

  10. Python代码约定

    建议遵守以下约定: 使用 4 个空格来缩进 永远不要混用空格和制表符 在函数之间空一行 在类之间空两行 字典,列表,元组以及参数列表中,在 , 后添加一个空格.对于字典,: 后面也添加一个空格 在赋值 ...