blob: ee49f501425a9e65d695fe1f81192bf69fe895d2 (
plain)
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
|
/////////////////////////////////////////////////////////////////////////
// ftoa.cpp
//
// Copyright (c) 1996-2003 Bryce W. Harrington [bryce at osdl dot org]
//
//-----------------------------------------------------------------------
// License: This code may be used by anyone for any purpose
// so long as the copyright notices and this license
// statement remains attached.
//-----------------------------------------------------------------------
//
// This routine converts an integer into a string
//
/////////////////////////////////////////////////////////////////////////
// Standard include files
#include <algorithm>
#include <string> // for string
using namespace std;
string itos(int n)
{
int sign;
string s;
if ((sign = n) < 0) // record sign
n = -n; // make n positive
do { // generate digits in reverse order
s += (char(n % 10) + '0'); // get next digit
} while ((n/=10) > 0); // delete it
if (sign < 0)
s += '-';
reverse(s.begin(), s.end()); // This is what the code should look like
// if the string class is compatible with
// the standard C++ string class
#ifdef DUMB_OS_LIKE_WINDOWS
// In Windows, we'll use this hack...
for (int i=0, j=s.GetLength()-1; i<j; i++, j--)
{
char c = s[i];
// s[i] = s[j];
// s[j] = c;
s.SetAt(i, s[j]);
s.SetAt(j, c);
}
#endif
return s;
}
string ultos(unsigned long n)
{
string s;
do { // generate digits in reverse order
s += (char(n % 10) + '0'); // get next digit
} while ((n/=10) > 0); // delete it
reverse(s.begin(), s.end()); // This is what the code should look like
// if the string class is compatible with
// the standard C++ string class
#ifdef DUMB_OS_LIKE_WINDOWS
// In Windows, we'll use this hack...
for (int i=0, j=s.GetLength()-1; i<j; i++, j--)
{
char c = s[i];
// s[i] = s[j];
// s[j] = c;
s.SetAt(i, s[j]);
s.SetAt(j, c);
}
#endif
return s;
}
|