一、程序清单2.1(代码和书略不一样)

  1. #include<iostream>
  2. using namespace std;//使用std这个命名空间,才能正确找到cin和cout,如果不使用命名空间,那么使用时需要 std::cout<<"hello"<<std::endl;
  3. void main()
  4. {
  5. cout<<"hello world"<<endl;//endl表示重起一行,信息从“hello”流向cout
  6. cout<<"hey man!"<<endl;
  7. //getchar();
  8. cin.get();//按下enter键结束
  9. }

二、程序清单2.3

  1. #include<iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. int carrots;
  6. cout<<"你有多少胡萝卜?"<<endl;
  7. cin>>carrots;//输入,信息从cin流向carrots
  8. cout<<"这里还有两个。";
  9. carrots=carrots+;
  10. cout<<"现在你有"<<carrots<<"个胡萝卜。"<<endl;
  11. cin.get();//按下enter键结束
  12. cin.get();// 两条cin.get() 语句可以替换成一条语句 system("pause");
  13. }

注意:只有一条  cin.get()   语句的话,在屏幕输入数字后就会一闪而过,只有两条  cin.get() 才能在屏幕看到输出

三、程序清单2.4(调用函数)

  1. #include<iostream>
  2. #include<cmath>//or math.h
  3. using namespace std;
  4. void main()
  5. {
  6. double area,side;
  7. cout<<"你家房子多少平米:"<<endl;
  8. cin>>area;
  9. side=sqrt(area);//开平方根
  10. cout<<"这相当于"<<side<<"米的正方形"<<endl;
  11. system("pause");//暂停,使屏幕处于等待状态
  12. }

四、程序清单2.5(用户自定义无返回函数)

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void simon(int n);//函数声明,simon函数在main函数之后,若不提前声明则无法在main函数中使用
  5.  
  6. void main()
  7. {
  8. int n;
  9. simon();
  10. cout<<"请输入一个正整数:";
  11. cin>>n;
  12. simon(n);//此处的n为实际参数(简称“实参”)
  13. system("pause");
  14. }
  15.  
  16. void simon(int n){//此处的n为形式参数(简称“形参”)
  17. cout<<"Simon says touch your toes "<<n<<" times"<<endl;
  18. }

五、程序清单2.6(用户自定义有返回函数)

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int simon(int n);
  5.  
  6. void main()
  7. {
  8. int n;
  9. cout<<"请输入青蛙数量:";
  10. cin>>n;
  11. cout<<"共有"<<simon(n)<<"只腿"<<endl;
  12. system("pause");
  13. }
  14.  
  15. int simon(int n){
  16. return *n;
  17. }

六、章节后的编程练习

1.显示姓名和地址

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. cout<<"XXX"<<endl;
  7. cout<<"xxx university"<<endl;
  8. cin.get();
  9. }

2.用户输入一个以 long 为单位的距离,然后将它转换为码(一long等于 220 码)。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. long l;
  7. cout<<"请输入距离(long):";
  8. cin>>l;
  9. cout<<l*<<"码"<<endl;
  10. system("pause");
  11. }

3.使用 3 个用户定义的函数(包括 m a iri()), 并生成下面的输出:

Three blind mice
Three blind mice
See how they run
See how they run

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void mice(){
  5. cout<<"Three blind mice"<<endl;
  6. }
  7. void run(){
  8. cout<<"See how they run"<<endl;
  9. }
  10.  
  11. void main()
  12. {
  13. mice();
  14. mice();
  15. run();
  16. run();
  17. system("pause");
  18. }

4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:

Enter your age : 29

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. int age;
  7. cout<<"Enter your age :";
  8. cin>>age;
  9. cout<<age*<<endl;
  10. system("pause");
  11. }

5.编写一个程序,其 中 的 m ain() 调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华

氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value : 20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是转换公式:

华氏温度 = 1.8 X 摄氏温度十 32.0

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. double Celsius;
  7. cout<<"Please enter a Celsius value:";
  8. cin>>Celsius;
  9. cout<<Celsius<<" degrees Celsius is "<<Celsius*1.8+<<" degrees Fahrenheit."<<endl;
  10. system("pause");
  11. }

