C++primer第一章(部分)

时间:2023-03-10 06:35:37
C++primer第一章(部分)

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第一章(部分)