Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2463.doc
Скачиваний:
3
Добавлен:
01.05.2025
Размер:
387 Кб
Скачать
☆

Int p_number_class::getDigitByPosition(int pos) {

if(pos >= strlen(number)) return -1;

switch(number[pos]) {

case '0' : return 0;

case '1' : return 1;

case '2' : return 2;

case '3' : return 3;

case '4' : return 4;

case '5' : return 5;

case '6' : return 6;

case '7' : return 7;

case '8' : return 8;

case '9' : return 9;

case 'A' : return 10;

case 'B' : return 11;

case 'C' : return 12;

case 'D' : return 13;

case 'E' : return 14;

case 'F' : return 15;

case 'a' : return 10;

case 'b' : return 11;

case 'c' : return 12;

case 'd' : return 13;

case 'e' : return 14;

case 'f' : return 15;

default : return -1;

}

}

Int p_number_class::transferTo10() {

int result = 0;

int len = strlen(number);

for (int index = 0; index < len; index++) {

int digit = getDigitByPosition(index);

if(digit != -1) {

result = result+digit*Power(scale_of_notation,len-index-1);

}

}

return result;

}

Void p_number_class::transferFrom10(int numb) {

int dividend = numb;

int index = 0;

char revers[200];

// используется операция деления в столбик

while(dividend >= scale_of_notation) {

int rest = dividend%scale_of_notation;

revers[index] = getCharByDigit(rest);

index++;

dividend = dividend/scale_of_notation;

}

revers[index] = getCharByDigit(dividend);

index++;

revers[index] = '\0';

// переворачиваем результат

for (int ind = 0; ind < index; ind++) {

number[ind] = revers[index - ind - 1];

}

number[index] = '\0';

}

Создание объектов класса и использование объектов класса. Для демонстрации созданных типов в методе main() объявляем переменные и вызываем методы.

Int main(int argc, char* argv[])

{

P_number_class a; // конструктор без аргументов (по умолчанию)

P_number_class b(“4”,5); // конструктор инициализации

P_number_class c(b); // конструктор копирования

cout << a;

cout << b;

cout << c;

P_number_class d = a + b;

cout << d;

P_number_class e = c - a;

cout << e;

P_number_class g = b*c;

cout << g;

P_number_class h = d/e;

cout << h;

cout << "\n" << (b == c);

cout << "\n" << (h > g);

cout << "\n" << (h < g);

int l;

cin >> l;

return 0;

}

Результат

Библиографический список

  1. Павловская Т. А., Щупак Ю. А. C++. Объектно-ориентированное программирование: Практикум. – СПб.: Питер, 2006. – 265 с.

  2. Лаптев В. В., Морозов А. В., Бокова А. В. С++. Объектно-ориентированное программирование: Задачи и упражнения. – М., 2007.

  3. Йордон Э., Аргила К. Объектно-ориентированный анализ и проектирование систем. – М., 2007. – 284 с.

28

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]