从零单排《C++ Primer》
——(1)一个简单的c++程序
本次学习收获
0、写在前面
风色曾经上过C++的课程,然而当时并没有认真去学,基本不能使用c++来作项目开发。这次重新学习c++,首先会阅读c++Prime英文版第五版,希望可以为以后的学习打下坚实的基础。
1、程序
一个简单的c++程序(P17),帮助我们快速了解c++的代码风格,并通过分析代码学习c++
程序意图:在终端上输入一组数字,输出不同数字输出的次数。相同的数字必须是连续的。
假设输入:
42 42 42 42 55 55 62 100 100 100
终端应该输出:
42 occurs 5 times
55 occurs 2 times
62 occurs 1 times
100 occurs 3 times
代码:
#include <iostream>
int main()
{
//currVal is the number we are counting;we will read new value into val. currVal用来存放当前计算的数据,新读入的数据会放入val
int currVal = 0, val = 0;
//read first number and ensure that we have data to process. 为确保有可操作的数据,先读入一个
if(std::cin >> currVal){
int cnt = 1; //store the count for the current value we're processing. cnt用来计数,当前存放的数据出现了多少次
while (std::cin >> val){ // read the remaining numbers.读剩下的数据
if(val == currVal) //if the values are the same.如果新读入的数据,和当前计算的数据是一样的
++cnt;
else {
std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;
currVal = val; //remember the new value
cnt = 1;
}
} // while loop ends here. while循环结束
} // outermost if statement ends here . if结束
return 0;
}
2、代码分析:
2.1函数:
每个c++程序拥有至少一个函数,其中一个函数必须叫作main。他是操作系统运行c++程序时程序的入口。在上面的程序中,我们也自己定义了一个main函数,它是:
</pre><pre name="code" class="cpp">int main()
{
//currVal is the number we are counting;we will read new value into val. currVal用来存放当前计算的数据,新读入的数据会放入val
......
return 0;
}
一个函数由4个部分组成:
1)返回类型
在这个函数中,它的返回类型是int 型,表示整数。
2)函数名
在这个函数中为main
3)参数(可以为空)
参数填在main后面的()内,改函数的参数为空
4)函数主体
花括号及以内的部分
这个函数最后一条语句:return 。这是一条可以用来结束函数的语句。在这个例子中,return可以返回一个值给函数的调用者。返回的值必须喝函数的返回类型一致。在这个例子中,函数返回了一个整数型0。
2.2使用Library(函数库):
程序第一行:
#include<iostream>
在程序实现时,我们不仅会自己定义一些函数,通常也会使用一些别人已经写好的函数来简化我们的编码。使用别人的函数必须引入对应的函数库的头文件。
程序迪一行高速编译器我们要使用iostream libraary。<>里面的名称是我们要引入的头文件。这里我们引入了iostream,以便实现在终端上的输入输出操作。<>表示引入的是标准文件,要引入自己的文件,使用""。
2.3注释:
注释掉的内容,编译器时不会执行的,注释用来方便别人阅读代码。
c++有两种注释方法
1)单行注释 //
2)c注释 /* */
2.4标准输入与输出:
这个例子中使用了iostream library来处理标准输入和输出。它包含了istream和ostream,分别代表input streams(输入流) 和 output streams(输出流)。Stream(流)表示从输入输出设备读取/写入一系列有顺序的字符。
std::cin >> currVal
从终端上读入一个数据,存放在变量currVal上。currVal变量用来存放当前计算的数字。
std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;
从终端上输出当前数字共出现了多少次。变量cnt记录了currVal所存放的数字出现的次数。endl表示换行。输入输出可以这样连续使用:
int v1 =0, v2 = 0;
std::cin>>v1>>v2;
std::cout<<v1<<v2<<std::endl;
这是因为>>(<<)会返回它左边的操作数(operand)
2.5命名空间:
如果引入了不同库,而库里面如果有同样名称的函数,调用的时候这个函数的时候,编译器就会不知道调用哪个。为了解决这个冲突,c++使用了命名空间。注意到
std::cin >> currVal
里面的std::,表示使用标准库里面的cin函数。
2.6IF语句以及IF语句与输入语句的组合使用:
if(条件语句){
//执行内容
}
首先执行在()内的语句条件语句,只有条件语句为真时才执行{ } 内的内容。常见的用法有if( a == b) , 如果a等于b,则a==b为真,执行{}内容,否则a==b为false,不执行{}内.事实上,条件语句非0即为真。
在这个例子中,有
if(std::cin >> currVal)
注意到,如果std::cin>>currVal成功读取到数据,则为真。
2.7WHILE语句以及IF语句与输入语句的组合使用:
while(条件语句){
//执行内容
}
首先执行条件语句,如果条件为真,则会执行{}内的内行,接着继续执行条件语句,如果继续为真,则继续执行{}内容,如此循环,直到条件执行后为假。
结合if语句里所述,在不知道有多少数据要读取时,可以编写如下语句
while(std::cin>>val){
}
当istream无效时,std::cin>>currVal读取失败,条件为假。无效的情况有
1)当我们输入end-of-file(eof)时 (Dos/Windows:ctrl+z Linux:ctrl+d)
2)输入了非法数据,如数据为空,或者类型不符
3、作业:
p9
/*
* Exercise 1.3: Write a program to print Hello,World on the standard output
* 写一个程序实现在标准输出中打印Hello,World
*/
#include<iostream>
int main()
{
std::cout<<"Hello,World"<<std::endl;
return 0;
}
p11
/*
* Exercise 1.8:Indicate which,if any,of the following output statements are legal:
* 指出哪些输出语句是合法的
*/
#include <iostream>
int main(){
std::cout<<"/*";//合法输出为/*
// std::cout<<"*/";//合法输出为*/
// std::cout<</*"*/"*/;//非法 最后缺少一个"
// std::cout<</* "*/" /* "/*" */;//合法 输
/*
* Exercise 1.9: Write a program that uses a while to sum the numbers from 50 to 100
* 用while计算50到100的累加值
*/
#include <iostream>
int main(){
int sum = 0,val = 50;
while(val<101){
sum += val;
++val;
}
std::cout<<"The sum of 50 to 100 inclusive is "<< sum <<std::endl;
return 0;
}
/*
*Exercise 1.11
*Write a program that prompts the user for two integers.Print each number in the range specified by thoe two intergers
*提示用户输入两个整数。打印着两个整数翻飞内的数。
*/
#include<iostream>
int main(){
int val1 = 0, val2 = 0, temp = 0;
std::cout<<"please input two intergers"<<std::endl;
if(std::cin>>val1){
if(std::cin>>temp){
if(temp>val1)
val2 = temp;
else{
val2 = val1;
val1 = temp;
}
while(val1<=val2){
std::cout<<val1<<std::endl;
++val1;
}
}
}
return 0;
}