Skip to content

Commit 5dc06cd

Browse files
committed
Created the Folder "code_in_book"
1 parent 63a351f commit 5dc06cd

36 files changed

+819
-74
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cmake.configureOnOpen": true
3+
}

.vscode/tasks.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: g++.exe build active file",
6+
"command": "D:\\msys64\\ucrt64\\bin\\g++.exe",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "D:\\msys64\\ucrt64\\bin"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "Task generated by Debugger."
25+
}
26+
],
27+
"version": "2.0.0"
28+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
我为此规划了一个大致的编章结构,参见[Structure.md](https://github.com/cppHusky/cppHusky-cpp-Tutorial/blob/main/Structure.md)
1010

11-
正在疯狂赶进度,还要补充插图。现阶段争取保持每3天更新一章的速度。
11+
正在疯狂赶进度,还要补充插图。现阶段争取保持每4天更新一章的速度。
12+
13+
我正在考虑把书中每一份正式的示例代码打包并存至本项目中。现在尚在研究阶段。
1214

1315
如果发现内容有任何疏漏和错误,欢迎来作补充和提出修改意见!可以直接提交到[Issues](https://github.com/cppHusky/cppHusky-cpp-Tutorial/issues)当中。

code_in_book/1.1/Hello_World.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// 这是一个简单的C++程序,用于输出Hello World
2+
#include <iostream> //标准输入输出头文件
3+
using namespace std; //使用std命名空间
4+
int main() { //主函数,程序执行始于此
5+
cout << "Hello World!";//输出Hello World
6+
return 0;
7+
}

code_in_book/1.2/calc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// 这是一个简单的C++程序,用于简易的数值计算
2+
#include <iostream> //标准输入输出头文件
3+
using namespace std; //使用std命名空间
4+
int main(){ //主函数,程序执行始于此
5+
double a {1.5}, b {0.3}, c {0.5}, d {4.5}; //定义并初始化
6+
cout << (a + b * c) / d; //输出(a+b*c)/d的值
7+
return 0;
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 这是一个简单的C++程序,用于输出一些整型数值的上/下限值
2+
#include <iostream> //标准输入输出头文件
3+
#include <limits> //标准算术类型属性头文件,numeric_limits定义于此
4+
using namespace std; //使用std命名空间
5+
int main(){ //主函数,程序执行始于此
6+
cout << numeric_limits<short>::lowest(); //输出short类型的下限值
7+
cout << endl; //输出换行,下同
8+
cout << numeric_limits<short>::max(); //输出short类型的上限值
9+
cout << endl;
10+
cout << numeric_limits<unsigned>::lowest(); //输出unsigned类型的下限值
11+
cout << endl;
12+
cout << numeric_limits<unsigned>::max(); //输出unsigned类型的上限值
13+
return 0;
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 这是一个简单的程序,用来将用户所有的输入转为ASCII码
2+
// 读者无需掌握本代码的全部细节,后续会讲到
3+
#include <iostream> //标准输入输出头文件
4+
using namespace std; //使用命名空间std
5+
int main() { //主函数,程序执行始于此
6+
char c; //定义一个char变量c用以接收输入
7+
while (cin.good()) { //while循环,当cin状态正常时继续循环
8+
c = cin.get(); //通过cin.get()接收输入,并存入c
9+
std::cout << static_cast<int>(c) << std::endl;
10+
//强制类型转换为int,故以ASCII码值形式显示输入内容
11+
}
12+
}

code_in_book/3.2/odd_or_even.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//这段代码可以判断输入的正整数是奇数还是偶数
2+
#include <iostream>
3+
using namespace std;
4+
int main() { //主函数,代码顺序执行
5+
int num; //也可以用unsigned
6+
cin >> num; //输入num变量
7+
const int mod {num % 2}; //num模除2的值;也可以不用const
8+
if (mod == 1) { //选择结构
9+
cout << "奇数";
10+
}
11+
else {
12+
cout << "偶数";
13+
}
14+
return 0;
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//这是一个基于while循环的奇偶判断程序,当输入为字母时停止循环
2+
#include <iostream> //标准输入输出头文件
3+
using namespace std; //命名空间使用
4+
int main() { //主函数
5+
int num; //定义num,可不初始化
6+
while (cin >> num) { //输入num并使用cin的返回值作为判断条件
7+
const int mod {num % 2}; //num模除2的值
8+
if (mod == 0) { //判断是否为偶数
9+
cout << "偶数" << endl;
10+
}
11+
else { //mod可能是1或-1,我们把它们都归入else中
12+
cout << "奇数" << endl;
13+
}
14+
}
15+
cout << "程序结束"; //输出一个提示语,告诉用户程序结束
16+
}

code_in_book/3.4/sum.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
void input_clear() {
4+
cin.clear(); //清除错误状态
5+
while (cin.get() != '\n') //清除本行输入
6+
continue;
7+
}
8+
int main() { //主函数
9+
int sum {0}, x {1}; //x初始化为1是为了防止误结束循环
10+
while (x != 0) { //当x为0时结束循环
11+
cin >> x; //输入x的值
12+
if (!cin) {
13+
input_clear(); //清除错误和错误输入
14+
x = 1; //x赋为1,否则可能导致循环误结束
15+
cout << "请输入数字!输入0结束计算" << endl;
16+
continue; //跳过sum+=x的操作
17+
}
18+
sum += x; //这是一个复合赋值运算符,等效于sum=sum+x
19+
}
20+
cout << "总数为:" << sum;
21+
return 0;
22+
}

0 commit comments

Comments
 (0)