Читает строку, выводит по отдельности числа и слова. Если есть на свете "Высокое Порно", то это должно быть "Высоким Быдлокодом"

Превышение INT_MAX не отслеживает, потому слишком длинные числа обрабатывает неправильно, а так вроде работает...
C++ |
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
60
61
62
63
64
| #include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <algorithm>
#include <iterator>
#include <cctype>
int main(){
int num;
char c;
std::string strNum, strWrd;
std::list<int> numbers;
std::list<std::string> words;
std::cout << "String: ";
strNum = strWrd = "";
while ( std::cin.get(c) ) {
if ( isspace(c) ){
if ( ! strNum.empty() ){
std::stringstream ss;
ss << strNum;
ss >> num;
numbers.push_back(num);
strNum = "";
}
if ( ! strWrd.empty() ){
words.push_back(strWrd);
strWrd = "";
}
if ( c == '\n' )
break;
}
else if ( isdigit(c) ){
if ( ! strWrd.empty() ){
words.push_back(strWrd);
strWrd = "";
}
strNum += c;
}
else {
if ( ! strNum.empty() ){
std::stringstream ss;
ss << strNum;
ss >> num;
numbers.push_back(num);
strNum = "";
}
strWrd += c;
}
}
if ( ! numbers.empty() ){
std::cout << "Numbers:" << std::endl;
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
if ( ! words.empty() ){
std::cout << "Words:" << std::endl;
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
return 0;
} |
|