1.4.2 for 语句

for (init-statement; condition; expression)
statement;

step1:初始化

step2:判断条件,为真则执行循体;为假则退出循环

step3:执行表达式,回到step2

例子:for循环从1加到10
#include <iostream>
int main()
{
int sum = ;
// 从 1 加到 10
for (int val = ; val <= ; ++val) //循环头:控制循环体的执行次数
// 初始化语句 循环条件 表达式
sum += val; //循环体
//val 在循环结束后不能使用
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
return ;
}

练习1.12:

-100加到100,sum的终值为0

练习1.13:

使用for循环将50-100的整数相加
#include <iostream>
int main()
{
int sum = ;
for (int val = ; val <= ; val++)
sum += val;
std::cout << "Sum of 50 to 100 inclusive is "
<< sum << std::endl;
return ;
}

使用for循环按递减顺序打印10-0

#include <iostream>
int main()
{
int val = ;
for (int val = ; val >= ; --val)
std::cout << val << " ";
std::cout << std::endl;
return ;
}

使用for循环打印输入的两个整数之间的数

#include <iostream>
int main()
{
int a , b;
std::cin >> a >> b;
if(a <= b){
for (; a <= b; a++)
std::cout << a << " ";
}
else{
for (; a >= b; a--)
std::cout << a << " ";
}
return ;
}

练习1.14

如果明确循环次数,更推荐使用for循环;不明确则推荐使用while循环
while循环的循环控制变量需要提前初始化,且需要在循环体中修改控制变量的值,形式上不如for简洁
功能上等价,可相互转换
 
练习1.15
熟悉语法错误、类型错误、声明错误
1、语法错误代码
 #include <iostream>
int main()
{
// 语法错误 syntax error
std::cout << "Read each file." << std::endl:
std::cout << Update master. << std::endl;
std::cout << "Write new master" std::endl;
return ;
}

报错信息:

prog1_15.cc: In function 'int main()':
prog1_15.cc::: error: found ':' in nested-name-specifier, expected '::'
std::cout << "Read each file." << std::endl:
^
prog1_15.cc::: error: 'std::endl' is not a class, namespace, or enumeration
std::cout << "Read each file." << std::endl:
^
prog1_15.cc::: error: 'Update' was not declared in this scope
std::cout << Update master. << std::endl;
^
prog1_15.cc::: error: expected ';' before 'master'
std::cout << Update master. << std::endl;
^
prog1_15.cc::: error: expected ';' before 'std'
std::cout << "Write new master" std::endl;
^

2、类型错误代码

 #include <iostream>
int main()
{
// 类型错误 type error
int a = "hello";
return ;
}

报错信息

prog1_15.cc: In function 'int main()':
prog1_15.cc::: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
int a = "hello";
^

3、声明错误代码

 #include <iostream>
int main()
{
// 声明错误 declaration error
int v1 = , v2 = ;
std::cin >> v >> v2;
cout << v1 + v2 << std::endl;
return ;
}

报错信息

prog1_15.cc: In function 'int main()':
prog1_15.cc::: error: 'v' was not declared in this scope
std::cin >> v >> v2;
^
prog1_15.cc::: error: 'cout' was not declared in this scope
cout << v1 + v2 << std::endl;
^
prog1_15.cc::: note: suggested alternative:
In file included from prog1_15.cc:::
d:\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.\include\c++\iostream::: note: 'std::cout'
extern ostream cout; /// Linked to standard output
^

好习惯: 编辑-编译-调试(edit-compile-debug)

1、按照编译器报告的顺序逐个修正错误(原因:单个错误具有传递效应)

2、每修正一个或一小部分明显错误后立即重新编译代码

1.4.3 读取数量不定的输入数据

使用istream对象作为条件时,效果是检测流的状态。

练习1.16:从 cin 读取一组数,输出其和

#include <iostream>
int main()
{
int sum = , value = ;
while (std::cin >> value)
sum += value;
std::cout << "Sum is: " << sum <<std::endl;
return ;
}

windows环境可使用ctrl+z或字母作为结束符,再按回车(因为value为int型,非int型输入导致isteam对象的状态变为无效,使条件为假从而结束循环)