6 . 编写一个程序,其 main() 调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。
该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years : 4.2
4.2 light years = 265608 astronomical units .
天文单位是从地球到太阳的平均距离(约 150000000 公里或 93000000 英里),光年是光一年走的距离
(约 10 万亿公里或 6 万亿英里)(除太阳外,最近的恒星大约离地球 4 .2 光年)。请使用 double 类 型 (参见
程序淸单 2 .4 ) , 转换公式为:
1 光年 =63240 天文单位

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. double light;
  7. cout<<"Enter the number of light years:";
  8. cin>>light;
  9. cout<<light<<" light years="<<light*<<" astronomical units."<<endl;
  10. system("pause");
  11. }

7 . 编写一个程序,要求用户输入小时数和分钟数。在 main() 函数中,将这两个值传递给一个 void 函
数,后者以下面这样的格式显示这两个值:
Enter the number of hours : 9
Enter the number of m inutes : 28
Time : 9:28

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void show(int hours,int minutes){
  5. cout<<hours<<":"<<minutes<<endl;
  6. }
  7.  
  8. void main()
  9. {
  10. int hours,minutes;
  11. cout<<"Enter the number of hours:";
  12. cin>>hours;
  13. cout<<"Enter the number of minutes:";
  14. cin>>minutes;
  15. show(hours,minutes);
  16. system("pause");
  17. }

[C++ Primer Plus] 第2章、开始学习c++的更多相关文章

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

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

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

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

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

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

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

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

  5. 逆向基础 C++ Primer Plus 第二章 开始学习C++

    C++ Primer Plus 第二章 开始学习C++ 知识点梳理 本章从一个简单的C++例子出发,主要介绍了创建C++程序的步骤,以及其所包含的预处理器编译指令.函数头.编译指令.函数体.注释等组成 ...

  6. C++ primer plus读书笔记——第2章 开始学习C++

    第2章 开始学习C++ 1. endl确保程序继续运行前刷新输出(将其立即显示在屏幕上),而使用"\n"不提供这样的保证,这意味着在有些系统中,有时可能在您输入信息后才会出现提示. ...

  7. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  8. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  9. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  10. 跟着鸟哥学Linux系列笔记3-第11章BASH学习

    跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 跟着鸟哥学Linux系列笔记2-第10章VIM学习 认识与学习bash 1. ...

随机推荐

  1. 如何快速知道一个颜色的rgb值

    1.如果你想使用某种颜色缺不知道rgb值是多少,可以将一张图片用系统自带的画图(我的系统是win7)0工具打开,点击编辑颜色就会出现调色板,然后就可以选择查看具体颜色的rgb值了 2.如果你想知道某个 ...

  2. python的赋值,深拷贝和浅拷贝的区别

    原文地址https://www.cnblogs.com/xueli/p/4952063.html 赋值:a = [1,2,3,["a","b"]]  b=a,那 ...

  3. android逆向四则运算

    不断更新 除法: ; bRet = a/b+; return bRet; .text:00001010 a = R0 ; int.text:00001010 b = R1 ; int.text:000 ...

  4. java随机排座位

    //打乱学生顺序 Collections.shuffle(); 容我记个单词 peer: vi.凝视; 盯着看; 隐退,若隐若现; 同等,比得上;n.同辈,同等的人; 贵族; 同伴,伙伴;adj.贵族 ...

  5. uml的几种关系总结

    UML类图几种关系的总结    在UML类图中,常见的有以下几种关系:泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregati ...

  6. PyQT5速成教程-4 Qt Designer实战[上]

    本文由 沈庆阳 所有,转载请与作者取得联系! 前言 在前面几节的学习中,我们对PyQt的基本使用.Qt Designer与Python编码的工作流程有了基本的学习.同时也掌握了Qt Designer中 ...

  7. python windows 安装pandas,numpy....

    用cmd进入python的安装目录的sripts文件夹下,输入pip install pandas 等它自己下载安装完成,提示

  8. JavaScript-判断指定日期是一年中第几天-按照从大到小的顺序输出

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. windows8安装msi或exe软件提示2503错误的解决办法

    windows8以后的版本安装msi软件(比如nodejs.msi.Git.msi.python.msi.T ortoiseSVN.msi)的时候老师出现2503.2502的错误,究其原因还是系统权限 ...

  10. html5-fieldset和legend和keygen元素的用法

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...