/
Number.cpp
73 lines (65 loc) · 1.51 KB
/
Number.cpp
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
/*
**
** Number.cpp
** Copyright by Jan Poeschko
**
*/
#include "StdAfx.h"
#include "Number.h"
#include "Integer.h"
#include "Real.h"
Number::Number()
{
}
Number::~Number()
{
}
IntegerType Number::ReadInteger(Input &input)
{
IntegerType intValue(0);
// Process part before decimal separator
while(input.Current() >= '0' && input.Current() <= '9')
{
intValue = intValue * 10 + (input.Current() - '0');
input.Next();
}
return intValue;
}
NumberPtr Number::Read(Input &input)
{
Integer ten(10);
auto_ptr<Integer> intValue(new Integer);
while(input.Current() >= '0' && input.Current() <= '9')
{
Integer digit(input.Current() - '0');
intValue = intValue->Times(&ten)->Plus(&digit);
input.Next();
}
// Process part after decimal separator
if(input.Current() == '.')
{
// Cast the current value to float (just to prevent compiler warning)
auto_ptr<MachineReal> realValue(new MachineReal(intValue));
input.Next();
auto_ptr<MachineReal> factor(new MachineReal(1.0));
MachineReal tenth(0.1);
while(input.Current() >= '0' && input.Current() <= '9')
{
MachineReal digit(input.Current() - '0');
factor = factor->Times(&tenth);
realValue = realValue->Plus(factor->Times(&digit).get());
input.Next();
}
return NumberPtr(realValue);
}
else
return NumberPtr(intValue);
}
NumberPtr Number::Sin()
{
throw NumberException("Not implemented yet.");
}
NumberPtr Number::Cos()
{
throw NumberException("Not implemented yet.");
}