[C++ Primer Plus] 第2章、开始学习c++

摘要:
“˂˂endl;7//getchar();8cin.get();//按enter键结束9}II.程序清单2.31#include2usingspacestd;3voidmain()4{5int胡萝卜;6cout˂˂”你有多少胡萝卜?“;9胡萝卜=胡萝卜+2;10cout˂˂”现在你有了“˂˂胡萝卜˂˂”胡萝卜。
一、程序清单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+2;
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(3);
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 4*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*220<<""<<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*12<<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+32<<" 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*63240<<" 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++》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇有道翻译js加密参数分析使用 Code Snippet 简化 Coding下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

docker创建一个容器

而容器技术的核心功能,就是通过约束和修改进程的动态表现,从而为其创造出一个“边界”。 对于 Docker 等大多数 Linux 容器来说,Cgroups 技术是用来制造约束的主要手段,而 Namespace 技术则是用来修改进程视图的主要方法。 你可能会觉得 Cgroups 和 Namespace 这两个概念很抽象, 别担心,接下来我们一起动手实践一下,你...

JAVA8 Stream

Stream流可以说是 Java8 新特性中用起来最爽的一个功能了,有了它,从此操作集合告别繁琐的for循环。与IO流不是一个概念。 Java8 Stream 使用的是函数式编程模式,如同它的名字一样,它可以被用来对集合进行链状流式的操作。 循环遍历的弊端: for循环的语法就是“怎么做” for循环的循环体才是“做什么” 为什么使用循环? 因为要进行遍历...

linux下memcached的安装

系统镜像及环境要求: 1) 适用于windows系列版本及开发者的相关教程  请参考本文1.0开始安装步骤 2)  Centos 6系列及Aliyun Linux 6系列以上版本 请参考本文2.0开始安装步骤 3)  Centos 5系列及Aliyun Linux 5系列版本,请参考本文3.0开始安装步骤 4)  Ubuntu Debian等系列版本,请...

gdb 调试

昨天下班之间请教师父一个panic的解法,见他用gdb调试,真的很厉害,看到堆栈的东西,跟踪堆栈,定位报错的panic出错的具体汇编语句,很惊吓,感叹自己也是用gdb很久的人了,他的很多用法还不知道,特补充下vmlinux和gdb的相关知识在此作为笔记。 (gdb)list *0xc33310a0 (查看0xc33310a0地址所在的源代码段,常用来查看报...

using和名空间namespace

using 指令后面跟namespace可以将当前的嵌套层与一个指定的名空间连在一起,以便使该名空间下定义的对象和函数可以被访问。 我们能够直接使用在namespace中定义的变量而不用在前面加任何范围操作符。例如: #include <iostream>#include <string>using namespace std...

C#-MVC开发常见异常处理

一、EF框架问题 错误的提示为:Model compatibility cannot be checked because the database does not contain model metadata.Model compatibility can only be checked for databases created using Cod...