Xom9ik

Lab_10/9 var (IIl semester)

Dec 11th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <windows.h>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. struct Patient
  11. {
  12.     string name, surname, middlename;
  13.     string address;
  14.     int medicalCard, insurancePolicy;
  15. };
  16.  
  17. Patient patient[100];
  18. int countRow = 0;
  19.  
  20. void outputScreen()
  21. {
  22.     cout << " Structure Patient" << endl;
  23.     cout << " ___________________________________________________________________________________________" << endl;
  24.     cout << " |№ |    Name    |  Surname   |   Middle   |   Address   |  Medical Card  |Insurance Policy| " << endl;
  25.     cout << " |--|------------|------------|------------|-------------|----------------|----------------|" << endl;
  26.     for (int i = 0; i < countRow; i++)
  27.         cout << left << " |" << setw(2) << i + 1 << "|" << setw(12) << patient[i].name << "|" << setw(12)
  28.             << patient[i].surname << "|" << setw(12) << patient[i].middlename << "|" << setw(13) << patient[i].address
  29.             << "|" << setw(16) << to_string(patient[i].medicalCard) << "|" << to_string(patient[i].insurancePolicy) << "|" << endl;
  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 >> patient[countRow].name >> patient[countRow].surname >> patient[countRow].middlename >> patient[countRow].address >> patient[countRow].medicalCard >> patient[countRow].insurancePolicy;
  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) << patient[i].surname << " " << setw(10) << patient[i].name << " " << setw(10) << patient[i].middlename << " " << setw(11) << patient[i].address << " " << setw(11) << patient[i].medicalCard;
  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 addPatient()
  72. {
  73.     int countAddRow;
  74.     cout << "How many lines to add: ";
  75.     cin >> countAddRow;
  76.     for (int i = countRow + countAddRow; i <= countAddRow - 1; i--)
  77.         patient[i + 1] = patient[i];
  78.     Patient tmp[100];
  79.     for (int i = 0; i < countRow; i++)
  80.     {
  81.         tmp[i+countAddRow] = patient[i];
  82.     }
  83.     for (int i = 0; i<countAddRow; i++)
  84.     {
  85.         cout << "-Enter Patient Data [" << i+1 << "]-" << endl;
  86.         cout << "Name: ";
  87.         cin >> tmp[i].name;
  88.         cout << "Surname: ";
  89.         cin >> tmp[i].surname;
  90.         cout << "Middle Name: ";
  91.         cin >> tmp[i].middlename;
  92.         cout << "Address: ";
  93.         getline(cin, tmp[i].address);
  94.         cout << "Medical Card: ";
  95.         cin >> tmp[i].medicalCard;
  96.         cout << "Insurance Policy: ";
  97.         cin >> tmp[i].insurancePolicy;
  98.         countRow++;
  99.     }
  100.     for (int i = 0; i < countRow; i++)
  101.     {
  102.         cout << "Save" << tmp[i].name << endl;
  103.         patient[i] = tmp[i];
  104.     }
  105.     outputScreen();
  106.     cout << countAddRow << " items added to the beginning of the file" << endl;
  107.     cout << "Changes are written to the Output.txt file" << endl;
  108.     outputFile();
  109. }
  110.  
  111. void deletePatient()
  112. {
  113.     int value;
  114.     cout << "Enter the medical card number: ";
  115.     cin >> value;
  116.     for (int i = 0; i < countRow; i++)
  117.     {
  118.         if (value == patient[i].medicalCard)
  119.         {
  120.             for (int j = i; j <= countRow; j++)
  121.                 patient[j] = patient[j+1];
  122.             cout << "Patient deleted" << endl;
  123.             countRow--;
  124.         }
  125.         else if (i == countRow - 1)
  126.             cout << "There is no such medical card number" << endl;
  127.     }
  128.     outputScreen();
  129.     cout << "Changes are written to the Output.txt file" << endl;
  130.     outputFile();
  131. }
  132.  
  133. int main()
  134. {
  135.     SetConsoleCP(1251);
  136.     SetConsoleOutputCP(1251);
  137.     int option;
  138.     cout << "Structure Patient" << endl;
  139.     while (1)
  140.     {
  141.         cout << "1-Input from a txt file (Input.txt)" << endl;
  142.         cout << "2-View data" << endl;
  143.         cout << "3-Saving to txt file (Output.txt)" << endl;
  144.         cout << "4-Add K elements to the beginning of the file" << endl;
  145.         cout << "5-Delete an item with the specified medical card number." << endl;
  146.         cout << "6-Exit" << endl;
  147.         cout << "Your choice (1-6): ";
  148.         cin >> option;
  149.         switch (option)
  150.         {
  151.         case 1: inputFile(); break;
  152.         case 2: outputScreen(); break;
  153.         case 3: outputFile(); break;
  154.         case 4: addPatient(); break;
  155.         case 5: deletePatient(); break;
  156.         case 6: exit(0);
  157.         default: cout << "Invalid value entered" << endl;
  158.             break;
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment