字符变量

This commit is contained in:
flykhan 2023-06-16 09:15:39 +08:00
parent e122c1de29
commit 12e18ec415
4 changed files with 63 additions and 2 deletions

3
.gitignore vendored
View File

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

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;
}

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;
}