-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifference.cpp
More file actions
183 lines (153 loc) · 5.25 KB
/
difference.cpp
File metadata and controls
183 lines (153 loc) · 5.25 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "difference.hpp"
ll convertBaseNDigitToDigit(char d, int base) {
ll result = NAN;
//If d is from '0' to '9' in ASCII
if (d >= 48 && d <= 57) {
result = (ll)(d - 48);
}
//If d is from 'A' to 'Z' in ASCII
else if (d >= 65 && d <= 90) {
result = (ll)(d - 55);
}
//If d is from 'a' to 'z' in ASCII
else if (d >= 97 && d <= 122) {
result = (ll)(d - 87);
}
//If d is the '-' character
else if (d == 45) {
return 45;
}
//If d is not from any range listed above
else result = LLONG_MAX;
//If the value of d is larger than or equal to the original base, then the input is invalid.
if (result >= base) {
cout << "Function 'convertBaseNDigitToDigit':" << endl;
cout << "Error: Invalid input since " << d << " is out of the base " << base << " set." << endl;
return -1;
}
else {
return result;
}
}
char convertDecimalToBaseNDigit(int dec, int base) {
//If the decimal value is larger than the base, then we return the NULL value of char.
if (dec >= base) {
cout << "Function 'convertDigitToBaseNDigit': " << endl;
cout << "Error: Cannot convert decimal value to base-n digit since the decimal value is larger than the target base." << endl;
return 0;
}
else {
if (dec >= 0 && dec <= 9) {
return (char)(dec + 48);
}
//If the decimal value
else if (dec >= 10 && dec <= 35) {
return (char)(dec + 55);
}
//If the decimal value is invalid
else {
cout << "Function 'convertDigitToBaseNDigit': " << endl;
cout << "Error: Cannot convert decimal value to base-n digit since the decimal value is invalid - less than 0 or larger than 35." << endl;
return 0;
}
}
}
ll convertValueToDecimal(string n, int base) {
ll dec = 0;
int power = 0;
//Loop from the last digit to the first digit
for (long long i(n.length() - 1); i >= 0; --i) {
//Get the value of the current digit in decimal
ll curr_digit_val = convertBaseNDigitToDigit(n[i], base);
//If the first value is the '-' sign
if (i == 0 && curr_digit_val == 45) {
return -dec;
}
//If the value of d is larger than or equal to the original base, then the input is invalid.
if (curr_digit_val == -1) {
cout << "Function 'convertValueToDecimal':" << endl;
cout << "Error: Cannot convert to decimal since the input is invalid." << endl;
return -1;
}
//For each digit, we multiply it to the base to the power and add the total value to the dec variable.
dec += (ll)(curr_digit_val * pow(base, power));
//Increase the power
++power;
}
return dec;
}
string convertDecimalToBaseN(ll dec, int base) {
//Create the result string
string result = "";
//Create the remainder variable
ll temp = abs(dec);
int remainder = base;
//If temp is 0
if (temp == 0) return "0";
while (temp != 0) {
//Calculate the remainder
remainder = temp % base;
//Convert the remainder to the appropriate base
char remainder_char = convertDecimalToBaseNDigit(remainder, base);
if (remainder_char == 0) {
cout << "Function 'convertDecimalToBaseN':" << endl;
cout << "Error: Cannot convert decimal value to base-n value since the decimal value is invalid." << endl;
return 0;
}
//Add the remainder_char to the result string
result = remainder_char + result;
//Update the temp variable
temp /= base;
}
if (dec < 0) {
result = "-" + result;
}
return result;
}
string difference(string x, string y, int base) {
Value a(x), b(y);
ll diff;
//Step 1: Calculate a.dec and b.dec base on a.str and b.str
a.dec = convertValueToDecimal(a.str, base);
b.dec = convertValueToDecimal(b.str, base);
//Step 2: If a and b is invalid
if (a.dec == -1 || b.dec == - 1) {
return "NaN";
}
//Step 3: Calculate the diff
diff = a.dec - b.dec;
return convertDecimalToBaseN(diff, base);
}
/*
Compile multiple C++ file in Visual Studio Code
Step 1: Open terminal using Ctrl + J or Cmd + J
Step 2: Use the cd command to move to the directory containing the files
Step 3: Enter 'g++ main.cpp difference.cpp -o main
Step 4: Enter ./main and and start using
NOTES: To re-compile, you just need to do the step 3 again if you're still at the directory as before.
*/
/*
SAMPLE OF 'main.cpp' FILE
//Test the difference function
#include "difference.h"
int main() {
string a, b;
int base;
cout << "Input base: ";
cin >> base;
cout << "Input number a: ";
cin >> a;
cout << "Input number b: ";
cin >> b;
string result = difference(a, b, base);
if (result == "NaN") {
cout << "Function 'difference':" << endl;
cout << "Error: Cannot find the difference since the input is invalid." << endl;
return 0;
}
else {
cout << "(|a - b|)_" << base << " = " << result << "_" << base << endl;
}
return 0;
}
*/