-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathoctal_to_decimal.dart
More file actions
70 lines (59 loc) · 1.84 KB
/
octal_to_decimal.dart
File metadata and controls
70 lines (59 loc) · 1.84 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
import "dart:math" show pow;
import 'package:test/test.dart';
// octal number to decimal number conversion
//
// function to take oct string value and return decmal string value
String ocatal_to_decimal(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}
// negative number check
bool is_negative = oct_val[0] == "-";
if (is_negative) oct_val = oct_val.substring(1);
int oct;
try {
oct = int.parse(oct_val);
} catch (e) {
throw new FormatException("An invalid value was passed to the function");
}
// checking number not valid for octal is passed(0-7)
for (int i = 0; i < oct_val.length; i++) {
if (!(int.parse(oct_val.substring(i, i + 1)) < 8)) {
throw new FormatException("An invalid value was passed to the function");
}
;
}
// converting octal to decimal
int dec_val = 0, i = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i).toInt());
i++;
oct = oct ~/ 10;
}
// returning the value
if (is_negative) {
return "-" + dec_val.toString();
}
return dec_val.toString();
}
// driver function
void main() {
// test cases for different input
test("ocatal_to_decimal 75", () {
expect(ocatal_to_decimal("75"), equals("61"));
});
test("ocatal_to_decimal -62", () {
expect(ocatal_to_decimal("-62"), equals("-50"));
});
test("ocatal_to_decimal rasies error when number is invalid", () {
expect(() => ocatal_to_decimal("84"), throwsFormatException);
});
test("ocatal_to_decimal rasies error when number is invalid", () {
expect(() => ocatal_to_decimal("as23"), throwsFormatException);
});
test("ocatal_to_decimal of empty string raises error", () {
expect(() => ocatal_to_decimal(""), throwsFormatException);
});
}