-
Notifications
You must be signed in to change notification settings - Fork 6
/
octal.cpp
84 lines (72 loc) · 2.7 KB
/
octal.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
74
75
76
77
78
79
80
81
82
83
#include <string>
#include <sstream>
#include <bitset>
#include <stdexcept>
using namespace std;
#define BITSET_VAL 16 // Assuming 8 bits for bitset value
namespace mlb {
// Function to convert decimal to octal
string decimalToOctal(int &decimalNumber) {
std::stringstream ss;
ss << std::oct << decimalNumber;
return ss.str();
}
// Function to convert octal to decimal
int octalToDecimal(string& octal) {
int decimalNumber = stoi(octal, nullptr, 8);
return decimalNumber;
}
// Function to add two octal numbers
string octalAddition(string& octal1, string& octal2) {
int decimal1 = octalToDecimal(octal1);
int decimal2 = octalToDecimal(octal2);
int sum = decimal1 + decimal2;
return decimalToOctal(sum);
}
// Function to subtract two octal numbers (octal1 - octal2)
string octalSubtraction(string& octal1, string& octal2) {
int decimal1 = octalToDecimal(octal1);
int decimal2 = octalToDecimal(octal2);
int difference = decimal1 - decimal2;
return decimalToOctal(difference);
}
// Function to multiply two octal numbers
string octalMultiplication(string& octal1, string& octal2) {
int decimal1 = octalToDecimal(octal1);
int decimal2 = octalToDecimal(octal2);
int product = decimal1 * decimal2;
return decimalToOctal(product);
}
// Function to divide two octal numbers (integer division, returns quotient)
string octalDivision(string& octal1, string& octal2) {
int decimal1 = octalToDecimal(octal1);
int decimal2 = octalToDecimal(octal2);
if (decimal2 == 0) {
throw invalid_argument("Division by zero is not allowed.");
}
int quotient = decimal1 / decimal2;
return decimalToOctal(quotient);
}
string octalAddition(int& decimal1, int& decimal2) {
int sum = decimal1 + decimal2;
return decimalToOctal(sum);
}
// Function to subtract two octal numbers (octal1 - octal2)
string octalSubtraction(int& decimal1, int& decimal2) {
int difference = decimal1 - decimal2;
return decimalToOctal(difference);
}
// Function to multiply two octal numbers
string octalMultiplication(int& decimal1, int& decimal2) {
int product = decimal1 * decimal2;
return decimalToOctal(product);
}
// Function to divide two octal numbers (integer division, returns quotient)
string octalDivision(int& decimal1, int& decimal2) {
if (decimal2 == 0) {
throw invalid_argument("Division by zero is not allowed.");
}
int quotient = decimal1 / decimal2;
return decimalToOctal(quotient);
}
};