/
7.8从用户输入中读取字符串.cpp
59 lines (53 loc) · 1.48 KB
/
7.8从用户输入中读取字符串.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include"print.h"
#include<sstream>
bool prompt(std::string_view s, std::string_view s2 = "") {
if (s2.size())std::cout<<format("{} ({}): ", s, s2);
else std::cout<<format("{}: ", s);
std::cout.flush();
return true;
}
void clearistream() {
std::string s{};
std::cin.clear();
std::getline(std::cin, s);
}
std::string trimstr(const std::string& s) {
constexpr const char* whitespace{ " \t\r\n\v\f" };
if (s.empty())return s;
const auto first{ s.find_first_not_of(whitespace) };
if (first == std::string::npos)return{};
const auto last{ s.find_last_not_of(whitespace) };
return s.substr(first, (last - first + 1));
}
int main() {
constexpr size_t MAXLINE{ 1024 };
char s[MAXLINE]{};
std::string line{};
std::string word{};
const char* pl{ "Words here" };
const char* pla{ "More words here" };
const char* p2{ "Please enter two numbers" };
const char* p3{ "Comma=separated words" };
prompt(pl);
std::cin.getline(s, MAXLINE, '\n');
std::cout << s << '\n';
prompt(pla, "pla");
std::getline(std::cin, line, '\n');
std::cout << line << '\n';
double a{};
double b{};
for (prompt(p2); !(std::cin >> a >> b); prompt(p2)) {
std::cout << "not numeric\n";
clearistream();
}
std::cout << std::format("You netered {} and {}\n", a, b);
std::cin.get();
line.clear();
prompt(p3);
while (line.empty())std::getline(std::cin, line);
std::stringstream ss(line);
while (std::getline(ss, word, ',')) {
if (word.empty())continue;
std::cout << std::format("word: [{}]\n", trimstr(word));
}
}