- •Мгул, пм-21, 4 семестр, 2012 год
- •Контрольная работа №2
- •Class Command (Calculator) (лаб. Раб. №3)
- •Задание * (лаб. Раб. №4)
- •Дать описание классов, созданных в файлах: Array.H, Array.Cpp
- •12.1. Исходный код файла Array.H (лаб. Раб. №5)
- •12.2. Исходный код файла Array.Cpp (лаб. Раб. №6)
- •Выполнить задание (лаб. Раб. №7)
- •Дать описание классов проекта Kolobok («Колобок») (лаб. Раб. №8)
- •В проект Kolobok добавить персонаж RedCup («Красная шапочка») (лаб. Раб. №9)
Контрольная работа №2
//Разобраться в программе, дописать методы класса My_class
class My_class
{
public:
char* s; // не забудьте отвести память для строки s
My_class() // проинициализировать s пустой строкой “”
{
}
My_class(int i) // проинициализировать s строкой из i точек
{
}
My_class(char* s) // проинициализировать s содержимым аргумента
{
}
My_class(My_class& obj) // конструктор копирования
{
}
My_class& operator =(My_class& obj) // оператор присваивания
{
}
void print()
{
cout << strlen(s) <<” “ << s << endl;
}
};
void main() // в строках с вызовом метода print() заполнить
// комментарий текстом, выводимым на экран
{
My_class obj1; obj1.print(); //
char* s = ”text”;
My_class obj2(strlen(s)); obj2.print(); //
strcpy(obj2.s,s); obj2.print(); //
obj1 = obj2; obj1.print(); //
My_class obj3(s); obj3.print(); //
My_class obj4(obj3); obj4.print(); //
}
Группа ПМ-21 Студент _____________________
Оценка _____________________
class Date -> Time (лаб. раб. №2)
// Задание
// 1. Разобраться в программе, дописать методы класса Date
// 2. Написать класс Time (производный от класса Date) для хранения (день, месяц, год, часы, минуты, секунды, миллисекунды) так, чтобы программа заработала при открытии комментариев в функции main(...)
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
class Date {
public:
int day, month, year;
Date()
{
cout << "Date()\n";
SYSTEMTIME st;
GetSystemTime(&st);
day = st.wDay;
month = st.wMonth;
year = st.wYear;
}
Date(Date &d)
{
…
}
Date(int d, int m, int y)
{
…
}
Date &operator =(Date &d)
{
…
}
bool visokostniy();
};
ostream &operator <<(ostream &out, Date d)
{
…
}
bool Date::visokostniy()
{
…
}
void PrintBytes(void *p, int size)
{
unsigned char *p1 = (unsigned char *)p;
for(int i=0;i<size;i++)
cout << (int)p1[i] << " ";
cout << endl;
}
int main()
{
cout << "Creating Date...\n";
Date obj1(1, 1, 2008);
// cout << "Creating Time...\n";
// Time obj2;
cout << "obj1: " << obj1 << " " << sizeof(obj1) << endl;
// cout << "obj2: " << obj2 << " " << sizeof(obj2) << endl;
PrintBytes(&obj1, sizeof(obj1));
// PrintBytes(&obj2, sizeof(obj2));
_getch();
return 0;
}
Class Command (Calculator) (лаб. Раб. №3)
// Задание
// 1. Разобраться в программе
// 2. Написать класс Read для чтения числа с клавиатуры в ячейку памяти
// 3. Написать код main(…), реализующий операции для вычисления выражения
// c = a + (d * e + 1)^2 / (d * e - 1)^2,
// используя минимальное количество операций и ячеек памяти
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
const int NumbersCount = 10;
const int CommandsCount = 20;
class Command {
public:
virtual void exec(int *M) = 0;
virtual ~Command() {}
};
class Plus : public Command
{
int arg1, arg2, res;
public:
Plus(int arg1, int arg2, int res)
{
this->arg1 = arg1;
this->arg2 = arg2;
this->res = res;
}
virtual void exec(int *M)
{
M[res] = M[arg1] + M[arg2];
}
};
class PlusI : public Command
{
int arg1, arg2, res;
public:
PlusI(int arg1, int arg2, int res)
{
this->arg1 = arg1;
this->arg2 = arg2;
this->res = res;
}
virtual void exec(int *M)
{
printf("M[a1] = %i; a2 = %i; ", M[arg1],arg2);
M[res] = M[arg1] + arg2;
printf("M[res] = %i;\n", M[res]);
}
};
class MinusI : public Command
{
int arg1, arg2, res;
public:
MinusI(int arg1, int arg2, int res)
{
this->arg1 = arg1;
this->arg2 = arg2;
this->res = res;
}
virtual void exec(int *M)
{
printf("M[a1] = %i; a2 = %i; ", M[arg1],arg2);
M[res] = M[arg1] - arg2;
printf("M[res] = %i;\n", M[res]);
}
};
class Mul : public Command
{
int arg1, arg2, res;
public:
Mul(int arg1, int arg2, int res)
{
this->arg1 = arg1;
this->arg2 = arg2;
this->res = res;
}
virtual void exec(int *M)
{
M[res] = M[arg1] * M[arg2];
}
};
class Div : public Command
{
int arg1, arg2, res;
public:
Div(int arg1, int arg2, int res)
{
this->arg1 = arg1;
this->arg2 = arg2;
this->res = res;
}
virtual void exec(int *M)
{
M[res] = M[arg1] / M[arg2];
}
};
class Dig : public Command
{
int arg1, arg2, res;
public:
Dig(int arg1, int arg2, int res)
{
this->arg1 = arg1;
this->arg2 = arg2;
this->res = res;
}
virtual void exec(int *M)
{
printf("M[a1] = %i; M[a2] = %i; ", M[arg1],M[arg2]);
M[res] = (int)pow((double)M[arg1], (double)M[arg2]);
printf("M[res] = %i;\n", M[res]);
}
};
class Print : public Command
{
int arg;
public:
Print(int arg)
{
this->arg = arg;
}
virtual void exec(int *M)
{
printf("M[%i] = %i;\n", arg, M[arg]);
}
};
int main(int argc, char **argv)
{
Command *C[CommandsCount];
int M[NumbersCount];
int n = 0;
// c = a + (d * e + 1)^2 / (d * e - 1)^2
C[n++] = new Read(1); //a
C[n++] = new Read(2); //d
C[n++] = new Read(3); //e
C[n++] = new Mul(2, 3, 2); //d*e in d
C[n++] = new PlusI(2, 1, 3); //d*e+1 in e
C[n++] = new MinusI(2, 1, 4); //d*e-1 in temp
C[n++] = new Div(3, 4, 0); //(d*e+1)/(d*e-1)
C[n++] = new Dig(0, 2, 0); // ((d*e+1)/(d*e-1))^2 in c
C[n++] = new Plus(0, 1, 0);
C[n++] = new Print(0);
for (int i = 0; i < n; i++)
C[i]->exec(M);
for (int i = 0; i < n; i++)
delete C[i];
_getch();
return 0;
}
