-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
37 lines (29 loc) · 838 Bytes
/
test.cpp
File metadata and controls
37 lines (29 loc) · 838 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int GetIntegerInput(const string& InputString) {
// 正则表达式匹配前后可以有空格的单个整数,并捕获该整数
cout << "InputString: " << InputString << endl;
regex pattern(R"(^(\d)$)");
smatch match;
if (regex_match(InputString, match, pattern)) {
// 提取捕获的数字部分
string number = match.str(1);
return stoi(number);
} else {
throw invalid_argument("Invalid input");
}
}
int main(){
string input;
cout << "Enter a single digit: ";
cin >> input;
try {
int number = GetIntegerInput(input);
cout << "You entered: " << number << endl;
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
return 0;
}