Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
Текст программы:
#include <Windows.h>
#include <stdio.h>
HANDLE std_out;
typedef class Rectangle {
private:
float x;
float y;
float x2;
float y2;
char chin;
char chout;
public:
Rectangle() {
}
Rectangle(float x, float y, float x2, float y2) {
setX(x);
setY(y);
setX2(x2);
setY2(y2);
}
void setX(float x) {
if (x > 0.1 && x < 20.1)
this->x = x;
}
void setY(float y) {
if (y > 0.1 && y < 20.1)
this->y = y;
}
void setX2(float x2) {
if (x2 > 0.1 && x2 < 20.1)
this->x2 = x2;
}
void setY2(float y2) {
if (y2 > 0.1 && y2 < 20.1)
this->y2 = y2;
}
float lenght() {
return x + x2;
}
float width() {
return y + y2;
}
float perimetr() {
return (lenght() + width()) * 2;
}
float area() {
return lenght() * width();
}
bool square() {
return lenght() == width() ? true : false;
}
void setFillCharacter(char chin) {
this->chin = chin;
}
void setPerimeterCharacter(char chout) {
this->chout = chout;
}
void draw() {
int xx = 5;
int yy = 5;
int xx2 = 15;
int yy2 = 10;
COORD pos;
pos.X = xx;
pos.Y = yy;
SetConsoleCursorPosition(std_out, pos);
for (int j = yy; j < yy2; j++) {
pos.Y = j;
for (int i = xx; i < xx2; i++) {
pos.X = i;
SetConsoleCursorPosition(std_out, pos);
printf("%c", chin);
}
}
pos.X = xx;
pos.Y = yy;
SetConsoleCursorPosition(std_out, pos);
for (int i = xx; i < xx2; i++) {
pos.X++;
SetConsoleCursorPosition(std_out, pos);
printf("%c", chout);
}
for (int i = yy; i < yy2; i++) {
pos.Y++;
SetConsoleCursorPosition(std_out, pos);
printf("%c", chout);
}
for (int i = xx2; i > xx; i--) {
pos.X--;
SetConsoleCursorPosition(std_out, pos);
printf("%c", chout);
}
for (int i = yy2; i > yy; i--) {
pos.Y--;
SetConsoleCursorPosition(std_out, pos);
printf("%c",chout);
}
}
void print() {
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(std_out, pos);
for (int i = x; i < x2; i++) {
pos.X++;
SetConsoleCursorPosition(std_out, pos);
printf(".");
}
int main(){
std_out = GetStdHandle(STD_OUTPUT_HANDLE);
Rect ss(1,1,20,20);
ss.setFillCharacter('x');
ss.setPerimeterCharacter('A');
system("cls");
ss.draw();
ss.print();
COORD out = { 0,0 };
SetConsoleCursorPosition(std_out, out);
system("pause");
return 0;
}
Результаты:
Лабораторная работа №2.
Тема: КОНСТРУКТОРЫ И ДЕСТРУКТОРЫ
5 - вариант
Задание:
Одномерный массив в С – полезный и эффективный тип. Однако
использование его может приводить к возникновению ошибок. Частой
ошибкой является попытка обращения к элементам, которые находятся
за пределами массива. С++ справляется с этой проблемой с помощью
определения типа, аналогичного массиву, в котором пределы могут
быть проверены. Напишите конструктор по умолчанию, динамически
распределяющий 10 элементов массива; конструктор, принимающий в
качестве параметра количество размещаемых элементов. Также
необходим деструктор удаляющий все распределенные элементы
массива. Реализуйте следующие функции:
int element(int i) – для возвращения i-го элемента массива;
int ub() – для возвращения текущей верхней границы массива.
Текст программы:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
class LikeMassive {
private:
int n;
int* mass;
public:
LikeMassive(int n) {
this->n = n;
mass = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
mass[i] = i;
}
}
~LikeMassive() {
delete[] mass;
}
int element(int i) {
int ret = -1;
if (i < n) {
ret = mass[i];
}
return ret;
}
int ub() {
return mass[n];
}
};
int main(){
LikeMassive lkm(10);
int i=0;
while (1) {
printf("Enter what element u want(i):");
scanf("%d", &i);
printf("Massive have element:%d\n", lkm.element(i));
}
return 0;
}
Результаты:
Лабораторная работа №3.
Тема: НАСЛЕДОВАНИЕ
5 - вариант
Задание:
Создать класс «студент» и его класс-наследник «группа
студентов» (вывести, изменить, отсортировать информацию).
Текст программы:
class Student {
protected:
char lname[15];
char name[15];
char fname[15];
char date[15];
int srmark;
};
int main() {
int n;
char s[15];
Group a[24];
printf("students N(<24):");
scanf_s("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter last name:");
scanf("%s", &s);
a[i].setLname(s);
printf("Enter name:");
scanf("%s", &s);
a[i].setname(s);
printf("Enter first name:");
scanf("%s", &s);
a[i].setfname(s);
printf("Enter birthday like dd/mm/yyyy:");
scanf("%s", &s);
a[i].setdate(s);
printf("Enter sr. mark:");
int x = 0;
scanf("%d", &x);
a[i].setsrmark(x);
}
printf("\nYou entered:\n");
for(int i = 0; i < n; i++) {
a[i].print();
printf("\n");
}
printf("\nSorted:\n");
Group* aSorted = a[0].sorting(a, n);
for (int i = 0; i < n; i++) {
aSorted[i].print();
printf("\n");
}
printf("\nEdit last name:\n");
a[0].setLname("HelloWorld");
a[0].print();
printf("\n");
system("pause");
return 0;
}
Результаты:
Лабораторная работа №4.
Тема: ПОЛИМОРФИЗМ. ПЕРЕГРУЗКА ОПЕРАЦИЙ И ФУНКЦИЙ
5 - вариант
Задание:
Ввести класс для работы с прямоугольной матрицей. Реализовать
операции:
а) сложение двух матриц (операция +);
б) умножение двух матриц (операция *).
Членом класса сделать функцию printf() для вывода конечной
матрицы и ее модуля.
Текст программы:
#include <stdio.h>
#include <Windows.h>
class Matrix {
private:
int cols;
int rows;
public:
int** matrix;
Matrix(int rows, int cols) {
setCols(cols);
setRows(rows);
matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
}
void MatrixGenerate() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = rand() % 9;
}
}
}
void MatrixOneValue(int value) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = value;
}
}
}
void setCols(int cols) {
this->cols = cols;
}
int getCols() {
return cols;
}
void setRows(int rows) {
this->rows = rows;
}
int getRows() {
return rows;
}
Matrix operator+(Matrix b) {
Matrix c(rows,cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c.matrix[i][j] = matrix[i][j] + b.matrix[i][j];
}
}
return c;
}
Matrix operator*(Matrix b) {
Matrix c(rows, b.getCols());
for (int i = 0; i < rows; i++){
for (int j = 0; j < b.getCols(); j++){
c.matrix[i][j] = 0;
for (int k = 0; k < cols; k++)
c.matrix[i][j] += matrix[i][k] * b.matrix[k][j];
}
}
return c;
}
void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
};
int main() {
Matrix a(5, 2);
a.MatrixOneValue(1);
Matrix b(5, 3);
printf("Matrix A:\n");
a.print();
b.MatrixOneValue(2);
printf("\nMatrix B:\n");
b.print();
Matrix c = a + b;
printf("\nA+B:\n");
c.print();
Matrix d = a * b;
printf("\nA*B:\n");
d.print();
system("pause");
return 0;
}
Результаты: