Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- class Integer {
- private:
- int base;
- string number;
- public:
- // конструктор
- Integer(int base, string number) {
- this->base = base;
- this->number = number;
- }
- // деструктор
- ~Integer() {}
- // getters
- int getBase() {
- return base;
- }
- string getNumber() {
- return number;
- }
- // сеттери
- void setBase(int base) {
- this->base = base;
- }
- void setNumber(string number) {
- this->number = number;
- }
- // конвертацiя в десяткову систему числення
- int toDecimal() {
- int decimal = 0;
- int power = 0;
- for (int i = number.length() - 1; i >= 0; i--) {
- int digit = 0;
- if (isdigit(number[i])) {
- digit = number[i] - '0';
- } else {
- digit = number[i] - 'A' + 10;
- }
- decimal += digit * pow(base, power);
- power++;
- }
- return decimal;
- }
- // конвертацiя з десяткової системи числення
- void fromDecimal(int decimal) {
- string result = "";
- while (decimal > 0) {
- int remainder = decimal % base;
- if (remainder < 10) {
- result = to_string(remainder) + result;
- } else {
- result = char(remainder - 10 + 'A') + result;
- }
- decimal /= base;
- }
- number = result;
- }
- };
- int main() {
- Integer num(16, "7FF");
- cout << "Base: " << num.getBase() << endl;
- cout << "Number: " << num.getNumber() << endl;
- cout << "Decimal: " << num.toDecimal() << endl;
- num.setBase(2);
- num.fromDecimal(2047);
- cout << "Base: " << num.getBase() << endl;
- cout << "Number: " << num.getNumber() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment