
- •Классы и объекты
- •Введение
- •Лабораторная работа № 1. Структура-пара
- •Выполнение лабораторной работы
- •Методические указания к выполнению лабораторной работы
- •XXX yyy; // скалярная переменная
- •Vl, v2[20]; // скалярная переменная и массив
- •Int _tmain(int argc, _tchar* argv[])
- •X.Read();
- •X.Display(); // вывод полей на экран
- •Лабораторная работа № 2. Структуры и классы
- •Выполнение лабораторной работы
- •Методические указания к выполнению лабораторной работы
- •Int getDigitByPosition(p_number V,int pos) {
- •Int transferTo10(p_number V) {
- •Int p_number_class::getDigitByPosition(int pos) {
- •Int p_number_class::transferTo10() {
- •Void p_number_class::transferFrom10(int numb) {
- •Int main(int argc, char* argv[])
- •Библиографический список
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 a,b;
read(a);
read(b);
display(a);
display(b);
add(a,b);
display(a);
sub(a,b);
display(a);
mul(a,b);
display(a);
div(a,b);
display(a);
cout << equal(a,b);
cout << greate(a,b);
cout << less(a,b);
P_number_class c,d;
c.read();
d.read();
c.display();
d.display();
cout << "\n sum \n";
c.add(d);
c.display();
cout << "\n sub \n";
c.sub(d);
c.display();
cout << "\n mul \n";
c.mul(d);
c.display();
cout << "\n div \n";
c.div(d);
c.display();
cout << "\n" << " equal ";
cout << "\n" << c.equal(d);
cout << "\n" << " greate ";
cout << "\n" << c.greate(d);
cout << "\n" << " less ";
cout << "\n" << c.less(d);
int l;
cin >> l;
return 0;
}
Проверяем работу программы.
Результаты работы программы совпадают с ожидаемыми результатами – программа работает правильно.
Библиографический список
C++. Объектно-ориентированное программирование : Практикум / Т. А. Павловская, Ю. А. Щупак. – СПб. : Питер, 2006. – 265 с.
С++. Объектно-ориентированное программирование. Задачи и упражнения / В. В. Лаптев, А. В. Морозов, А. В. Бокова. – 2007.
Объектно-ориентированный анализ и проектирование систем / Эдвард Йордон, Карл Аргила. – 2007. – 284 с.