1//
2//  NSMutableNumber.m
3//  dataBoiler
4//
5//  Created by matthew on Wed Sep 18 2002.
6//  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7//
8
9#import "NSMutableNumber.h"
10#import <stdlib.h>
11
12
13@implementation NSMutableNumber
14-(id) init
15{
16    [super init];
17    [self zeroInt];
18    return self;
19}
20-(id) initWithInt: (int) value
21{
22    [super init];
23    _iValue = value;
24    _type = INTTYPE;
25    return self;
26}
27
28-(id) initWithFloat: (float) value
29{
30    [super init];
31    _fValue = value;
32    _type = FLOATTYPE;
33    _exp = 0;
34    _decPlaces = 4;
35    return self;
36
37}
38-(id) initWithString: (NSString *) value
39{
40    [super init];
41    [self setStringValue: value];
42    return self;
43}
44
45-(void) setDecPlaces: (int) value
46{
47    _decPlaces = value;
48}
49
50-(id) initWithFloat: (float) value withDecPlaces: (int) dp
51{
52    [super init];
53    _fValue = value;
54    _type = FLOATTYPE;
55    _decPlaces = dp;
56    return self;
57
58}
59
60-(void) setStringValue: (NSString *) value
61{
62    int n;
63    int i;
64    int fflag, dcnt;
65    int dflag;
66    char c;
67
68   dflag = dcnt= fflag = 0;
69
70    n = [value length];
71
72    for(i = 0; i < n; i++)
73    {
74	c = [value characterAtIndex: i];
75	if(c == '.' || c ==  'e' || c == 'E') fflag = 1;
76	//do the counting before the flag check so it wont ++ for the decimal pt.
77	if(dflag && isdigit(c)) dcnt++;
78	if(c == '.') dflag = 1;
79	if(c == 'e' || c == 'E' || c == ' ') dflag = 0;
80	if(c == 'e' || c == 'E') _exp = 1;
81    }
82
83    if(fflag)
84    {
85	_type = FLOATTYPE;
86	if(dcnt == 0) dcnt = 1;
87	_decPlaces = dcnt;
88	_fValue = [value floatValue];
89    }
90    else
91    {
92	_type = INTTYPE;
93	_iValue = [value intValue];
94    }
95
96
97}
98-(void) add: (NSMutableNumber *) what
99{
100    switch(_type)
101    {
102	case INTTYPE:
103	    if([what type] == INTTYPE)
104		[self addInt: [what intValue]];
105	    else
106	    {
107		_type = FLOATTYPE;
108		_fValue = _iValue + [what floatValue];
109		_decPlaces = [what decPlaces];
110	    }
111	    break;
112
113	case FLOATTYPE:
114	    if([what type] == INTTYPE)
115		[self addInt: [what intValue]];
116	    else
117	    {
118		_fValue += [what floatValue];
119		_decPlaces = ([what decPlaces] < _decPlaces)? [what decPlaces] : _decPlaces;
120	    }
121	    break;
122    }
123}
124
125-(void) addString: (NSString *) what
126{
127    NSMutableNumber *ss;
128    ss = [[NSMutableNumber alloc] initWithString: what];
129    [self add: ss];
130    [ss release];
131}
132
133-(void) toExp
134{
135    _exp = 1;
136}
137
138-(void) fromExp
139{
140    _exp = 0;
141}
142-(void) toInt
143{
144    if(_type == FLOATTYPE)
145    {
146	_type = INTTYPE;
147	_iValue = (int) _fValue;
148    }
149}
150
151-(void) toFloat
152{
153    if(_type == INTTYPE)
154    {
155	_type = FLOATTYPE;
156	_fValue = (int) _iValue;
157    }
158
159}
160
161-(void) negate
162{
163    switch(_type)
164    {
165	case INTTYPE:
166	    _iValue *= -1;
167	    break;
168	case FLOATTYPE:
169	    _fValue *= -1;
170
171	    break;
172	default: ;
173    }
174}
175
176-(void) zeroInt
177{
178    _type = INTTYPE;
179    _iValue = 0;
180    _exp = 0;
181}
182
183-(void) zeroFloat
184{
185    _type = FLOATTYPE;
186    _fValue = 0.0;
187    _decPlaces = 4;
188}
189
190-(void) addInt: (int) what
191{
192    switch(_type)
193    {
194	case INTTYPE: _iValue += what;
195	    break;
196	case FLOATTYPE:
197	    _fValue += what;
198	    break;
199	default: ;
200    }
201}
202
203/*
204-(void) template: (float) what
205{
206    switch(_type)
207    {
208	case INTTYPE:
209	    break;
210	case FLOATTYPE:
211	    	    break;
212	default: ;
213    }
214}
215*/
216-(void) addFloat: (float) what
217{
218    switch(_type)
219    {
220	case INTTYPE:
221	    _type = FLOATTYPE;
222	    _fValue = _iValue + what;
223	    _decPlaces = 4;
224	    break;
225	case FLOATTYPE:
226	    _fValue += what;
227	    break;
228	default: ;
229    }
230}
231
232-(NSString *) description
233{
234    NSMutableString *rString, *fString;
235
236    switch(_type)
237    {
238	case INTTYPE:
239		rString = [NSString stringWithFormat: @"%d", (long) _iValue];
240	    break;
241	case FLOATTYPE:
242		fString = [[NSMutableString alloc] init];
243		[fString ADDCHAR('\%')];
244		if(_exp)
245		    [fString appendString: [NSString stringWithFormat: @"0.%dg", _decPlaces]];
246		else
247		    [fString appendString: [NSString stringWithFormat: @"0.%df", _decPlaces]];
248		rString = [NSString stringWithFormat: fString, (double) _fValue];
249		[fString release];
250	    	    break;
251    }
252    return rString;
253
254}
255
256-(void) inc
257{
258    [self addInt: 1];
259}
260
261-(void) dec
262{
263    [self addInt: -1];
264}
265
266
267
268//accessors
269-(int) type
270{
271    return _type;
272}
273-(int) decPlaces
274{
275    return _decPlaces;
276}
277
278-(float) floatValue
279{
280    return _fValue;
281}
282
283-(int) intValue
284{
285    return _iValue;
286}
287-(void) setInt: (int) value
288{
289    _iValue = value;
290    _type = INTTYPE;
291}
292
293-(void) setFloat: (float) value
294{
295    _fValue = value;
296    _type = FLOATTYPE;
297    _decPlaces = 4;
298    _exp = 0;
299
300}
301
302-(void) setFloat: (float) what decPlaces: (int) howMany
303{
304    _fValue = what;
305    _type = FLOATTYPE;
306    _decPlaces = howMany;
307    _exp = 0;
308
309}
310@end
311