-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpIntClassIO.cpp
More file actions
88 lines (73 loc) · 2.52 KB
/
Copy pathpIntClassIO.cpp
File metadata and controls
88 lines (73 loc) · 2.52 KB
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
/*
Copyright © 2024 Claus Vind - Andreasen
This program is free software; you can redistribute it and /or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 - 1307 USA
This General Public License does not permit incorporating your program into proprietary programs.If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library.
If this is what you want to do, use the GNU Library General Public License instead of this License.
*/
#include <iostream>
#ifndef OS_WINDOWS
#include <algorithm>
#endif
#include "pIntClass.h"
pIntClass::pIntClass(const std::string& x)
{
value.clear();
value.reserve(ReservationSize);
int index1 = 0;
int sign = 1; //default positive
while (index1 < x.length() && isspace(x[index1])) index1++;
if (x[index1] == '-') {
sign = -1;
index1++;
}
int index2 = index1;
while (index2 < x.length() && isdigit(x[index2])) index2++;
if (index2 == index1)
std::cerr << "unknown format: " << x << std::endl;
// we assume we have something number like
value.push_back(0);
while (index1 < x.length() && isdigit(x[index1])) {
mul10();
value[0] += x[index1] - '0';
index1++;
}
normalize(value);
for (int i = 0; i < value.size(); i++) value[i] *= sign;
}
std::string pIntClass::ToString()
{
#define FORMATSTRING "%09d"
#define DIGITS 9
if (value.size() == 0) {
std::string* s = new std::string();
s->append("0");
return *s;
}
else {
char buffer[12];
std::string s;// = new std::string();
int sign = value.back();
for (int i = 0; i < value.size(); i++) {
sprintf(buffer, FORMATSTRING, (sign<0 )? -value[i] : value[i]);
char* c1 = buffer;
char* c2 = buffer + DIGITS - 1;
while (c1 < c2)
{
char t = *c1;
*c1 = *c2;
*c2 = t;
c1++; c2--;
}
s.append(buffer);
}
while (s.size() && (s.back() == '0')) s.pop_back();
if (s.size() == 0)
s.append("0");
else
if (sign < 0) s.append("-");
std::reverse(s.begin(), s.end());
return s;
}
}