Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <set>
- using namespace std;
- void printTask()
- {
- cout << "Вывести на печать числа которые делятся на 3 без остатка из множества, полученного пересечением 2-мя другими множ." << endl;
- }
- int askUserAboutInput() {
- int answer;
- bool isCorrect;
- cout << "If you want to enter data from the console - enter 0, read from the file - 1." << endl;
- do {
- isCorrect = true;
- cin >> answer;
- if (cin.fail())
- {
- isCorrect = false;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Check the correctness of the entered data." << endl;
- }
- if (isCorrect && cin.get() != '\n')
- {
- cin.clear();
- isCorrect = false;
- while (cin.get() != '\n');
- cout << "Check the correctness of the entered data." << endl;
- }
- if (isCorrect && (answer > 1 || answer < 0))
- {
- isCorrect = false;
- cout << "If you want to enter data from the console - enter 0, read from the file - 1." << endl;
- }
- } while (!isCorrect);
- return answer;
- }
- int askUserAboutOutput() {
- int answer;
- bool isCorrect;
- cout << "If you want to output the result to the console - enter 0, if you want to output to a file - enter 1.\n";
- do {
- isCorrect = true;
- cin >> answer;
- if (cin.fail())
- {
- isCorrect = false;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Check the correctness of the entered data." << endl;
- }
- if (isCorrect && cin.get() != '\n')
- {
- cin.clear();
- isCorrect = false;
- while (cin.get() != '\n');
- cout << "Check the correctness of the entered data." << endl;
- }
- if (isCorrect && (answer > 1 || answer < 0))
- {
- isCorrect = false;
- cout << "If you want to output the result to the console - enter 0, if you want to output to a file - enter 1." << endl;
- }
- } while (!isCorrect);
- return answer;
- }
- string getPath() {
- bool isCorrect;
- string path;
- cout << "Enter the full path to the file." << endl;
- do {
- isCorrect = true;
- getline(cin, path);
- ifstream file(path);
- if (!file.is_open())
- {
- cout << "Error! Check if the directory you entered is correct. \n";
- isCorrect = false;
- }
- } while (!isCorrect);
- return path;
- }
- int getSizeForSetFromConsole() {
- const int MAX = 15;
- const int MIN = 1;
- bool isCorrect;
- int size;
- cout << "Input amount number for set\n";
- do {
- isCorrect = true;
- cin >> size;
- if (cin.fail())
- {
- isCorrect = false;
- cin.clear();
- while (cin.get() != '\n');
- cout << "Check the correctness of the entered data." << endl;
- }
- if (isCorrect && cin.get() != '\n')
- {
- cin.clear();
- isCorrect = false;
- while (cin.get() != '\n');
- cout << "Check the correctness of the entered data." << endl;
- }
- if (isCorrect && (size > MAX || size < MIN)) {
- isCorrect = false;
- cout << "You have gone beyond the range [1..15].\n";
- }
- } while (!isCorrect);
- return size;
- }
- set<int> getSetFromConsole() {
- int k;
- int size = getSizeForSetFromConsole();
- bool isCorrect;
- set<int> mst;
- for (int i = 0; i < size; i++) {
- do {
- cout << "Enter " << i + 1 << " number = ";
- isCorrect = true;
- cin >> k;
- if (cin.fail()) {
- cin.clear();
- isCorrect = false;
- while (cin.get() != '\n');
- cout << "Check the correctness of the data.\n";
- }
- if (isCorrect && cin.get() != '\n') {
- cin.clear();
- isCorrect = false;
- while (cin.get() != '\n');
- cout << "Check the correctness of the data.\n";
- }
- } while (!isCorrect);
- mst.insert(k);
- }
- return mst;
- }
- void printConsole(set<int> mst) {
- cout << "set = { ";
- for (int elem : mst) {
- cout << elem << " ";
- }
- cout << "}\n\n";
- }
- int getSizeForSetFromFile(string path) {
- const int MAX = 15;
- const int MIN = 1;
- int size;
- bool isCorrect;
- ifstream fin;
- do {
- isCorrect = true;
- fin.open(path);
- fin >> size;
- if (fin.fail()) {
- isCorrect = false;
- fin.clear();
- cout << "Check the correctness of the entered data from the file.\n";
- path = getPath();
- }
- if (isCorrect && (size > MAX || size < MIN)) {
- isCorrect = false;
- cout << "check the correctness of the entered data from the file.\n";
- path = getPath();
- }
- //fin.close();
- } while (!isCorrect);
- return size;
- }
- set<int> getSetFromFile(string path) {
- bool isCorrect;
- string temp;
- set<int> mySet;
- int num;
- int size = getSizeForSetFromFile(path);
- ifstream fin;
- isCorrect = true;
- fin.open(path);
- fin >> temp;
- for (int i = 0; i < size; i++) {
- do {
- isCorrect = true;
- fin >> num;
- if (fin.fail()) {
- isCorrect = false;
- cout << "Invalid data in the file.\n";
- path = getPath();
- }
- } while (!isCorrect);
- mySet.insert(num);
- }
- //fin.close();
- return mySet;
- }
- set<int> getNewSet(set<int> firstSet, set<int> secondSet) {
- set<int> fourthSet;
- for (int element1 : firstSet)
- for (int element2 : secondSet)
- if (element1 == element2)
- fourthSet.insert(element1);
- return fourthSet;
- }
- set<int> getSetForDivision(set<int> fourthSet) {
- set<int> fifthSet;
- for (int element : fourthSet)
- if (element % 3 == 0)
- fifthSet.insert(element);
- return fifthSet;
- }
- int subsetOf(set<int> thirdSet, set<int> fourthSet) {
- int count = 0;
- for (int elem1 : thirdSet)
- for (int elem2 : fourthSet)
- if (elem1 == elem2)
- count++;
- return count;
- }
- void outputToConsole(set<int> fifthSet, set<int> thirdSet, int count) {
- if (fifthSet.size() == 0)
- cout << "No numbers\n";
- else {
- cout << "числа нового множества, которые делятся на 3 без остатка : ";
- for (int elem : fifthSet)
- cout << elem << " ";
- }
- cout << "\n\n";
- if (count == thirdSet.size())
- cout << "True. Set3 input at set4\n";
- else
- cout << "False. Not input\nТретье множество не является подмножеством в новом множестве";
- }
- void outputToFile(set<int> fifthSet, set<int> thirdSet, int count) {
- string path = getPath();
- ofstream fout;
- fout.open(path);
- if (fifthSet.size() == 0)
- fout << "No numbers\n";
- else {
- fout << "/3 : ";
- for (int elem : fifthSet)
- fout << elem << " ";
- }
- fout << "\n\n";
- if (count == thirdSet.size())
- fout << "True. Set3 input at set4\nТретье множество является подмножеством нового множества";
- else
- fout << "False. Not input\nТретье множество не является подмножеством в новом множестве";
- fout.close();
- }
- int main()
- {
- string path;
- set<int> firstSetOfInteger;
- set<int> secondSetOfInteger;
- set<int> thirdOfSetInteger;
- set<int> fourthSetOfInteger;
- set<int> fifthSetOfInteger;
- printTask();
- if (askUserAboutInput() == 1) {
- path = getPath();
- firstSetOfInteger = getSetFromFile(path);
- printConsole(firstSetOfInteger);
- secondSetOfInteger = getSetFromFile(path);
- printConsole(secondSetOfInteger);
- thirdOfSetInteger = getSetFromFile(path);
- printConsole(thirdOfSetInteger);
- }
- else {
- firstSetOfInteger = getSetFromConsole();
- printConsole(firstSetOfInteger);
- secondSetOfInteger = getSetFromConsole();
- printConsole(secondSetOfInteger);
- thirdOfSetInteger = getSetFromConsole();
- printConsole(thirdOfSetInteger);
- }
- fourthSetOfInteger = getNewSet(firstSetOfInteger, secondSetOfInteger);
- cout << "new set4 = { ";
- for (int elem : fourthSetOfInteger)
- cout << elem << " ";
- cout << "}\n\n";
- fifthSetOfInteger = getSetForDivision(fourthSetOfInteger);
- int count = subsetOf(fourthSetOfInteger, thirdOfSetInteger);
- if (askUserAboutOutput() == 1) {
- outputToFile(fifthSetOfInteger, thirdOfSetInteger, count);
- }
- else {
- outputToConsole(fifthSetOfInteger, thirdOfSetInteger, count);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement