Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторні роботи по програмуванні(14 лаб).doc
Скачиваний:
77
Добавлен:
16.05.2015
Размер:
1.43 Mб
Скачать

Приклад виконання Завдання 1

Варіант 1. Похідний клас містить метод (функцію) копіювання заданої кількості символів з заданої позиції.

Дана програма містить два класи, що працюють з текстовим файлом. Базовий клас містить конструктор, за допомогою якого файл відкривається автоматично після ініціалізації даних в головній програмі, деструктор, що автоматично викликається на заключному етапі виконання програми і виконує закриття файлу. Похідний клас містить функцію Copy_to_file(), що призначена для копіювання заданої кількості символів з заданої позиції.

Структура класів має такий вигляд:

class main_class {

char c;

int Num,UK,KIL;

protected:

FILE* stream;

public:

main_class();

~main_class();

void Quantity_of_chars();

void Number_of_space();

void Number_of_words();

void Delete_wrong_space();

};

class successor: public main_class {

public:

successor():main_class(){}

void Copy_to_file();

};

Лістинг програми:

#include <cstring>

#include <cstdio>

#include <cstdlib>

class main_class {

char c;

int Num,UK,KIL;

protected:

FILE* stream;

public:

main_class();

~main_class();

void Quantity_of_chars();

void Number_of_space();

void Number_of_words();

void Delete_wrong_space();

};

class successor: public main_class {

public:

successor():main_class(){}

void Copy_to_file();

};

int main (){

successor problem;

problem.Quantity_of_chars();

problem.Number_of_space();

problem.Number_of_words();

problem.Delete_wrong_space();

problem.Copy_to_file();

system("pause");

return 0;

}

main_class::main_class()

{

stream = fopen( "Dat.txt", "r+");

}

main_class::~main_class()

{

fclose(stream);

}

void main_class::Quantity_of_chars()

{

Num = 0;

while ((c = getc(stream)) != EOF) {

Num++;

}

printf("Quantity of chars=%i\n", Num);

}

void main_class::Number_of_space()

{

stream = fopen( "Dat.txt", "r+");

int i = 0;

while ((c = getc(stream)) != EOF) {

if (c == ' ')

i++;

}

printf("Quantity of space symbols=%i\n", i);

}

void main_class::Number_of_words()

{

stream = fopen( "Dat.txt", "r+");

char *Mas = new char[Num];

int i = 0;

while ((c = getc(stream)) != EOF){

Mas[i]=c;

i++;

}

UK=0;

KIL = 0;

for(i=0; i<=Num; i++) {

if((Mas[i] == ' ')||(Mas[i] == '\n'))

UK++;

if(((Mas[i] != ' ')&&(Mas[i] != '\n')) && (UK >= 1)) {

KIL++;

UK = 0;

}

}

printf("Quantity of words=%i\n", KIL+1);

delete Mas;

}

void main_class::Delete_wrong_space()

{

UK=0;

stream = freopen( "Dat.txt", "r+",stream);

char *Mas = new char[Num];

int i = 0;

while ((c = getc(stream)) != EOF){

if(c== ' '){

UK++;

}else{

Mas[i]=c;

UK=0;

}

if((c == ' ')&&(UK==1)){

Mas[i]=c;

}

if((c== ' ')&&(UK >1)){

i--;

}

i++;

}

UK=i;

printf("\n");

for(i=0; i<UK; i++){

printf("%c",Mas[i]);

}

printf("\n");

}

void successor::Copy_to_file(){

int pos=0,kil=0;

printf("\nInput position and number of symbols\n ");

scanf("%i %i",&pos,&kil);

char c;

int i=0,j=0;

FILE *stream2;

stream = freopen("Dat.txt","r+",stream);

stream2 = fopen( "Dat2.txt", "w");

while ((c = getc(stream)) != EOF) {

i++;

if((i>=pos)&&(j<kil)){

putc(c,stream2);

j++;

}

}

}

Тестування:

Для перевірки правильності роботи програми створимо файл такого виду:

aaa bb ccccc

ffff

ggggggg

Перевіримо, який результат видасть програма:

Quantity of chars=32

Quantity of space symbols=8

Quantity of words=5

aaa bb ccccc

ffff

ggggggg

Input position and number of symbols

7 2

Press any key to continue . . .

Зміст файлу Dat2.txt буде наступним:

bb