Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. /
  2.  
  3. //#define _CRT_SECURE_NO_WARNINGS
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include <string>
  7. #include <sstream>
  8. #include <limits>
  9. #include <locale.h>
  10. #include <limits>
  11. #include <iomanip>
  12. #include <fstream>
  13. #include <io.h>
  14. using namespace std;
  15.  
  16. class Fio
  17. {
  18. private:
  19.     char* familia_;
  20.     char* imya_;
  21.     char* otchestvo_;
  22. public:
  23.     Fio()
  24.     {
  25.         familia_ = new char[512];
  26.         imya_ = new char[512];
  27.         otchestvo_ = new char[512];
  28.     }
  29.    
  30.     void setFio(char *familia, char *imya,  char *otchestvo)
  31.     {
  32.         familia_ = familia;
  33.         imya_ = imya;
  34.         otchestvo_ = otchestvo;
  35.     }
  36.     string getStringFio()
  37.     {
  38.         string familia = to_string(*familia_);
  39.         string imya = to_string(*imya_);
  40.         string otchestvo = to_string(*otchestvo_);
  41.         return (familia + " " + imya + " " + otchestvo);
  42.     }
  43.  
  44.     char* getCharFio()
  45.     {
  46.         char* fio = new char[1024];
  47.         char c4[] = " ";
  48.         memset(fio, 0, strlen(fio));
  49.         strcat(fio, familia_);
  50.         strcat(fio, c4);
  51.         strcat(fio, imya_);
  52.         strcat(fio, c4);
  53.         strcat(fio, otchestvo_);
  54.         return fio;
  55.     }
  56.  
  57. };
  58.  
  59. class Train
  60. {
  61. private:
  62.     Fio fio_;       // пассажир
  63.     char* point_from;   // пункт отправления
  64.     char* point_to; // пункт прибытия
  65.     double price;       // цена билета
  66.     class Train *next;
  67.     class Train *prev;
  68.     friend void PriceCheck();
  69.     friend void SortyByDepart();
  70.     friend Train* Swap(Train* );
  71.     friend void PrintDataTrain();
  72.  
  73. public:
  74.     Train()
  75.     {
  76.         point_to = new char[1024];
  77.         point_from = new char[1024];
  78.  
  79.         prev = NULL;
  80.         next = NULL;
  81.     }
  82.     ~Train()
  83.     {
  84.         delete[] point_to;
  85.         delete[] point_from;
  86.         delete[] &fio_;
  87.     }
  88.  
  89.    
  90.  
  91.     Train* loadFromFile(const string filename)
  92.     {
  93.         Train *prevTrain = NULL;
  94.         Train *nextTrain;
  95.        
  96.  
  97.         string fio_line, departure_line, destination_line, price_line;
  98.         ifstream dataFile(filename, ios::in);
  99.         if (!dataFile.is_open()) {
  100.             cout << "File cannot be open. Check is the file is there.\n";
  101.         }
  102.         else {
  103.             string name, surname, patronyc;
  104.             double price;
  105.             char* name_ = new char[512]; char* surname_ = new char[512]; char* patronyc_ = new char[512];
  106.  
  107.             while (getline(dataFile, fio_line) &&
  108.                 getline(dataFile, departure_line) &&
  109.                 getline(dataFile, destination_line) &&
  110.                 getline(dataFile, price_line))
  111.             {
  112.                 istringstream iss(fio_line);
  113.  
  114.                 if (!(iss >> name >> surname >> patronyc))
  115.                     break;
  116.  
  117.                 nextTrain = new Train();
  118.                 memset(name_, 0, strlen(name_));
  119.                 memcpy(name_, name.c_str(), name.size());
  120.                 memset(surname_, 0, strlen(surname_));
  121.                 memcpy(surname_, surname.c_str(), surname.size());
  122.                 memset(patronyc_, 0, strlen(patronyc_));
  123.                 memcpy(patronyc_, patronyc.c_str(), patronyc.size());
  124.                 Fio fio;
  125.                 fio.setFio(name_, surname_, patronyc_);
  126.  
  127.                 price = atof(price_line.c_str());
  128.  
  129.                 if (!price) {
  130.                     cout << "Error. Wrong type of variable. Check information in the file.\n";
  131.                     break;
  132.                 }
  133.  
  134.                
  135.  
  136.                 nextTrain->fio_ = fio;
  137.                 nextTrain->price = price;
  138.                 memset(nextTrain->point_from, 0, strlen(nextTrain->point_from));
  139.                 memcpy(nextTrain->point_from, departure_line.c_str(), departure_line.size());
  140.                 memset(nextTrain->point_to, 0, strlen(nextTrain->point_to));
  141.                 memcpy(nextTrain->point_to, destination_line.c_str(), destination_line.size());
  142.  
  143.                 if (prevTrain)
  144.                     prevTrain->next = nextTrain;
  145.  
  146.                 nextTrain->prev = prevTrain;
  147.                 //nextTrain->next = NULL;
  148.  
  149.                 prevTrain = nextTrain;
  150.             }
  151.  
  152.             Train *start_of_list = NULL;
  153.  
  154.             start_of_list = prevTrain;
  155.  
  156.             while (start_of_list->prev)
  157.                 start_of_list = start_of_list->prev;
  158.  
  159.             return start_of_list;
  160.         }
  161.     }
  162. };
  163.  
  164. void PrintDataTrain()
  165. {
  166.     //trip = ReadingFile();
  167.     Train *p = NULL;
  168.     p = p->loadFromFile("File_1.txt");
  169.     if (p != NULL) {
  170.         int r = 0;
  171.         cout << "\n\n";
  172.         cout << setw(3) << right << r << " : ";
  173.         cout << setw(30) << left << "Passenger";
  174.         cout << setw(18) << "Departure point";
  175.         cout << setw(14) << "Destination";
  176.         cout << setw(7) << "Price";
  177.         cout << endl;
  178.         cout << ".............................................." << endl;
  179.  
  180.         while (p != NULL)
  181.         {
  182.             r++;
  183.             cout << setw(3) << right << r << " : ";
  184.             cout << setw(30) << left << p->fio_.getStringFio();
  185.             cout << setw(18) << p->point_from;
  186.             cout << setw(14) << p->point_to;
  187.             cout << setw(7) << p->price;
  188.             //cout << "\n";
  189.             //      p = p->next;
  190.             cout << endl;
  191.             p = p->next;
  192.         }
  193.  
  194.         cout << "______________________________________________" << endl;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement