几百年没有写小程序玩。
//1 2 3 5 6 8 9 -> 1-3,5-6,8,9
1#include "generator1.h"
2
3typedef std::vector<std::wstring> WSTRVECTOR;
4typedef std::vector<int> INTVECTOR;
5
6struct range
7{
8 int start;
9 int end;
10};
11typedef std::vector<range> RGVECTOR;
12
13void CovertToRangeVector( INTVECTOR &nINTVECTOR, RGVECTOR &rgVector )
14{
15 INTVECTOR::size_type pos = 0;
16 int nPre = -1;
17 int nIndex = -1;
18 while (pos < nINTVECTOR.size())
19 {
20 if (0 == pos)
21 {
22 nPre = nINTVECTOR[pos];
23 range rgTemp;
24 rgTemp.start = rgTemp.end = nPre;
25 rgVector.push_back(rgTemp);
26 ++pos;
27 ++nIndex;
28 continue;
29 }
30
31 if (1 == nINTVECTOR[pos] - nPre)
32 {
33 rgVector[nIndex].end = nINTVECTOR[pos];
34 nPre = nINTVECTOR[pos];
35 ++pos;
36 continue;
37 }
38 else
39 {
40 range rgTemp;
41 rgTemp.start = rgTemp.end = nINTVECTOR[pos];
42 rgVector.push_back(rgTemp);
43 nPre = nINTVECTOR[pos];
44 ++pos;
45 ++nIndex;
46 continue;
47 }
48 }
49}
50
51void ConvertToWstringVector( RGVECTOR &rgVector, WSTRVECTOR &wstrvtUserStringAns )
52{
53 for (RGVECTOR::size_type pos = 0; pos < rgVector.size(); ++pos)
54 {
55 //s - e, s - e, e
56 std::wstring wstrRange;
57 std::wstring wstrStart;
58 std::wstring wstrEnd;
59
60 if (rgVector[pos].start != rgVector[pos].end)
61 {
62 wstring wstrTemp;
63 wchar_t Buffer[256] = {0};
64 int nStart = rgVector[pos].start;
65 _ltow_s(rgVector[pos].start, Buffer, 10);
66 //swprintf(Buffer, L"%d", L"DSDASDA");
67 wstrTemp.assign(Buffer);
68 wstrTemp.append(L"-");
69 _ltow_s(rgVector[pos].end, Buffer, 10);
70 //swprintf(Buffer, L"%d", L"DSDASDA");
71 wstrTemp.append(Buffer);
72
73 wstrRange.append(wstrTemp);
74 }
75 else
76 {
77 WCHAR Buffer[256] = {0};
78 _ltow_s(rgVector[pos].start, Buffer, 10);
79 /**//*wsprintf(Buffer, L"%d", rgVector[pos].start);*/
80 wstring wstrTemp;
81 wstrTemp.assign(Buffer);
82
83 wstrRange.append(wstrTemp);
84 }
85
86 if (pos + 1 < rgVector.size())
87 {
88 wstrRange.append(L",");
89 }
90 wstrvtUserStringAns.push_back(wstrRange);
91 }
92}
93HRESULT SimpleGeneratorToUserString(INTVECTOR &nINTVECTOR, WSTRVECTOR &wstrvtUserString)
94{
95 //1 2 3 5 6 8 9 -> 1-3,5-6,8,9
96 //ASSERT(!nINTVECTOR.empty());
97 if (nINTVECTOR.empty())
98 {
99 return E_FAIL;
100 }
101 RGVECTOR rgVector;
102 //
103 CovertToRangeVector(nINTVECTOR, rgVector);
104
105 //WSTRVECTOR wstrvtUserStringAns;
106 //to string
107 ConvertToWstringVector(rgVector, wstrvtUserString);
108
109 return S_OK;
110}
111
112//
113//HRESULT SimpleGeneratorToUserString(INTVECTOR &nINTVECTOR, WSTRVECTOR &wstrvtUserString)
114int main()
115{
116 INTVECTOR nINTVECTOR;
117 WSTRVECTOR wstrvtUserString;
118 //int a[10] = {1,2,3,5,8,9,10,100};
119 //int a[10] = {1};
120 int a[2] = {1,100};
121 for (int pos = 0; pos < 2; ++pos)
122 {
123 nINTVECTOR.push_back(a[pos]);
124 }
125 SimpleGeneratorToUserString(nINTVECTOR, wstrvtUserString);
126 for (WSTRVECTOR::size_type pos = 0; pos < wstrvtUserString.size(); ++pos)
127 {
128 wstring wstrtemp = wstrvtUserString[pos];
129 wcout<<wstrtemp;
130 //printf("%s",wstrvtUserString[pos]);
131 }
132 system("pause");
133 return 0;
134}
posted on 2011-02-17 14:42
爬 阅读(2037)
评论(0) 编辑 收藏 引用 所属分类:
测试训练