Compare commits

..

3 Commits

Author SHA1 Message Date
flykhan 12e18ec415 字符变量 2023-06-16 09:15:39 +08:00
flykhan e122c1de29 字符串: '0' & 0 & \n 2023-06-15 16:55:31 +08:00
flykhan ef7b0af039 键盘获取输入 2023-06-15 16:43:42 +08:00
7 changed files with 102 additions and 2 deletions

5
.gitignore vendored
View File

@ -86,9 +86,10 @@ Module.symvers
Mkfile.old
dkms.conf
# 自定义
# 自定义过滤
*/output
*.i
*.s
*.o
*.swp
*.swp
.vscode

15
day4/d10.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main()
{
char c = 'a'; // 1B
int m = 2; // 4B
// 小字节转大字节, 会自动转换
m += c; // 先将c转换为int,再相加
cout << "m = " << m << endl;
// 大字节转小字节,会丢失精度,需要强制类型转换
cout << "m to char is " << (char)m << endl;
return 0;
}

11
day4/d11.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <iostream>
using namespace std;
int main()
{
cout << "'0' = " << (int)'0' << endl;
cout << 0 << endl;
cout << (int)'\0' << endl;
return 0;
}

24
day4/d12.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <regex> // 引入正则表达式库
using namespace std;
int main()
{
string x;
cout << "输入一个字符:" << endl;
cin >> x;
regex regex_lower("[a-z]");
regex regex_upper("[A-Z]");
cout << regex_match(x, regex_lower) << regex_match(x, regex_upper) << endl;
// 正则表达式
if (regex_match(x, regex_lower)) // regex_match 匹配正则表达式
{
cout << (char)(x.at(0) - 32) << endl; // .at(0) 获取第一个字符
}
else if (regex_match(x, regex_upper))
{
cout << (char)(x.at(0) + 32) << endl;
}
}

24
day4/d13.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <string>
#include <iostream>
using namespace std;
int main()
{
// 字符串打印遇到\0则结束打印
string str1 = "abc\0good"; // abc\0good\0
cout << str1 << str1 << endl;
cout << "sizeof str1 = " << sizeof(str1) << endl; // 32B 32*8=256位
char str2[] = "disen666"; // 字符数组, 实际上也是一个字符串
cout << "str2 = " << str2 << endl;
char str3[] = {'d', 'i', 's', 'e', 'n'}; // 自动添加 '\0'
cout << "str3 = " << str3 << endl;
cout << "sizeof str3 = " << sizeof(str3) << endl; // 5B, 没算 '\0'
string str4 = "a";
cout << "sizeof str4 = " << sizeof(str4) << endl; // 32B 32*8=256位
return 0;
}

13
day4/d8.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
using namespace std;
int main()
{
int x1, y1, x2, y2;
cout << "请输入两个点坐标, 格式如 x1 y1 x2 y2" << endl;
cin >> x1 >> y1 >> x2 >> y2;
cout << "P1(" << x1 << "," << y1 << ")"
<< " P2(" << x2 << "," << y2 << ")" << endl;
return 0;
}

12
day4/exercise_2.4.5.2.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "输入两个数:" << endl;
cin >> a >> b;
cout << "最大值为: " << (a > b ? a : b) << endl;
return 0;
}