第1章 开始
初识输入输出
1 | #include <iostream> //载入iostream库 |
练习题
打印”Hello World”
1
2
3
4
5
6#include <iostream>
int main() {
std::cout << "Hello, World" << std::endl;
return 0;
}编写乘法运算
1
2
3
4
5
6
7
8
9#include <iostream>
int main() {
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The multiplication of " << v1 << " and " << v2 << " is " << v1 * v2 << std::endl;
return 0;
}
控制流
while语句
1 | #include <iostream> |
练习题
50到100的整数相加
1
2
3
4
5
6
7
8
9
10
11#include <iostream>
int main() {
int sum = 0, val = 50;
while (val <= 100) {
sum += val;
++val;
}
std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
return 0;
}打印10到0之间的整数
1
2
3
4
5
6
7
8
9
10
11#include <iostream>
int main() {
int number = 0, val = 10;
while (val >= 0) {
number = val;
std::cout << number << " " << std::endl;
--val;
}
return 0;
}打印指定范围内的所有整数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <iostream>
int main() {
std::cout << "Enter two number:" << std::endl;
int num1 = 0, num2 = 0;
std::cin >> num1 >> num2;
if (num1 < num2) {
while (num1 <= num2) {
std::cout << num1 << std::endl;
++num1;
}
}
else {
while (num1 >= num2) {
std::cout << num1 << std::endl;
--num1;
}
}
return 0;
}
for语句
1 | #include <iostream> |
读取数量不定的输入数据
1 | #include <iostream> |
if语句
1 | #include <iostream> |
不够完善
类简介
1 | #include <iostream> |
书店程序
1 | #include <iostream> |
第2章 变量和基本类型
基本内置类型
类型转换
1 | #include <iostream> |
练习题
- 变量赋值是否正确
1
2
3
4
5
6
7
8#include <iostream>
int main() {
std::cin >> int input_value; //意外的类型
int i = { 3.14 }; //从double转换到int需要收缩转换
double salary = wage = 999.99; //wage为声明的标识符
int i = 3.14; //3
}
变量
作用域
1 | #include <iostream> |
练习题
- 求j的值
1
2
3
4
5
6
7#include <iostream>
int i = 42;
int main() {
int i = 100;
int j = i;
std::cout << "i = " << i << " j = " << j << std::endl; //100
}
复合类型
引用
练习题
求输出结果
1 | #include <iostream> |
指针
练习题
更改指针的值及所指对象的值
1
2
3
4
5
6
7
8
9#include <iostream>
int main() {
int val1 = 15, val2 = 16;
int *p1 = &val1;
p1 = &val2;
val2 = 17;
std::cout << "val1 = " << val1 << " p1 = " << *p1 << " val2 = " << val2 << std::endl; // 由符号*得到指针p1所指的对象 15 17 17
}代码结果
1
2
3
4
5
6
7
8
9#include <iostream>
int main() {
int i = 42;
int *p1 = &i;
*p1 = *p1 * *p1; // 1764
std::cout << *p1 << std::endl;
}
const
练习题
初始化是否合法
1 | int &r = -1, i= 0; //int &r错误无法从int 转换为 &r |
处理类型
练习题
计算值
1
2
3
4
5
6
7
8#include <iostream>
int main() {
int a = 3, b = 4;
decltype(a) c = a;
decltype((b)) d = a;
std::cout << ++c << " " << ++d << std::endl; // 4 4
}计算值
1
2
3
4
5
6
7
8#include <iostream>
int main() {
int a = 3, b = 4;
decltype(a) c = a;
decltype((a = b)) d = a;
std::cout << c << " " << d << std::endl; // 3 3
}
自定义数据类型
练习题
- 利用自定义数据类型重写bookshop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <iostream>
#include <string>
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main() {
double price = 0.0;
Sales_data item1, item2;
std::cin >> item1.bookNo >> item1.units_sold >> price;
item1.revenue = item1.units_sold * price;
std::cin >> item2.bookNo >> item2.units_sold >> price;
item2.revenue = item2.units_sold * price;
if (item1.bookNo == item2.bookNo) {
unsigned totalCnt = item1.units_sold + item2.units_sold;
std::cout << "图书" << item1.bookNo << ": 总共销售" << totalCnt << "本,收入" << item1.revenue + item2.revenue << "元" << std::endl;
}
else {
std::cout << "图书" << item1.bookNo << ": 总共销售" << item1.units_sold << "本,收入" << item1.revenue << "元" << std::endl;
std::cout << "图书" << item2.bookNo << ": 总共销售" << item2.units_sold << "本,收入" << item2.revenue << "元" << std::endl;
}
}
第3章 字符串,向量和数组
命名空间的using声明
1 | #include <iostream> |
string对象上的操作
1 | #include <iostream> |
For处理范围内的字符
1 | string s("Hello World!!!"); |
练习题
替换所有字符为X
1
2
3
4
5
6
7
8
9string s("Hello World!!!"), x;
for (auto c : s) {
c = 'X';
x = x + c;
}
cout << " punctuation characters in " << x << endl;
return 0;删除字符串中的标点符号
1
2
3
4
5
6
7
8
9
10
11
12string s("Hello World!!!"), shuchu;
for (auto c : s) {
if (!ispunct(c)) {
shuchu = shuchu + c;
}
}
cout << "删除符号最后结果 " << shuchu << endl;
return 0;
vector 对象
练习题
cin读入一组整数并把它们保存到vector对象
1
2
3
4
5
6
7
8
9
10
11int val;
vector<int> number;
while (cin >> val) {
number.push_back(val);
}
for (int i = 0; i < number.size(); i++) {
cout << number[i] << endl;
}
return 0;读入一组字符串
1
2
3
4
5
6
7
8
9
10
11string word;
vector<string> chars;
while (cin >> word) {
chars.push_back(word);
}
for (int i = 0; i < chars.size(); i++) {
cout << chars[i] << endl;
}
return 0;
迭代器介绍
迭代控制字符串第一个文字大小写
1 | int main() { |
迭代器修改全部字符串
1 | int main() { |
第4章 表达式
基础
1 | int main() { |
练习题判断奇数偶数
1 | int main() { |