@igorrr37
1812 / 1430 / 214
Регистрация: 21.12.2010
Сообщений: 2,331
|
10.11.2012, 22:34
|
|
а попробуй такую штуку
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| #include <iostream>
#include <string>
#include <cctype>
#include <list>
#include <utility>
class Parser
{
public:
Parser(){}
void Parse(std::string const& s)
{
list.clear();
let.clear();
dig.clear();
state = Base;
for(std::string::const_iterator cib = s.begin(), cie = s.end(); cib != cie; ++cib)
{
if(isalpha(*cib) && isupper(*cib))
{
switch(state)
{
case Dig:
Dump();
state = Base;
break;
default:
state = Let;
break;
}
Collect(*cib);
}
else if(isdigit(*cib))
{
if(Let == state)
{
state = Dig;
}
Collect(*cib);
}
else
{
if(Dig == state)
Dump();
state = Base;
}
}
if(Dig == state)
Dump();
}
typedef std::list<std::pair<std::string, std::string> >::const_iterator const_iterator;
const_iterator begin() const
{
return list.cbegin();
}
const_iterator end() const
{
return list.cend();
}
private:
enum State{Base = 0, Let = 1, Dig = 2};
State state;
std::list<std::pair<std::string, std::string> > list;
std::string let, dig;
void Collect(char c)
{
if(isdigit(c))
dig += c;
else if(isalpha(c) && isupper(c))
let += c;
}
void Dump()
{
list.push_back(std::make_pair(let, dig));
let.clear();
dig.clear();
}
};
int main()
{
Parser p;
p.Parse("=C1+AC234/A1-C51");
for(Parser::const_iterator ib = p.begin(), ie = p.end(); ib != ie; ++ib)
{
std::cout << ib->first << ' ' << ib->second << std::endl;
}
return 0;
} |
|
0
|