Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- #include <io.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- struct poezda
- {
- char nazv[20];
- char time[20];
- char punkt[20];
- int mesta;
- };
- void vvsp(char *fname); //Ввести список
- void prsp(char *fname); //Прочитать список
- void rezc(char *fname); //Вывести результат
- void rezf(char *fname); //Ввести результат в файл
- void textf(char *fnazv, char *fname); //Ввести список в текстовый файл
- void qsort(int left, int right, poezda *poe);
- void shella(poezda *poe, int n);
- int menu(); //Меню
- int _tmain(int argc, _TCHAR* argv[])
- {
- char fname[30];
- cout<<"Vvedite imya fila";
- cin>>fname;
- while (true)
- {
- switch (menu())
- {
- case 1: vvsp(fname); break; //Ввести список в файл
- case 2: prsp(fname); break; //Прочитать
- case 3: rezc(fname); break; //Вывести результат на экран
- case 4: return 0;
- default: "Viberite pravilno!";
- }
- puts("Press any key to continue");
- system("cls");
- }
- system("pause");
- return 0;
- }
- int menu()
- {
- int i;
- cout << "Vyberite:" << endl;
- cout << "1.Vyvesti dannye v fail" << endl;
- cout << "2.Proschitat dannye iz faila" << endl;
- cout << "3.Vyvesti rezultat na ekran" << endl;
- cout << "4.Exit" << endl;
- cin >> i;
- return i;
- }
- void vvsp(char *fname)
- {
- poezda poe;
- FILE *fl;
- if ((fl = fopen(fname, "ab")) == NULL)
- if ((fl = fopen(fname, "wb")) == NULL)
- {
- cout << "Oshibka pri sozdanii" << endl;
- }
- char ch;
- do
- {
- cout << " Vvedite nazvanie poezda"; cin >> poe.nazv;
- cout << " Vvedite vremya otpravleniya"; cin >> poe.time;
- cout << "Vvedite punkt naznacheniya"; cin >> poe.punkt;
- cout << "Vvedite chislo svobodnih mest"; cin >> poe.mesta;
- fwrite(&poe, sizeof(poezda), 1, fl);
- cout << endl << " Budete vvodit dalshe? (y/n) ";
- cin >> ch;
- cout << endl;
- }
- while (ch == 'y');
- fclose(fl);
- menu();
- getch();
- }
- void prsp(char *fname)
- {
- FILE *fl;
- poezda poe;
- if ((fl = fopen(fname, "rb")) == NULL)
- {
- cout << "oshibka pri otktitii" << endl;
- return;
- }
- long int n=0;
- n=filelength(fileno(fl))/sizeof(poezda);
- for (int i=0; i<n; i++)
- {
- fread(&poe, sizeof(poezda), 1, fl);
- cout << "\n Nazvanie poezda " << poe.nazv << "\n";
- cout << "\n Vremya otpravleniya " << poe.time << "\n";
- cout << "\n Punkt naznacheniya" << poe.punkt << "\n";
- cout << "\n Chislo svobodnih mest" << poe.mesta << endl;
- }
- fclose(fl);
- }
- void rezc(char *fname)
- {
- poezda *poe;
- FILE *fl;
- int n;
- if ((fl = fopen(fname, "r")) == NULL)
- {
- cout << "Oshibka pri otkritii" << endl;
- return;
- }
- n=filelength(fileno(fl)) / sizeof(fname);
- for (int i = 0; i<n; i++)
- {
- fread(&poe, sizeof(poezda), 1, fl);
- if (poe[i].punkt == "Brest")
- {
- cout <<"Nazvanie poezda"<<poe[i].nazv<<"Vremya otpravleniya"<< poe[i].time<<"Punkt naznacheniya"<<poe[i].punkt<<"Chislo svobodnih mest"<<poe[i].mesta<<endl;
- }
- int b;
- cout<<"viberite metod sortirovki" << endl;
- cout<<"1.QuickSort" << endl;
- cout<<"2.Shella" << endl;
- switch (b)
- {
- case 1: qsort(0, n-1, poe);break;
- case 2: shella(poe, n);break;
- default: cout <<"Ne vibran metod sortirovki!"<<endl;
- }
- cin>>b;
- char q;
- cout <<"Zanesty otsortyrowannye dannye v textowiy file? (y/n)"<< endl;
- cin >> q;
- if (q == 'y')
- {
- char fnazv[20];
- cout << "Vvedite textfile nazv" << endl;
- cin >> fnazv;
- textf(fnazv,fname);
- }
- fclose(fl);
- return;
- }
- void textf(char *fnazv, char *fname)
- {
- poezda poe;
- FILE *fl, *ft;
- if ((ft = fopen(fnazv, "w")) == NULL)
- {
- cout << "Oshibka pri sozdanii textfile" << endl;
- return;
- }
- if ((fl = fopen(fname, "rb")) == NULL)
- {
- cout << "Oshibka pri otkritii" << endl;
- return;
- }
- n=filelength(fileno(fl))/sizeof(poezda);
- for (int i=0; i<n; i++)
- {
- fread(&poe, sizeof(poezda), 1, fl);
- if (poe[i].punkt == "Brest")
- {
- fprintf(ft, "%s %s %s %d \n", poe[i].nazv, poe[i].time, poe[i].punkt, poe[i].mesta);
- }
- }
- cout << "Successfully complited" << endl;
- fclose(ft);
- fclose(fl);
- }
- void qsort(int left, int right, poezda *poe)
- {
- int i=left, j=right, t;
- poezda l;
- t=(left+right)/2;
- do
- {
- while(poe[i].time<poe[t].time&&i<right) i++;
- while(poe[j].time>poe[t].time&&j>left) j--;
- if(i<=j)
- {
- l=poe[i];
- poe[i]=poe[j];
- poe[j]=l;
- i++; j--;
- }
- }
- while(i<=j);
- if(left<j)qsort(left, j, poe);
- if(i<right)qsort(i, right, poe);
- return;
- }
- void shella(poezda *poe, int n)
- {
- int i, j, d;
- poezda t;
- for(d=3; d>0; d--)
- for(i=d; i<n; i++)
- {
- t=poe[i];
- for(j=i-d; j>=0&&t.time<poe[i].time; j-=d) poe[j+d]=poe[j];
- poe[j+d]=t;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement