Xom9ik

Lab_10/6 var (IIl semester)

Dec 3rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. //Lab_10_var8
  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 Schoolboy
  11. {
  12.     string name, surname, Сlass;
  13.     string phone;
  14.     int grades[4];
  15. };
  16. Schoolboy x[100];
  17. int countRow = 0;
  18. void outputScreen()
  19. {
  20.     cout << " Structure Schoolboy" << endl;
  21.     cout << " __________________________________________________________________________" << endl;
  22.     cout << " |№ |    Name    |  Surname   |    Сlass   |    Phone    | M | P | R | L | " << endl;
  23.     cout << " |--|------------|------------|------------|-------------|---|---|---|---|" << endl;
  24.     for (int i = 0; i < countRow; i++)
  25.     {
  26.         cout << left << " |" << setw(2) << i + 1 << "|" << setw(12) << x[i].name << "|" << setw(12)
  27.             << x[i].surname << "|" << setw(12) << x[i].Сlass << "|" << setw(13) << x[i].phone
  28.             << "|" << setw(3) << x[i].grades[0] << "|" << setw(3) << x[i].grades[1] << "|"
  29.             << setw(3) << x[i].grades[2] << "|" << setw(3) << x[i].grades[3] << "|" << endl;
  30.     }
  31.     cout << " __________________________________________________________________________" << endl;
  32. }
  33. void inputFile()
  34. {
  35.     ifstream input;
  36.     input.open("Input.txt", ios::in);
  37.     if (input.fail()) {
  38.         cout << "Could not open file" << endl;
  39.         system("pause");
  40.     }
  41.     do
  42.     {
  43.         input >> x[countRow].name >> x[countRow].surname >> x[countRow].Сlass >> x[countRow].phone >> x[countRow].grades[0]
  44.             >> x[countRow].grades[1] >> x[countRow].grades[2] >> x[countRow].grades[3];
  45.         countRow++;
  46.     } while (!input.eof());
  47.     input.close();
  48.     cout << "\nFile read out. Number of items = " << countRow << endl;
  49.     outputScreen();
  50. }
  51.  
  52. void outputFile()
  53. {
  54.     ofstream output;
  55.     output.open("Output.txt", ios::out);
  56.     if (output.fail())
  57.     {
  58.         cout << "Could not create file" << endl;
  59.         system("pause");
  60.     }
  61.     for (int i = 0; i<countRow; i++)
  62.     {
  63.         output << setw(10) << x[i].surname << " " << setw(10) << x[i].name << " " << setw(10) << x[i].Сlass << " " << setw(11) << x[i].phone << " " << setw(11) << x[i].grades;
  64.         if (i != countRow - 1)
  65.             output << endl;
  66.         if (output.fail())
  67.             break;
  68.     }
  69.     output.close();
  70.     cout << "File created" << endl;
  71. }
  72.  
  73. void addSchoolboy()
  74. {
  75.     int countAddRow;
  76.     cout << "How many lines to add: ";
  77.     cin >> countAddRow;
  78.     for (int j = 0; j<countAddRow; j++)
  79.     {
  80.         cout << "-Enter Schoolboy Data-" << endl;
  81.         cout << "Surname: ";
  82.         cin >> x[countRow].surname;
  83.         cout << "Name: ";
  84.         cin >> x[countRow].name;
  85.         cout << "Сlass: ";
  86.         cin >> x[countRow].Сlass;
  87.         cout << "Phone: ";
  88.         cin >> x[countRow].phone;
  89.         cout << "Mathematics: ";
  90.         cin >> x[countRow].grades[0];
  91.         cout << "Physics: ";
  92.         cin >> x[countRow].grades[1];
  93.         cout << "Russian language: ";
  94.         cin >> x[countRow].grades[2];
  95.         cout << "Literature: ";
  96.         cin >> x[countRow].grades[3];
  97.         countRow++;
  98.     }
  99.     outputScreen();
  100. }
  101.  
  102.  
  103. void deleteSchoolboy()
  104. {
  105.     int ptr = 0;
  106.     for (int i = 0; i < countRow; i++)
  107.     {
  108.         for (int j = 0; j < 4; j++)
  109.         {
  110.             if (x[i].grades[j] == 2)
  111.             {
  112.                 for (int ii = i; ii <= countRow; ii++)
  113.                     x[ii] = x[ii + 1];
  114.                 i--;
  115.                 countRow--;
  116.             }      
  117.         }
  118.     }
  119.     outputScreen();
  120. }
  121.  
  122. int main()
  123. {
  124.     SetConsoleCP(1251);
  125.     SetConsoleOutputCP(1251);
  126.     setlocale(LC_ALL, 0);
  127.     int option;
  128.     cout << "Structure Schoolboy" << endl;
  129.     while (1)
  130.     {
  131.         cout << "1-Input from a text file" << endl;
  132.         cout << "2-View data" << endl;
  133.         cout << "3-Saving to text file" << endl;
  134.         cout << "4-Add K elements to the end of the file" << endl;
  135.         cout << "5-Delete all items that have 2 at least one school subject" << endl;
  136.         cout << "6-Exit" << endl;
  137.         cout << "Your choice (1-6): ";
  138.         cin >> option;
  139.         switch (option)
  140.         {
  141.         case 1: inputFile(); break;
  142.         case 2: outputScreen(); break;
  143.         case 3: outputFile(); break;
  144.         case 4: addSchoolboy(); break;
  145.         case 5: deleteSchoolboy(); break;
  146.         case 6: exit(0);
  147.         default: cout << "Invalid value entered" << endl;
  148.             break;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment