-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListToDoubleConverter.cpp
57 lines (44 loc) · 1.09 KB
/
ListToDoubleConverter.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
#include "ListToDoubleConverter.h"
#include <stdlib.h>
#include <math.h>
double ConvertCharListToDouble(LinkedList<char>* list) {
LinkedList<int>* leftSide = new LinkedList<int>();
LinkedList<int>* rightSide = new LinkedList<int>();
bool hitADot = false;
list->MoveFirst();
while (!list->AtEnd()) {
if (list->GetCurrent() == '.') {
if (hitADot) {
exit(-1);
}
hitADot = true;
}
else if(list->GetCurrent() >= '0' && list->GetCurrent() <= '9') {
if (!hitADot) {
leftSide->Append(list->GetCurrent() - '0');
}
else {
rightSide->Append(list->GetCurrent() - '0');
}
}
list->MoveNext();
}
double value = 0;
leftSide->MoveFirst();
int leftSidePower = leftSide->Length() - 1;
while (!leftSide->AtEnd()) {
value += leftSide->GetCurrent() * pow(10, leftSidePower);
leftSidePower--;
leftSide->MoveNext();
}
rightSide->MoveFirst();
int rightSidePower = 1;
while (!rightSide->AtEnd()) {
value += rightSide->GetCurrent() * pow(10, -rightSidePower);
rightSidePower++;
rightSide->MoveNext();
}
delete leftSide;
delete rightSide;
return value;
}