[C++ Primer Plus] 第2章、开始学习c++
一、程序清单2.1(代码和书略不一样)
#include<iostream>
using namespace std;//使用std这个命名空间,才能正确找到cin和cout,如果不使用命名空间,那么使用时需要 std::cout<<"hello"<<std::endl;
void main()
{
cout<<"hello world"<<endl;//endl表示重起一行,信息从“hello”流向cout
cout<<"hey man!"<<endl;
//getchar();
cin.get();//按下enter键结束
}
二、程序清单2.3
#include<iostream>
using namespace std;
void main()
{
int carrots;
cout<<"你有多少胡萝卜?"<<endl;
cin>>carrots;//输入,信息从cin流向carrots
cout<<"这里还有两个。";
carrots=carrots+;
cout<<"现在你有"<<carrots<<"个胡萝卜。"<<endl;
cin.get();//按下enter键结束
cin.get();// 两条cin.get() 语句可以替换成一条语句 system("pause");
}
注意:只有一条 cin.get() 语句的话,在屏幕输入数字后就会一闪而过,只有两条 cin.get() 才能在屏幕看到输出
三、程序清单2.4(调用函数)
#include<iostream>
#include<cmath>//or math.h
using namespace std;
void main()
{
double area,side;
cout<<"你家房子多少平米:"<<endl;
cin>>area;
side=sqrt(area);//开平方根
cout<<"这相当于"<<side<<"米的正方形"<<endl;
system("pause");//暂停,使屏幕处于等待状态
}
四、程序清单2.5(用户自定义无返回函数)
#include<iostream>
using namespace std; void simon(int n);//函数声明,simon函数在main函数之后,若不提前声明则无法在main函数中使用 void main()
{
int n;
simon();
cout<<"请输入一个正整数:";
cin>>n;
simon(n);//此处的n为实际参数(简称“实参”)
system("pause");
} void simon(int n){//此处的n为形式参数(简称“形参”)
cout<<"Simon says touch your toes "<<n<<" times"<<endl;
}
五、程序清单2.6(用户自定义有返回函数)
#include<iostream>
using namespace std; int simon(int n); void main()
{
int n;
cout<<"请输入青蛙数量:";
cin>>n;
cout<<"共有"<<simon(n)<<"只腿"<<endl;
system("pause");
} int simon(int n){
return *n;
}
六、章节后的编程练习
1.显示姓名和地址
#include<iostream>
using namespace std; void main()
{
cout<<"XXX"<<endl;
cout<<"xxx university"<<endl;
cin.get();
}
2.用户输入一个以 long 为单位的距离,然后将它转换为码(一long等于 220 码)。
#include<iostream>
using namespace std; void main()
{
long l;
cout<<"请输入距离(long):";
cin>>l;
cout<<l*<<"码"<<endl;
system("pause");
}
3.使用 3 个用户定义的函数(包括 m a iri()), 并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
#include<iostream>
using namespace std; void mice(){
cout<<"Three blind mice"<<endl;
}
void run(){
cout<<"See how they run"<<endl;
} void main()
{
mice();
mice();
run();
run();
system("pause");
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age : 29
#include<iostream>
using namespace std; void main()
{
int age;
cout<<"Enter your age :";
cin>>age;
cout<<age*<<endl;
system("pause");
}
5.编写一个程序,其 中 的 m ain() 调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华
氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value : 20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是转换公式:
华氏温度 = 1.8 X 摄氏温度十 32.0
#include<iostream>
using namespace std; void main()
{
double Celsius;
cout<<"Please enter a Celsius value:";
cin>>Celsius;
cout<<Celsius<<" degrees Celsius is "<<Celsius*1.8+<<" degrees Fahrenheit."<<endl;
system("pause");
}
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 天文单位
#include<iostream>
using namespace std; void main()
{
double light;
cout<<"Enter the number of light years:";
cin>>light;
cout<<light<<" light years="<<light*<<" astronomical units."<<endl;
system("pause");
}
7 . 编写一个程序,要求用户输入小时数和分钟数。在 main() 函数中,将这两个值传递给一个 void 函
数,后者以下面这样的格式显示这两个值:
Enter the number of hours : 9
Enter the number of m inutes : 28
Time : 9:28
#include<iostream>
using namespace std; void show(int hours,int minutes){
cout<<hours<<":"<<minutes<<endl;
} void main()
{
int hours,minutes;
cout<<"Enter the number of hours:";
cin>>hours;
cout<<"Enter the number of minutes:";
cin>>minutes;
show(hours,minutes);
system("pause");
}
[C++ Primer Plus] 第2章、开始学习c++的更多相关文章
- 《C++Primer》第五版习题答案--第三章【学习笔记】
[C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...
- 《C++Primer》第五版习题解答--第四章【学习笔记】
[C++Primer]第五版习题解答--第四章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/11 第四章:表达式 练习4. ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
- 《C++Primer》第五版习题答案--第六章【学习笔记】
<C++Primer>第五版习题答案--第六章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/16 第六章:函数 ...
- 逆向基础 C++ Primer Plus 第二章 开始学习C++
C++ Primer Plus 第二章 开始学习C++ 知识点梳理 本章从一个简单的C++例子出发,主要介绍了创建C++程序的步骤,以及其所包含的预处理器编译指令.函数头.编译指令.函数体.注释等组成 ...
- C++ primer plus读书笔记——第2章 开始学习C++
第2章 开始学习C++ 1. endl确保程序继续运行前刷新输出(将其立即显示在屏幕上),而使用"\n"不提供这样的保证,这意味着在有些系统中,有时可能在您输入信息后才会出现提示. ...
- 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 ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- 跟着鸟哥学Linux系列笔记3-第11章BASH学习
跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 跟着鸟哥学Linux系列笔记2-第10章VIM学习 认识与学习bash 1. ...
随机推荐
- 1021 Deepest Root
这道题的关键在于如何求两个最远的结点,二叉树比较容易一直DFS就能找到,但是普通树就比较麻烦.要先找到一端,再去找另外一端,两端的并集就是答案.因为结点都是对称的,所以两端都是答案.还要注意去重,12 ...
- cocos2d-x JS 纯代码加载播放plist与png动画
var cache = cc.spriteFrameCache; cache.addSpriteFrames(plist, png); var frames = []; for (var i = 1; ...
- 41.SEO----前端SEO技巧
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- sql注入学习笔记,什么是sql注入,如何预防sql注入,如何寻找sql注入漏洞,如何注入sql攻击 (原)
(整篇文章废话很多,但其实是为了新手能更好的了解这个sql注入是什么,需要学习的是文章最后关于如何预防sql注入) (整篇文章废话很多,但其实是为了新手能更好的了解这个sql注入是什么,需要学习的是文 ...
- Lepus(天兔)监控MySQL部署
http://www.dbarun.com/docs/lepus/install/lnmp/ 注意:xampp mysqldb-python版本太高会导致lepus白屏 apache版本最好选择2.2 ...
- MyBatis基础入门《七》查询参数传入对象
MyBatis基础入门<七>查询参数传入对象 描述: 在执行查询语句的时候,传入的参数是一个对象,依据对象的属性,进行检索数据.此时,书写SQL语句中的条件时,其参数需要和对象中的属性保持 ...
- 从0开始搭建vue+webpack脚手架(二)
接着从0开始搭建vue+webpack脚手架(一) 三.配置webpack-dev-server 1. webpack-dev-server自带一个node的服务器, 项目在服务端运行的同时可以实现热 ...
- Bootstrap-全局CSS样式-图片样式
Bootstrap第二部分:全局CSS样式-图片样式.img-rounded 圆角图片.img-circle 圆形图片.img-thumbnail 缩略图片.img-responsive响应 ...
- MYSQL5.6.X 非在线安装版(解压版)安装过程
一.卸载以前旧版本(本人5.5版本) 1.关闭MySQL服务 以管理员身份运行cmd,执行以下命令: net stop mysql 或者右键我的电脑,在管理——服务——停止MySQL 2.卸载MySQ ...
- oauth2.0学习笔记(摘抄简化)
大量摘抄白话简明教程. 附:可以参考<RFC6749协议中文版及oauth2.0>文档 一.OAuth 白话简明教程 1.简述 http://www.cnblogs.com/Ceri/p/ ...