1.4.4 if 语句

if (condition)
statement;
else
statement;

例子:统计输入中每个连续值出现几次

#include <iostream>
int main()
{
int currVal = , val = ;
if (std::cin >> currVal){
int cnt = ;
while(std::cin >> val){
if (val == currVal)
cnt++;
else{ // 否则,打印前一个值个数
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
currVal = val; // 记住新值
cnt = ; // 重置计数器
}
}//while
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
}
return ;
}
 关键概念:C++的程序的缩进和格式
C++在很大程度上是格式自由的:花括号的放置、缩进、注释、换行通常不影响程序的语义。
原则:保证可读性、易理解性、一致性,且一旦选择风格,要坚持使用。
 
1.5 类简介
关键概念:类定义了行为
类定义了类对象可执行的所有动作。
 
练习1.20:读取书籍销售记录,打印
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item book;
while(std::cin>>book)
std::cout << book << std::endl;
return ;
}

练习1.21:读取两个同样ISBN的记录,打印和

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item book1, book2;
std::cin >> book1 >> book2;
if (book1.isbn() == book2.isbn())
std::cout << book1 + book2 <<std::endl;
else
std::cout << "Data must refer to same ISBN" << std::endl;
return ;
}

练习1.22:读取多个具有相同ISBN的销售记录,输出所有记录的和。

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item_sum;
if(std::cin >> item_sum){
while(std::cin >> item1){
if (item1.isbn() == item_sum.isbn())
item_sum += item1;
else{
std::cout << "Data must refer to same ISBN" << std::endl;
break;
}
}
std::cout << item_sum << std::endl;
return ;
}
else{
std::cout << "No data" << std::endl;
return -;
}
}

成员函数(member function):有时也被称为方法(method)

调用方式:类类型的对象.成员名()             点运算符.   调用运算符(),括号内放实参列表,可为空

练习1.23-1.24:读取多条销售记录,统计每个ISBN有几条销售记录(每个ISBN的记录聚在一起)

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item0, item;
if(std::cin >> item0){
int cnt = ;
while(std::cin >> item){
if(item.isbn() == item0.isbn())
cnt++;
else{
std::cout << item0.isbn() << " has " << cnt << " items."
<< std::endl;
item0 = item;
cnt = ;
}
}
std::cout << item0.isbn() << " has " << cnt << " items."
<< std::endl;
}
else
{
std::cout << "No data." << std::endl;
return -;
}
return ;
}

书店程序:读取销售记录,生成每本书的销售报告,显示售出册数、总销售额和平均售价(每个ISBN书号中所有销售记录在文件中聚在一起保存)

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total;
// 读入第一条交易记录,并确保有数据可以处理
if(std::cin >> total){
Sales_item trans;
// 读入并处理剩余交易记录
while(std::cin >> trans){
if(trans.isbn() == total.isbn()) // 如果仍在处理相同的书
total += trans; // 更新总销售额
else{
// 打印前一本书的结果
std::cout << total << std::endl;
total = trans; // 重置
}
}
std::cout << total << std::endl; // 打印最后一本书的结果
}
else
{
std::cerr << "No data?!" << std::endl;
return -;
}
return ;
}

术语

 
 
 
 
 
 

C++primer第一章(部分)的更多相关文章

  1. C++primer第一章

    第一章 : 开始 1.1 编写一个简单的C++程序 要点:每个函数有且只能拥有一个main函数,且main的默认返回是一个int类型. 函数定义:返回类型,函数名,形参列表,函数体 1.1.1编译 运 ...

  2. C++Primer 第一章

    /* 1.main函数的标准写法就只有两种,一种是带命令行的,一种是不带命令行的.其返回类型必须是int. 2.如果main函数最后没有return语句,则编译器会自动加上一句 return 0; * ...

  3. <<C++ Primer>> 第一章 开始 术语表

    术语表 第 1 章 开始 参数(实参, argument): 向函数传递值    赋值(assignment): 抹去一个对象当前值一个新值取代之    缓冲区(buffer): 一个存储区域, 用于 ...

  4. C++ Primer 笔记 第一章

    C++ Primer 学习笔记 第一章 快速入门 1.1 main函数 系统通过调用main函数来执行程序,并通过main函数的返回值确定程序是否成功执行完毕.通常返回0值表明程序成功执行完毕: ma ...

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

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

  6. C++ Primer Plus 第一章 预备知识

    C++ Primer Plus 第一章 预备知识 知识点梳理 本章主要讲述了C++的由来,讨论了面向过程语言与面向对象语言的区别,介绍了ANSI/ISO制定的C++标准,阐述了在Windows.Mac ...

  7. C++PRIMER第五版练习题答案第一章

    C++PRIMER第五版练习题答案第一章 应该有很多小伙伴和我一样,闲来无事买了本C++的书自己啃,课后的练习题做的很揪心,这里我分享下我写的答案,希望能帮助到你,提供源码,就不跑了哈,毕竟现在是第一 ...

  8. C++ primer的第一章的主要内容

    第一章主要是把C++的主要的部分简单的介绍了一下,让读者对C++开始有一个简单的了解.看完第一章的收获就是知道如何去读入不确定数目的输入,主要是形式是:whlie(cin>>s){},利用 ...

  9. 第一章 C++ primer Plus

    第一章 1.4程序创建的技巧 大体如下: 1.使用文本编辑器编写程序,并将其保存到文件中,这个文件就是程序的源代码. 2.编译源代码.这意味着运行一个程序,将源代码翻译为主机使用的内部语言——机器语言 ...

随机推荐

  1. 添加PROPAGATION_REQUIRES_NEW 事务没有产生作用

    最近在做事务添加时  发现自己的事务没有新建,上网查到   仅用作收藏. 其二  注意  事务的注解  应该在 内层的事务上面 一.描述Spring遇到嵌套事务时,当被嵌套的事务被定义为“PROPAG ...

  2. linux 路由表 的一些相关资料

    linux 路由表维护 查看 Linux 内核路由表 使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination Gateway Genmask Flags ...

  3. VS2010与Matlab2010b混合编程

    环境: 1.VS2010 2.MATLAB 2010b 3.WINDOW 7 (X64) 1.Matlab环境设置 要建立独立运行的C应用程序,系统中需要安装Matlab.Matlab编译器.C/C+ ...

  4. oracle中left join,right join,inner join的坑

    本文主要是记录一下实际使用oracle中join查询遇到的坑 1.用到两张表,学生表和学年分数表,先建立 2.普通连接查询 INNER JOIN,查询每个学年有成绩的学生以及分数情况 LFET JOI ...

  5. paddlepaddle

    1. 训练过程中cost出现nan 可能是因为有脏数据,寻找脏数据的方法就是,设置batch_size=1, paddle.reader.shuffle 中buf_size=1 一条一条的进行训练,看 ...

  6. 《HTTP权威指南》读书笔记(二) :URL与资源

    1.URL是什么 URL就是因特网资源的标准化名称.URL指向一条电子信息片段,告诉你它们位于何处,以及如何与之交互.通俗来说,就是浏览器寻找信息所需的资源位置. URI是一类更通用的资源标识符,UR ...

  7. 约瑟夫斯问题-java版数组解法和链表解法

    10个人围成一圈,从1到10编号,从1开始数,数到3或3的倍数的位置,则该位置的人出局,求最后剩下哪一个号? 数组解法: 数组存放数组:a[10]存在1到10编号人 数组遍历到尾部又从头遍历:遍历数组 ...

  8. mininet的学习之二-----miniedit可视化

    安装ryu git clone git://github.com/osrg/ryu.git cd ./ryu  sudo python setup.py install mininet可视化 git ...

  9. Git 几个常用操作

    git init        --    初始化仓库, git clone    --    从远端克隆仓库到本地 git status   --    查看git仓库的状态 git log    ...

  10. 学习笔记DL003:神经网络第二、三次浪潮,数据量、模型规模,精度、复杂度,对现实世界冲击

    神经科学,依靠单一深度学习算法解决不同任务.视觉信号传送到听觉区域,大脑听学习处理区域学会“看”(Von Melchner et al., 2000).计算单元互相作用变智能.新认知机(Fukushi ...