Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Программирование на C / C++ / Лабораторные работы / Лабораторная работа №12

.docx
Скачиваний:
36
Добавлен:
02.05.2014
Размер:
199.88 Кб
Скачать

Уфимский Государственный Авиационный Технический Университет

Лабораторная работа №12

Списки. Класс списка структур

Выполнил студент

группы

Проверил доцент

кафедры АПрИС

Бежаева О.Я.

Уфа 2008.

Цель работы: Знакомство и получение навыков работы со списками классами на языке С++.

Условия Задания:

Строка таблицы данных содержит следующую информацию о книгах: автор, наименование, издательство, год издания. Требуется найти перечень издательств с указанием числа их книг.

Решение задания:

1)Текст программы на языке C/C++

# include <iostream.h>

# include <fstream.h>

# include <string.h>

# include <iomanip.h>

# include <stdlib.h>

# include <conio.h>

struct BOOK

{

char avtor[30];

char name[30];

char izd[30];

double year;

};

struct IZDATELSTVO

{

char izd[30];

int kol;

};

struct node1

{

BOOK b;

node1*next;

};

struct node2

{

IZDATELSTVO iz;

node2*next;

};

class spisok

{

private:

node1*beg1, *end1;

int n;

node2 *beg2, *end2;

int k;

node1*findavtor (BOOK b);

public:

spisok() {n=k=0; beg1=end1=NULL;beg2=end2=NULL;}

~spisok();

void inputstructfile();

void outputstruct();

void perechizd();

void outputizd();

void outputizdfile();

};

spisok::~spisok ()

{

node1 *p1;

node2 *p2;

if (beg1!=NULL)

{

while (beg1!=NULL)

{

p1=beg1;

beg1=beg1->next;

delete p1;

}

end1=NULL;

n=0;

}

if (beg2!=NULL)

{

while (beg2!=NULL)

{

p2=beg2;

beg2=beg2->next;

delete p2;

}

end2=NULL;

k=0;

}

}

node1 *spisok:: findavtor (BOOK t)

{

node1 *p;

p=beg1;

while (p!=NULL)

{

if( strcmp( t.avtor, p->b.avtor)==0 && strcmp( t.izd, p->b.izd)==0) break;

p=p->next;

}

}

void spisok:: inputstructfile()

{

ifstream fin;

char infile[20];

node1 *p1, *p;

node2 *p2;

if (beg1!=NULL)

{

while (beg1!=NULL)

{

p1=beg1;

beg1=beg1->next;

delete p1;

}

end1=NULL;

n=0;

}

if (beg2!=NULL)

{

while (beg2!=NULL)

{

p2=beg2;

beg2=beg2->next;

delete p2;

}

end2=NULL;

k=0;

}

cout << "Enter file name, please:\n" ;

cin >> infile;

fin.open(infile);

if( fin==NULL) {cout<<"File not open\n"; exit(1);}

p= new node1;

if( p==NULL ) { cout<<"No dinamic memory"; getch(); exit(1);}

fin >> p->b.avtor >> p->b.name >> p->b.izd >> p->b.year ;

p->next=NULL;

while( fin.good())

{

if (beg1==NULL) beg1=p; else end1->next=p;

end1=p;

n++;

p= new node1;

if(p==NULL){cout<<"No dinamic memory";getch();exit(1);}

fin >> p->b.avtor >> p->b.name >> p->b.izd >> p->b.year ;

p->next=NULL;

}

delete p;

fin.close();

}

void spisok:: outputstruct()

{node1 *p;

cout.setf(ios::left);

cout<<"\nABTOP Ha3BaHiE I/I3DaHiE God\n";

for( p=beg1; p!=NULL; p=p->next)

{cout<<setw(13)<<p->b.avtor<<setw(10)<<p->b.name

<<setw(10)<<p->b.izd<<setw(10)<<p->b.year<<endl;}

}

void spisok:: perechizd()

{

int fl;

node1 *p1;

node2 *p2, *p;

if (beg2!=NULL)

{

while (beg2!=NULL)

{

p2=beg2;

beg2=beg2->next;

delete p2;

}

end2=NULL;

k=0;

}

for( p1=beg1; p1!=NULL; p1=p1->next)

{

fl=1;

for( p2=beg2; p2!=NULL; p2=p2->next)

{

if (strcmp (p1->b.izd, p2->iz.izd)==0)

{fl=0; p2->iz.kol++; break;}

}

if (fl==1)

{

p=new node2;

if (p==NULL) {cout<<"No dinamic memory"; getch(); exit(1);}

p->next=NULL;

strcpy (p->iz.izd, p1->b.izd);

p->iz.kol=1;

if (beg2==NULL) beg2=p; else end2->next=p;

end2=p;

k++;

}

}

}

void spisok::outputizd()

{

node2 *p;

cout.setf(ios::left);

cout<<"\nPerechen...\n";

cout<<"\n|/|3DaTeL'CTBO KOLI4ECTBO\n";

for( p=beg2; p!=NULL; p=p->next)

{cout<<setw(20)<<p->iz.izd<<setw(10)<<p->iz.kol<<endl;}

}

void spisok:: outputizdfile()

{

ofstream fout;

char outfile[10];

node2 *p;

cout<<"\nEnter output file name, please:\n" ;

cin >> outfile;

fout.open(outfile);

if( !fout.good()) {cout<<"Fail ne otkrit.\n"; exit(1);}

for( p=beg2; p!=NULL; p=p->next)

fout <<p->iz.izd<<" "<<p->iz.kol<<endl;

fout.close();

return;

}

void main()

{clrscr();

spisok a;

a.inputstructfile();

a.outputstruct();

a.perechizd();

a.outputizd();

a.outputizdfile();

}

2)Результат работы программы:

Текстовой файл с данными

Работа программы:

Текстовой файл, в который был сохранен перечень