Advertisement
Xom9ik

Lab_10/16 var (IIl semester)

Nov 18th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. //Lab_10.cpp
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <windows.h>
  6. #include <fstream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. struct ownerСar
  11. {
  12.     string name, surname, carNumber;
  13.     string phone, technicalNumber;
  14. };
  15. ownerСar x[100];
  16. int countRow = 0;
  17. void outputScreen()
  18. {
  19.     cout << " Structure Car owner" << endl;
  20.     cout << " __________________________________________________________________________" << endl;
  21.     cout << " |№ |    Name    |  Surname   | Сar number |    Phone    |Technical number| " << endl;
  22.     cout << " |--|------------|------------|------------|-------------|----------------|" << endl;
  23.     for (int i = 0; i < countRow; i++)
  24.     {
  25.         cout << left << " |" << setw(2) << i + 1 << "|" << setw(12) << x[i].name << "|" << setw(12)
  26.             << x[i].surname << "|"<< setw(12) << x[i].carNumber << "|" << setw(13) << x[i].phone
  27.             << "|" << setw(16) << x[i].technicalNumber << "|" << endl;
  28.     }
  29.        
  30.     cout << " __________________________________________________________________________" << endl;
  31. }
  32. void inputFile()
  33. {
  34.     ifstream input;
  35.     input.open("Input.txt", ios::in);
  36.     if (input.fail()){
  37.         cout << "Could not open file" << endl;
  38.         system("pause");
  39.     }
  40.     do
  41.     {
  42.         input >> x[countRow].name >> x[countRow].surname >> x[countRow].carNumber >> x[countRow].phone >> x[countRow].technicalNumber;
  43.         countRow++;
  44.     } while(!input.eof());
  45.     input.close();
  46.     cout << "\nFile read out. Number of items = " << countRow << endl;
  47.     outputScreen();
  48. }
  49.  
  50. void outputFile()
  51. {
  52.     ofstream output;
  53.     output.open("Output.txt", ios::out);
  54.     if (output.fail())
  55.     {
  56.         cout << "Could not create file" << endl;
  57.         system("pause");
  58.     }
  59.     for (int i = 0; i<countRow; i++)
  60.     {
  61.         output << setw(10) << x[i].surname << " " << setw(10) << x[i].name << " " << setw(10) << x[i].carNumber << " " << setw(11) << x[i].phone << " " << setw(11) << x[i].technicalNumber;
  62.         if (i != countRow - 1)
  63.             output << endl;
  64.         if (output.fail())
  65.             break;
  66.     }
  67.     output.close();
  68.     cout << "File created" << endl;
  69. }
  70.  
  71. void addOwner()
  72. {
  73.     int countAddRow;
  74.     string tmpNumber;
  75.     cout << "How many lines to add: ";
  76.     cin >> countAddRow;
  77.     cout << "Enter vehicle number: ";
  78.     cin >> tmpNumber;
  79.     for (int j = 0; j<countAddRow; j++)
  80.     {
  81.         cout << "-Enter Vehicle Owner Data-" << endl;
  82.         cout << "Surname: ";
  83.         cin >> x[countRow].surname;
  84.         cout << "Name: ";
  85.         cin >> x[countRow].name;
  86.         x[countRow].carNumber = tmpNumber;
  87.         cout << "Phone: ";
  88.         cin >> x[countRow].phone;
  89.         cout << "Technical document number: ";
  90.         cin >> x[countRow].technicalNumber;
  91.         countRow++;
  92.     }
  93.     cout << countAddRow << " owners with number " << tmpNumber << " added" << endl;
  94.     outputScreen();
  95. }
  96.  
  97.  
  98. void deleteOwner()
  99. {
  100.     string tmpNumber;
  101.     ownerСar tempStruct;
  102.     cout << "Enter the number of the car: ";
  103.     cin >> tmpNumber;
  104.     for (int i = 0; i < countRow; i++)
  105.     {
  106.         if (tmpNumber == x[i].carNumber)
  107.         {
  108.             cout << tmpNumber << "==" << x[i].carNumber << endl;
  109.             for (int j = i; j < countRow - 1; j++)
  110.             {
  111.                 x[i] = x[i + 1];
  112.             }
  113.             countRow--;
  114.             cout << "Owner deleted" << endl;
  115.         }
  116.         else if (i == countRow - 1)
  117.             cout << "There is no such number" << endl;
  118.     }
  119.     outputScreen();
  120. }
  121.  
  122. int main()
  123. {
  124.     SetConsoleCP(1251);
  125.     SetConsoleOutputCP(1251);
  126.     int option;
  127.     cout << "Structure Car owner" << endl;
  128.     while (1)
  129.     {
  130.         cout << "1-Input from a text file" << endl;
  131.         cout << "2-View data" << endl;
  132.         cout << "3-Saving to text file" << endl;
  133.         cout << "4-Adding N elements with the number K" << endl;
  134.         cout << "5-Deleting an item with a specified number" << endl;
  135.         cout << "6-Exit" << endl;
  136.         cout << "Your choice (1-6): ";
  137.         cin >> option;
  138.         switch (option)
  139.         {
  140.         case 1: inputFile(); break;
  141.         case 2: outputScreen(); break;
  142.         case 3: outputFile(); break;
  143.         case 4: addOwner(); break;
  144.         case 5: deleteOwner(); break;
  145.         case 6: exit(0);
  146.         default: cout << "Invalid value entered" << endl;
  147.              break;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement