МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ
Поможем в ✍️ написании учебной работы
Поможем с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой

НАЦІОНАЛЬНИЙ ТЕХНІЧНИЙ УНІВЕРСИТЕТ

"ХАРКІВСЬКИЙ ПОЛІТЕХНІЧНИЙ ІНСТИТУТ

Кафедра Обчислювальної техніки та програмування

 

ЗАТВЕРДЖУЮ

Завідувач кафедри ОТП

__________ /Xxxxxxxxxxx./

"___" __________ 2007р.

 

Розрахунково-графічне завдання № 1, 2

ІНФОРМАЦІЙНО-ДОВІДКОВА СИСТЕМА

Текст програми

Xxxxxxxxxxx-01 12 01-1-ЛЗ

  Розробники Керівник проекту __________ /доц. Xxxxxxxxxxx./ "___" __________ 2007 р. Виконавець __________ /студ. Xxxxxxxxxxx./ "___" __________ 2007 р.

 

 

2007
УЗГОДЖЕНО

 

Xxxxxxxxxxx-01 81 01-1-ЛЗ

 


Розрахунково-графічне завдання № 1, 2

ІНФОРМАЦІЙНО-ДОВІДКОВА СИСТЕМА

Текст програми

Xxxxxxxxxxx-01 12 01-1

 

2007

 

Текст програми

//Заголовковий файл

#include <string.h>

 

class album

    {

              public:

                       album() {}

                       virtual void input();

                       virtual void output();

                       char nazv[4];//Назва

                       int god;//Рік випуску

                       ~album() {}

    };

 

class album1: public album

    {   protected:

                       int trek;//Кількість треків

              public:

                       album1();

                       virtual void input();

                       virtual void output();

                       int getTrek()

                       {

                                 return trek;

                       }

                       void setTrek(int c)//ф-ція створення поля

                       {

                                 trek=c;

                       }

                       ~album1();

    };

 

class album2: public album1

    {

              protected:

                       int time;//Час звучання

              public:

                       album2();

                       virtual void input();

                       virtual void output();

                       int getTime()

                       {

                                 return time;

                       }

                       void setTime(int c)

                       {

                                 time=c;

                       }

                       ~album2();

    };

 

class album3: public album2

    {

              protected:

                       int bonus;//Кількість бонус треків

              public:

                       album3();

                       virtual void input();

                       virtual void output();

                       int getBonus()

                       {

                                 return bonus;

                       }

                       void setBonus(int c)

                       {

                                 bonus=c;

                       }

                       ~album3();

    };

class Tool

    {

              public:

                       album *d;

                       int t;

                       friend class Trist;//об'явлення дружнього класу

                       Tool *right,*left;

    };

class Trist

    {

              public:

                       Tool *head,*tail;

                       Tool *current;

                       Trist(int type,int vvod);//Конструктор

                       void add(int type, int vvod);//ф_ція добавлення об’єктів

                       ~Trist();//Деструктор

    };

//Описний файл

#include "RGZ.h"

#include <iostream.h>

#include <string.h>

#include <stdlib.h>

#include <iomanip.h>

void album::input()//ф-ція вводу

{

    cout << "Vvedite nazvanie alboma: ";

    cin >> nazv;

    nazv[3]=NULL;

    cout << "Vvedite god vipuska: ";

    cin >> god;

}

void album::output()//ф-ція виводу

{

 cout << endl<< setw(10) << setiosflags(ios::left) << nazv

     << setw(10) << setiosflags(ios::left) << god;

}

album1::album1():album() {}//конструктор

album1::~album1(){ album::~album(); }//деструктор

void album1::input()

{

    album::input();

    cout << "Vvedite kl-vo trekov v albome: ";

    cin >> trek;

}

void album1::output()

{

    album::output();

    cout<< setw(10) << setiosflags(ios::left) << trek;

}

 

album2::album2():album1() {}

album2::~album2() { album1::~album1(); }

void album2::input()

{

    album1::input();

    cout << "Vvedite prodolzitelnost alboma po vremeni: ";

    cin >> time;

}

void album2::output()

{

    album1::output();

    cout << setw(12) << setiosflags(ios::left) << time;

}

album3::album3():album2() {}

album3::~album3() { album2::~album2(); }

void album3::input()

{

    album2::input();

    cout << "Vvedite kolichestvo bonus trekov: ";

    cin >> bonus;

}

void album3::output()

{

    album2::output();

    cout << setw(10) << setiosflags(ios::left) << bonus;

}

void caption()//заголовкова ф-ція

{

 cout << endl << setw(10) << setiosflags(ios::left) << "|Nazv"

               << setw(10) << setiosflags(ios::left) << "|God"

               << setw(10) << setiosflags(ios::left) << "|Treki"

               << setw(10) << setiosflags(ios::left) << "|Vremya"

               << setw(10) << setiosflags(ios::left) << "|Bonus+"<< endl;

 cout << "-------------------------------------------------------";

}

Trist::Trist(int type, int vvod)//Створення списку об'єктів

{

    album1 *pr;

    album2 *sp;

    album3 *fl;

    head = new Tool;

    head->t = type;

    if (head->t == 0)

    {

              pr = new album1;

              if (vvod)

                       pr->input();

              head->d=pr;

    } else

    if (head->t == 1)

    {

              sp = new album2;

              if (vvod)

                       sp->input();

              head->d=sp;

    } else

    if (head->t == 2)

    {

              fl = new album3;

              if (vvod)

                       fl->input();

              head->d=fl;

    }

    head->right=NULL;

    head->left=NULL;

    tail=head;

    current=head;

}

Trist::~Trist()//Висвободження памяті

{

    Tool *h;

    h=head;

    while (h!=0)

    {   head=head->right;

              head->left=NULL;

              delete h;

              h=head;

    }

}

 

void Trist::add(int type, int vvod)//додавання нового об’єкта у список

{

    Tool *q = new Tool;

    album1 *pr;

    album2 *sp;

    album3 *fl;

    q->t = type;

    switch (q->t)

    {

              case 0:

                       pr = new album1;

                       if (vvod)

                                 pr->input();

                       q->d=pr;

                       break;

              case 1:

                       sp=new album2;

                       if (vvod)

                                 sp->input();

                       q->d=sp;

                       break;

              case 2:

                       fl=new album3;

                       if (vvod)

                                 fl->input();

                       q->d=fl;

                       break;

    }

    if (current==tail)

    {

              q->right=NULL;

              q->left=current;

              current->right=q;

              tail=q;

              current=q;

    } else

    {   q->right=current->right;

              current->right->left=q;

              current->right=q;

              q->left=current;

              current=q;

    }

}

//Головний файл

#include "RGZ_dsc.cpp"

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

#include <fstream.h>

 

void sozdan(Trist *&a, int n);//створення

void output(Trist *&a);//вивід на екран

void output_file(Trist *&a, int n);//вивід до файлу

void input_file(Trist *&a);//читання з файлу

Tool* pois(Trist *&a, char* nazv);//ф-ція пошуку

Tool* zadacha(Trist *&a,int godL,int godR);//виконання задачі

void main()//Головна програма

{

    int i,N=5,k;

    Trist *a = NULL;

    clrscr();

    randomize();

    while (1)

    {

              cout << "\n\n############################";

              cout << "\n# 1 - Sozdanie bazi dannih #";

              cout << "\n# 2 - Vivod na ekran #";

              cout << "\n# 3 - Vivod v fail #";

              cout << "\n# 4 - Chtenie iz faila #";

              cout << "\n# 5 - Poisk #";

              cout << "\n# 6 - Redaktirovanie zapisi #";

              cout << "\n# 7 - Vstavka novoi zapisi #";

              cout << "\n# 8 - Udalenie zapisi #";

              cout << "\n# 9 - Vipolnrnie zadachi #";

              cout << "\n# 10 - Vihod #";

              cout << "\n\n############################";

              cout << "\n Vvedite nomer operatsii:";

              cin >> k;

              switch (k)

              {

                       case 1:

                                 cout << "\n Sozdanie tablizi dannih";

                                 cout << "\n Vvedite N: ";

                                 cin >> N;

                                 sozdan(a,N);

                                 cout << "\n Tabliza dannih sozdana.";

                                 getch();

                                 break;

                       case 2:

                                 cout << "\n Vivod na ekran ";

                                 output(a);

                                 cout << "\n Vivod na ekran zavershen.";

                                 getch();

                                 break;

                       case 3:

                                 cout << "\n Vivod v fail a.dat ";

                                 output_file(a,N);

                                 cout << "\n Vivod v fail uspeshno zavershen.";

                                 getch();

                                 break;

                       case 4:

                                 cout << "\n Chtenie iz faila a.dat ";

                                 input_file(a);

                                 cout << "\n Chtenie iz faila uspeshno zavershena.";

                                 getch();

                                 break;

                       case 5:

                                 cout << "\n Poisk obyekta ";

                                 cout << "\n Vvedite kluch dlya poiska(nazvanie alboma): ";

                                 char *nazv;

                                 cin >> nazv;

                                 Tool *n;

                        // n=NULL;

                                 n = pois(a,nazv);

                                 if (n==NULL) cout << "\n Zapis ne naidena.";

                                 else

                                 {

                                          cout << "\n Zapis bila naidena.";

                                          caption();

                                          n->d->output();

                                 }

                                 delete nazv;

                                 getch();

                                 break;

                       case 6:

                                 cout << "\n Redaktirovanie zapisi ";

                                 cout << "\n Vvedite nomer zapisi: ";

                                 int c;

                                 cin >> c;

                                 a->current=a->head;

                                 for (i=0;a->current!=NULL;i++)

                                 {

                                          if (i==c)

                                          {

                                                   a->current->d->input();

                                                   break;

                                          }

                                          a->current=a->current->right;

                                 }

                                 cout << "\n Redaktirovanie uspeshno zaversheno.";

                                 getch();

                                 break;

                       case 7:

                                 cout << "\n Vstavka novoi zapisi ";

                                 cout << "\n Vvedite tip novoy zapisi [0..2]: ";

                                 int p = 0;

                                 cin >> p;

                                 a->current=a->tail;

                                 a->add(p,1);

                                 cout << "\n Vstavka zapisi uspeshno zaversena.";

                                 getch();

                                 break;

                       case 8:

                                 cout << "\n Udalenie zapisi ";

                                 cout << "\n Vvedite nomer zapisi dlya udalenia: ";

                                 int j = 0;

                                 cin >> j;

                                 a->current=a->head;

                                 for (i=0;a->current!=NULL;i++)

                                 {

                                          if (i==j)

                                          {

                                                   Tool *q = a->current;

                                                   if (q->left!=NULL) q->left->right = q->right;

                                                   if (q->right!=NULL) q->right->left = q->left;

                                                   if (q==a->head) a->head=q->right;

                                                   if (q==a->tail) a->tail=q->left;

                                                   delete q;

                                                   a->current = a->tail;

                                                   break;

                                          }

                                          a->current=a->current->right;

                                 }

                                 cout << "\n Zapis bila udalena.";

                                 getch();

                                 break;

                       case 9:

                                 cout << "\n Poisk obyekta s minimalnim kl-vom trekov v zadannom diapazone let";

                                 cout << "\n Vvedite leviy predel poiska(god): ";

                                 int godL,godR;

                                 cin >> godL;

                                 cout << "\n Vvedite praviy predel poiska(god): ";

                                 cin >> godR;

                                 Tool *k;

                                 k = zadacha(a,godL,godR);

                                 if (n==NULL) cout << "\n Zapis ne naidena.";

                                 else

                                 {

                                          cout << "\n Zapis bila naidena.";

                                          caption();

                                          k->d->output();

                                 }

                                 getch();

                                 break;

                       case 10:

                                 delete a;

                                 exit(0);

                                 break;

 

              }

}

}

void sozdan(Trist *&a, int n)

{

    if (a!=NULL)

              delete a;

    cout << "\nVvedite tip zapisi [0..2]: ";

    int k;

    cin >> k;

    a = new Trist(k,1);

    for (int i=1;i<n;i++)

    {

              cout << "\nVvedite tip zapisi [0..2]: ";

              cin >> k;

              a->add(k,1);

    }

}

void output(Trist *&a)

{

    caption();

    a->current=a->head;

    while (a->current!=NULL)

    {

              a->current->d->output();

              a->current=a->current->right;

    }

}

void output_file(Trist *&a, int n)

{

 ofstream file("a.dat",ios::out|ios::binary);

 if (!file)

 {

              cerr << "\nOshibka vivoda v fail!\n";

              getch();

              exit(1);

 }

 file.seekp(0);

 file.write((char*)&(n),sizeof(n));

 a->current=a->head;

 while (a->current!=NULL)

 {

              file.write((char*)&(a->current->t),sizeof(a->current->t));

              file.write(a->current->d->nazv,sizeof(a->current->d->nazv));

              switch (a->current->t)

              {

                       case 0:

                                 album1 *obj1;

                                 obj1=(album1*)a->current->d;

                                 int trek = obj1->getTrek();

                                 file.write((char*)&(trek),sizeof(trek));

                                 break;

                       case 1:

                                 album2 *obj2;

                                 obj2=(album2*)a->current->d;

                                 trek = obj2->getTrek();

                                 int time = obj2->getTime();

                                 file.write((char*)&(trek),sizeof(trek));

                                 file.write((char*)&(time),sizeof(time));

                                 break;

                       case 2:

                                 album3 *obj3;

                                 obj3=(album3*)a->current->d;

                                 trek = obj3->getTrek();

                                 time = obj3->getTime();

                                 int bonus = obj3->getBonus();

                                 file.write((char*)&(trek),sizeof(trek));

                                 file.write((char*)&(time),sizeof(time));

                                 file.write((char*)&(bonus),sizeof(bonus));

                                 break;

              }

              a->current=a->current->right;

 }

 file.close();

}

 

void input_file(Trist *&a)

{

 ifstream file("a.dat",ios::in);

 if (!file)

 {

              cerr << "\nOshibka chtenia faila!\n";

              getch();

              exit(1);

 }

 int n;

 file.read((char*)&(n),sizeof(n));

 int i;

 delete a;

 for (i=0;i<n;i++)

 {

              int t;

              file.read((char*)&(t),sizeof(t));

              if (i==0) a=new Trist(t,0); else a->add(t,0);

              file.read((char*)&(a->current->d->nazv),sizeof(a->current->d->nazv));

              switch (t)

              {

                       case 0:

                                 album1 *obj1;

                                 obj1=(album1*)a->current->d;

                                 int trek = 0;

                                 file.read((char*)&(trek),sizeof(trek));

                                 obj1->setTrek(trek);

                                 break;

                       case 1:

                                 album2 *obj2;

                                 obj2=(album2*)a->current->d;

                                 trek = 0;

                                 int time = 0;

                                 file.read((char*)&(trek),sizeof(trek));

                                 file.read((char*)&(time),sizeof(time));

                                 obj2->setTrek(trek);

                                 obj2->setTime(time);

                                 break;

                       case 2:

                                 album3 *obj3;

                                 obj3=(album3*)a->current->d;

                                 trek = 0;

                                 time = 0;

                                 int bonus = 0;

                                 file.read((char*)&(trek),sizeof(trek));

                                 file.read((char*)&(time),sizeof(time));

                                 file.read((char*)&(bonus),sizeof(bonus));

                                 obj3->setTrek(trek);

                                 obj3->setTime(time);

                                 obj3->setBonus(bonus);

                                 break;

              }

 }

 file.close();

}

Tool* pois(Trist *&a, char* nazv)

{

    Tool *n = NULL;

    a->current=a->head;

    while (a->current!=NULL)

    {

              n = a->current;

              if (strcmp(n->d->nazv,nazv)==0) break;

              else n=NULL;

              a->current=a->current->right;

    }

    return n;

}

Tool* zadacha(Trist *&a,int godL,int godR)

{

    Tool *k = NULL;

    a->current=a->head;

    int max=1000;

    while (a->current!=NULL)

    {

              k = a->current;

              if(k->d->god>godL)

               if(k->d->god<godR)

               if(k->d->trek<min) break;

               else k=NULL;

              a->current=a->current->right;

    }

    return k;

Дата: 2019-12-10, просмотров: 256.