Advertisement
SmnVadik

№25 Lab3_2

Dec 6th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <set>
  5. using namespace std;
  6.  
  7.  
  8.  
  9. void printTask()
  10. {
  11.     cout << "Вывести на печать числа которые делятся на 3 без остатка из множества, полученного пересечением 2-мя другими множ." << endl;
  12. }
  13.  
  14. int askUserAboutInput() {
  15.     int answer;
  16.     bool isCorrect;
  17.     cout << "If you want to enter data from the console - enter 0, read from the file - 1." << endl;
  18.         do {
  19.             isCorrect = true;
  20.             cin >> answer;
  21.             if (cin.fail())
  22.             {
  23.                 isCorrect = false;
  24.                 cin.clear();
  25.                 while (cin.get() != '\n');
  26.                 cout << "Check the correctness of the entered data." << endl;
  27.             }
  28.             if (isCorrect && cin.get() != '\n')
  29.             {
  30.                 cin.clear();
  31.                 isCorrect = false;
  32.                 while (cin.get() != '\n');
  33.                 cout << "Check the correctness of the entered data." << endl;
  34.             }
  35.             if (isCorrect && (answer > 1 || answer < 0))
  36.             {
  37.                 isCorrect = false;
  38.                 cout << "If you want to enter data from the console - enter 0, read from the file - 1." << endl;
  39.             }
  40.         } while (!isCorrect);
  41.         return answer;
  42. }
  43.  
  44. int askUserAboutOutput() {
  45.     int answer;
  46.     bool isCorrect;
  47.     cout << "If you want to output the result to the console - enter 0, if you want to output to a file - enter 1.\n";
  48.         do {
  49.             isCorrect = true;
  50.             cin >> answer;
  51.             if (cin.fail())
  52.             {
  53.                 isCorrect = false;
  54.                 cin.clear();
  55.                 while (cin.get() != '\n');
  56.                 cout << "Check the correctness of the entered data." << endl;
  57.             }
  58.             if (isCorrect && cin.get() != '\n')
  59.             {
  60.                 cin.clear();
  61.                 isCorrect = false;
  62.                 while (cin.get() != '\n');
  63.                 cout << "Check the correctness of the entered data." << endl;
  64.             }
  65.             if (isCorrect && (answer > 1 || answer < 0))
  66.             {
  67.                 isCorrect = false;
  68.                 cout << "If you want to output the result to the console - enter 0, if you want to output to a file - enter 1." << endl;
  69.             }
  70.         } while (!isCorrect);
  71.         return answer;
  72. }
  73.  
  74. string getPath() {
  75.     bool isCorrect;
  76.     string path;
  77.     cout << "Enter the full path to the file." << endl;
  78.     do {
  79.         isCorrect = true;
  80.         getline(cin, path);
  81.         ifstream file(path);
  82.         if (!file.is_open())
  83.         {
  84.             cout << "Error! Check if the directory you entered is correct. \n";
  85.             isCorrect = false;
  86.         }
  87.     } while (!isCorrect);
  88.     return path;
  89. }
  90.  
  91.  
  92. int getSizeForSetFromConsole() {
  93.     const int MAX = 15;
  94.     const int MIN = 1;
  95.     bool isCorrect;
  96.     int size;
  97.     cout << "Input amount number for set\n";
  98.     do {
  99.         isCorrect = true;
  100.         cin >> size;
  101.         if (cin.fail())
  102.         {
  103.             isCorrect = false;
  104.             cin.clear();
  105.             while (cin.get() != '\n');
  106.             cout << "Check the correctness of the entered data." << endl;
  107.         }
  108.         if (isCorrect && cin.get() != '\n')
  109.         {
  110.             cin.clear();
  111.             isCorrect = false;
  112.             while (cin.get() != '\n');
  113.             cout << "Check the correctness of the entered data." << endl;
  114.         }
  115.         if (isCorrect && (size > MAX || size < MIN)) {
  116.             isCorrect = false;
  117.             cout << "You have gone beyond the range [1..15].\n";
  118.         }
  119.     } while (!isCorrect);
  120.     return size;
  121. }
  122.  
  123. set<int> getSetFromConsole() {
  124.     int k;
  125.     int size = getSizeForSetFromConsole();
  126.     bool isCorrect;
  127.     set<int> mst;
  128.     for (int i = 0; i < size; i++) {
  129.         do {
  130.             cout << "Enter " << i + 1 << " number = ";
  131.             isCorrect = true;
  132.             cin >> k;
  133.             if (cin.fail()) {
  134.                 cin.clear();
  135.                 isCorrect = false;
  136.                 while (cin.get() != '\n');
  137.                 cout << "Check the correctness of the data.\n";
  138.             }
  139.             if (isCorrect && cin.get() != '\n') {
  140.                 cin.clear();
  141.                 isCorrect = false;
  142.                 while (cin.get() != '\n');
  143.                 cout << "Check the correctness of the data.\n";
  144.             }
  145.         } while (!isCorrect);
  146.         mst.insert(k);
  147.     }
  148.     return mst;
  149. }
  150.  
  151. void printConsole(set<int> mst) {
  152.     cout << "set = { ";
  153.     for (int elem : mst) {
  154.         cout << elem << " ";
  155.     }
  156.     cout << "}\n\n";
  157. }
  158.  
  159.  
  160. int getSizeForSetFromFile(string path) {
  161.     const int MAX = 15;
  162.     const int MIN = 1;
  163.     int size;
  164.     bool isCorrect;
  165.     ifstream fin;
  166.     do {
  167.         isCorrect = true;
  168.         fin.open(path);
  169.         fin >> size;
  170.         if (fin.fail()) {
  171.             isCorrect = false;
  172.             fin.clear();
  173.             cout << "Check the correctness of the entered data from the file.\n";
  174.             path = getPath();
  175.         }
  176.         if (isCorrect && (size > MAX || size < MIN)) {
  177.             isCorrect = false;
  178.             cout << "check the correctness of the entered data from the file.\n";
  179.             path = getPath();
  180.         }
  181.         //fin.close();
  182.     } while (!isCorrect);
  183.     return size;
  184. }
  185.  
  186.  
  187. set<int> getSetFromFile(string path) {
  188.     bool isCorrect;
  189.     string temp;
  190.     set<int> mySet;
  191.     int num;
  192.     int size = getSizeForSetFromFile(path);
  193.     ifstream fin;
  194.     isCorrect = true;
  195.     fin.open(path);
  196.     fin >> temp;
  197.     for (int i = 0; i < size; i++) {
  198.         do {
  199.             isCorrect = true;
  200.             fin >> num;
  201.             if (fin.fail()) {
  202.                 isCorrect = false;
  203.                 cout << "Invalid data in the file.\n";
  204.                 path = getPath();
  205.             }
  206.         } while (!isCorrect);
  207.         mySet.insert(num);
  208.     }
  209.     //fin.close();
  210.     return mySet;
  211. }
  212.  
  213.  
  214. set<int> getNewSet(set<int> firstSet, set<int> secondSet) {
  215.     set<int> fourthSet;
  216.     for (int element1 : firstSet)
  217.         for (int element2 : secondSet)
  218.             if (element1 == element2)
  219.                 fourthSet.insert(element1);
  220.     return fourthSet;
  221. }
  222.  
  223.  
  224. set<int> getSetForDivision(set<int> fourthSet) {
  225.     set<int> fifthSet;
  226.     for (int element : fourthSet)
  227.         if (element % 3 == 0)
  228.             fifthSet.insert(element);
  229.     return fifthSet;
  230. }
  231.  
  232.  
  233. int subsetOf(set<int> thirdSet, set<int> fourthSet) {
  234.     int count = 0;
  235.     for (int elem1 : thirdSet)
  236.         for (int elem2 : fourthSet)
  237.             if (elem1 == elem2)
  238.                 count++;
  239.     return count;
  240. }
  241.  
  242.  
  243. void outputToConsole(set<int> fifthSet, set<int> thirdSet, int count) {
  244.     if (fifthSet.size() == 0)
  245.         cout << "No numbers\n";
  246.     else {
  247.         cout << "числа нового множества, которые делятся на 3 без остатка :  ";
  248.         for (int elem : fifthSet)
  249.             cout << elem << " ";
  250.     }
  251.     cout << "\n\n";
  252.  
  253.     if (count == thirdSet.size())
  254.         cout << "True. Set3 input at set4\n";
  255.     else
  256.         cout << "False. Not input\nТретье множество не является подмножеством в новом множестве";
  257. }
  258.  
  259. void outputToFile(set<int> fifthSet, set<int> thirdSet, int count) {
  260.     string path = getPath();
  261.     ofstream fout;
  262.     fout.open(path);
  263.     if (fifthSet.size() == 0)
  264.         fout << "No numbers\n";
  265.     else {
  266.         fout << "/3 :  ";
  267.         for (int elem : fifthSet)
  268.             fout << elem << " ";
  269.     }
  270.     fout << "\n\n";
  271.  
  272.     if (count == thirdSet.size())
  273.         fout << "True. Set3 input at set4\nТретье множество является подмножеством нового множества";
  274.     else
  275.         fout << "False. Not input\nТретье множество не является подмножеством в новом множестве";
  276.     fout.close();
  277. }
  278.  
  279.  
  280. int main()
  281. {
  282.     string path;
  283.     set<int> firstSetOfInteger;
  284.     set<int> secondSetOfInteger;
  285.     set<int> thirdOfSetInteger;
  286.     set<int> fourthSetOfInteger;
  287.     set<int> fifthSetOfInteger;
  288.  
  289.  
  290.     printTask();
  291.     if (askUserAboutInput() == 1) {
  292.         path = getPath();
  293.         firstSetOfInteger = getSetFromFile(path);
  294.         printConsole(firstSetOfInteger);
  295.         secondSetOfInteger = getSetFromFile(path);
  296.         printConsole(secondSetOfInteger);
  297.         thirdOfSetInteger = getSetFromFile(path);
  298.         printConsole(thirdOfSetInteger);
  299.     }
  300.     else {
  301.         firstSetOfInteger = getSetFromConsole();
  302.         printConsole(firstSetOfInteger);
  303.         secondSetOfInteger = getSetFromConsole();
  304.         printConsole(secondSetOfInteger);
  305.         thirdOfSetInteger = getSetFromConsole();
  306.         printConsole(thirdOfSetInteger);
  307.     }
  308.  
  309.     fourthSetOfInteger = getNewSet(firstSetOfInteger, secondSetOfInteger);
  310.     cout << "new set4 = { ";
  311.     for (int elem : fourthSetOfInteger)
  312.         cout << elem << " ";
  313.     cout << "}\n\n";
  314.  
  315.     fifthSetOfInteger = getSetForDivision(fourthSetOfInteger);
  316.  
  317.     int count = subsetOf(fourthSetOfInteger, thirdOfSetInteger);
  318.  
  319.     if (askUserAboutOutput() == 1) {
  320.         outputToFile(fifthSetOfInteger, thirdOfSetInteger, count);
  321.     }
  322.     else {
  323.         outputToConsole(fifthSetOfInteger, thirdOfSetInteger, count);
  324.     }
  325.  
  326.     return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement