Advertisement
Toliak

lab61

Nov 13th, 2018
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. struct Car
  6. {
  7.     std::string model;
  8.     double fuelConsumption[3];
  9.     double maxSpeed;
  10.     double power;
  11. };
  12.  
  13. std::ostream &operator<<(std::ostream &stream, Car &car)
  14. {
  15.     stream << "Model: " << car.model << std::endl;
  16.     stream << "Fuel consumption: "
  17.                 << car.fuelConsumption[0] << " "
  18.                 << car.fuelConsumption[1] << " "
  19.                 << car.fuelConsumption[2] << std::endl;
  20.     stream << "Max speed: " << car.maxSpeed << std::endl;
  21.     stream << "Power: " << car.power << std::endl;
  22.     return stream;
  23. }
  24.  
  25. int write()
  26. {
  27.     size_t amount;
  28.     std::cin >> amount;
  29.  
  30.     auto array = new Car[amount];
  31.     for (size_t i = 0; i < amount; i++) {
  32.         Car &car = array[i];
  33.         std::cin.ignore();
  34.         std::getline(std::cin, car.model);
  35.         std::cin >> car.fuelConsumption[0];
  36.         std::cin >> car.fuelConsumption[1];
  37.         std::cin >> car.fuelConsumption[2];
  38.         std::cin >> car.maxSpeed;
  39.         std::cin >> car.power;
  40.     }
  41.  
  42.     std::ofstream outputText("file.txt");
  43.     outputText << amount << std::endl;
  44.     for (size_t i = 0; i < amount; i++) {
  45.         Car &car = array[i];
  46.         outputText << car.model << std::endl;
  47.         outputText << car.fuelConsumption[0] << " ";
  48.         outputText << car.fuelConsumption[1] << " ";
  49.         outputText << car.fuelConsumption[2] << std::endl;
  50.         outputText << car.maxSpeed << std::endl;
  51.         outputText << car.power << std::endl;
  52.     }
  53.  
  54.     std::ofstream outputBinary("file.bin", std::ios::binary);
  55.     outputBinary.write(reinterpret_cast<char *>(&amount), sizeof(size_t));
  56.     for (size_t i = 0; i < amount; i++) {
  57.         Car &car = array[i];
  58.         outputBinary << car.model << std::endl;
  59.         outputBinary.write(reinterpret_cast<char *>(&car.fuelConsumption), sizeof(double) * 3);
  60.         outputBinary.write(reinterpret_cast<char *>(&car.maxSpeed), sizeof(double));
  61.         outputBinary.write(reinterpret_cast<char *>(&car.power), sizeof(double));
  62.     }
  63.  
  64.     delete[] array;
  65.  
  66.     return 0;
  67. }
  68.  
  69. int read()
  70. {
  71.     std::ifstream inputText("file.txt");
  72.     size_t amount;
  73.     inputText >> amount;
  74.     auto array = new Car[amount];
  75.     for (size_t i = 0; i < amount; i++) {
  76.         Car &car = array[i];
  77.         inputText.ignore();
  78.         std::getline(inputText, car.model);
  79.         inputText >> car.fuelConsumption[0];
  80.         inputText >> car.fuelConsumption[1];
  81.         inputText >> car.fuelConsumption[2];
  82.         inputText >> car.maxSpeed;
  83.         inputText >> car.power;
  84.  
  85.         std::cout << car;
  86.     }
  87.     delete[] array;
  88.  
  89.     std::ifstream inputBinary("file.bin", std::ios::binary);
  90.     inputBinary.read(reinterpret_cast<char *>(&amount), sizeof(size_t));
  91.     array = new Car[amount];
  92.     for (size_t i = 0; i < amount; i++) {
  93.         Car &car = array[i];
  94.         std::getline(inputBinary, car.model);
  95.         inputBinary.read(reinterpret_cast<char *>(&car.fuelConsumption), sizeof(double) * 3);
  96.         inputBinary.read(reinterpret_cast<char *>(&car.maxSpeed), sizeof(double));
  97.         inputBinary.read(reinterpret_cast<char *>(&car.power), sizeof(double));
  98.  
  99.         std::cout << car;
  100.     }
  101.     delete[] array;
  102.  
  103.     return 0;
  104. }
  105.  
  106. int main()
  107. {
  108.    
  109.     size_t state;
  110.     do {
  111.         std::cout << "Read(0) or write(1): ";
  112.         std::cin >> state;
  113.         if (state == 0) {
  114.             read();
  115.         } else if (state == 1) {
  116.             write();
  117.         }
  118.     } while (state != 0 && state != 1);
  119.    
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement