Xom9ik

Lab_10/3 var (IIl semester)

Dec 4th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 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 theState
  11. {
  12.     string name, capital;
  13.     int area, population;
  14. };
  15. theState state[100];
  16. int countRow = 0;
  17. void outputScreen()
  18. {
  19.     cout << " Structure The State" << endl;
  20.     cout << " _________________________________________________________" << endl;
  21.     cout << " |№ |    Name    |  Capital   | Population |    Area     | " << endl;
  22.     cout << " |--|------------|------------|------------|-------------|" << endl;
  23.     for (int i = 0; i < countRow; i++)
  24.         cout << left << " |" << setw(2) << i + 1 << "|" << setw(12) << state[i].name << "|" << setw(12)
  25.             << state[i].capital << "|" << setw(12) << state[i].population << "|" << setw(13) << state[i].area << "|" << endl;
  26.     cout << " _________________________________________________________" << endl;
  27. }
  28. void inputFile()
  29. {
  30.     ifstream input;
  31.     input.open("Input.txt", ios::in);
  32.     if (input.fail()) {
  33.         cout << "Could not open file" << endl;
  34.         system("pause");
  35.     }
  36.     do
  37.     {
  38.         input >> state[countRow].name >> state[countRow].capital >> state[countRow].population >> state[countRow].area;
  39.         countRow++;
  40.     } while (!input.eof());
  41.     input.close();
  42.     cout << "\nFile read out. Number of items = " << countRow << endl;
  43.     outputScreen();
  44. }
  45.  
  46. void outputFile()
  47. {
  48.     ofstream output;
  49.     output.open("Output.txt", ios::out);
  50.     if (output.fail())
  51.     {
  52.         cout << "Could not create file" << endl;
  53.         system("pause");
  54.     }
  55.     for (int i = 0; i<countRow; i++)
  56.     {
  57.         output << setw(15) << state[i].name << " " << setw(15) << state[i].capital << " " << setw(15) << state[i].population << " " << setw(15) << state[i].area;
  58.         if (i != countRow - 1)
  59.             output << endl;
  60.         if (output.fail())
  61.             break;
  62.     }
  63.     output.close();
  64.     cout << "File created" << endl;
  65. }
  66.  
  67. void addState()
  68. {
  69.     int countAddRow;
  70.     int tmpArea;
  71.     cout << "How many lines to add: ";
  72.     cin >> countAddRow;
  73.     cout << "Enter area: ";
  74.     cin >> tmpArea;
  75.     for (int j = 0; j<countAddRow; j++)
  76.     {
  77.         cout << "-Enter State Data-" << endl;
  78.        
  79.         cout << "Name: ";
  80.         cin >> state[countRow].name;
  81.         cout << "Capital: ";
  82.         cin >> state[countRow].capital;
  83.         cout << "Population: ";
  84.         cin >> state[countRow].population;
  85.         state[countRow].area = tmpArea;
  86.         countRow++;
  87.     }
  88.     cout << countAddRow << " state with area " << tmpArea << " added" << endl;
  89.     outputScreen();
  90.     cout << "Saving file" << endl;
  91.     outputFile();
  92. }
  93.  
  94. void deleteState()
  95. {
  96.     int findPopulation;
  97.     theState tmp;
  98.     cout << "Enter the population: ";
  99.     cin >> findPopulation;
  100.     for (int i = 0; i < countRow; i++)
  101.     {
  102.         if (findPopulation > state[i].population)
  103.         {
  104.             cout << findPopulation << ">" << state[i].population << " - delete" << endl;
  105.             for (int ii = i; ii <= countRow; ii++)
  106.                 state[ii] = state[ii + 1];
  107.             i--;
  108.             countRow--;
  109.         }
  110.     }
  111.     outputScreen();
  112.     cout << "Saving file" << endl;
  113.     outputFile();
  114. }
  115.  
  116. int main()
  117. {
  118.     SetConsoleCP(1251);
  119.     SetConsoleOutputCP(1251);
  120.     int option;
  121.     cout << "Structure The State" << endl;
  122.     while (1)
  123.     {
  124.         cout << "1-Input from a txt file" << endl;
  125.         cout << "2-View data" << endl;
  126.         cout << "3-Saving to txt file" << endl;
  127.         cout << "4-Adding N elements with the number K" << endl;
  128.         cout << "5-Delete all items that have fewer than the specified number" << endl;
  129.         cout << "6-Exit" << endl;
  130.         cout << "Your choice (1-6): ";
  131.         cin >> option;
  132.         switch (option)
  133.         {
  134.         case 1: inputFile(); break;
  135.         case 2: outputScreen(); break;
  136.         case 3: outputFile(); break;
  137.         case 4: addState(); break;
  138.         case 5: deleteState(); break;
  139.         case 6: exit(0);
  140.         default: cout << "Invalid value entered" << endl;
  141.             break;
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment