Advertisement
Egor_Vakar

lab3_2(C++)

Nov 20th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <set>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const set <char> setOfDigitsAndOperations { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', ':', '^'};
  10.  
  11. char takeSource() {
  12.     char choice;
  13.     cin >> choice;
  14.     while ((choice != '1') && (choice != '2')) {
  15.         while (cin.get() != '\n') {
  16.             cin.clear();
  17.         }
  18.         cout << "Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ";
  19.         cin >> choice;
  20.     }
  21.     return choice;
  22. }
  23.  
  24. string takeInPath() {
  25.     string path;
  26.     bool isIncorrect;
  27.     cout << "Enter file path: ";
  28.     do {
  29.         isIncorrect = true;
  30.         cin >> path;
  31.         ifstream fin(path);
  32.         if (fin.is_open()) {
  33.             isIncorrect = false;
  34.             fin.close();
  35.         }
  36.         else {
  37.             cout << "File is not found\nEnter file path: ";
  38.         }
  39.         if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
  40.             cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
  41.             isIncorrect = true;
  42.         }
  43.     } while (isIncorrect);
  44.     return path;
  45. }
  46.  
  47. string takeOutPath() {
  48.     string path;
  49.     bool isIncorrect;
  50.     cout << "Enter file path: ";
  51.     do {
  52.         isIncorrect = false;
  53.         cin >> path;
  54.         if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
  55.             cout << "It should be a \".txt\" file\nEnter file path: ";
  56.             isIncorrect = true;
  57.         }
  58.     } while (isIncorrect);
  59.     return path;
  60. }
  61.  
  62. int takeSize() {
  63.     const int MIN_SIZE = 2;
  64.     const int MAX_SIZE = 30;
  65.     int number = 0;
  66.     bool isIncorrect;
  67.     do {
  68.         isIncorrect = false;
  69.         cin >> number;
  70.         if (!cin.good()) {
  71.             isIncorrect = true;
  72.             cout << "Incorrect input!!!\n";
  73.             while (cin.get() != '\n') {
  74.                 cin.clear();
  75.             }
  76.         }
  77.         if ((!isIncorrect) && (number < MIN_SIZE || number > MAX_SIZE)) {
  78.             isIncorrect = true;
  79.             cout << "Incorrect input!!!\n";
  80.         }
  81.     } while (isIncorrect);
  82.     return number;
  83. }
  84.  
  85. char takeChar() {
  86.     char inputChar;
  87.     bool isIncorrect;
  88.     string line;
  89.     do {
  90.         cin >> line;
  91.         isIncorrect = false;
  92.         if ((line.length() != 1)) {
  93.             cout << "Incorrect input!!! Must be char:";
  94.             isIncorrect = true;
  95.         }
  96.         else {
  97.                 inputChar = line[0];
  98.         }
  99.     } while (isIncorrect);
  100.     return inputChar;
  101. }
  102.  
  103. set <char> takeSetFromConsole() {
  104.     cout << "Enter size: ";
  105.     int size = takeSize();
  106.     set <char> inputSet;
  107.     for (int i = 0; i < size; i++) {
  108.         cout << "Enter element " << (i + 1) << ": ";
  109.         inputSet.insert(takeChar());
  110.     }
  111.     return inputSet;
  112. }
  113.  
  114. set <char> takeSetFromFile(const string path)  {
  115.     ifstream fin(path);
  116.     set <char> inputSet;
  117.     string sign;
  118.     bool isCorrect = true;
  119.     while (isCorrect && !fin.eof()) {
  120.         fin >> sign;
  121.         if (sign.size() == 1) {
  122.             inputSet.insert(sign[0]);
  123.         }
  124.         else {
  125.             cout << "Incorrect file data\n";
  126.             isCorrect = false;
  127.         }
  128.     }
  129.     fin.close();
  130.     if (!isCorrect) {
  131.         cout << "Enter set from console:\n";
  132.         inputSet = takeSetFromConsole();
  133.     }
  134.     return inputSet;
  135. }
  136.  
  137. set <char> takeSet(const char source) {
  138.     string inPath;
  139.     set <char> inputSet;
  140.     if (source == '1') {
  141.         inputSet = takeSetFromConsole();
  142.     }
  143.     else {
  144.         inPath = takeInPath();
  145.         inputSet = takeSetFromFile(inPath);
  146.         }
  147.     return inputSet;
  148. }
  149.  
  150. set <char> findAnswer(set <char> inputSet) {
  151.     set <char> answer;
  152.     set_intersection(inputSet.begin(), inputSet.end(), setOfDigitsAndOperations.begin(), setOfDigitsAndOperations.end(), inserter(answer, answer.end()));
  153.  
  154.    
  155.     return answer;
  156. }
  157.  
  158. void outputToConsole(set <char> setOfAnswers) {
  159.     cout << "Answer is:\n(";
  160.     set<char>::iterator number;
  161.     for (number = setOfAnswers.begin(); number != setOfAnswers.end(); number++) {
  162.         cout << " " << *number;
  163.     }
  164.     cout << ")";
  165. }
  166.  
  167. void outputToFile(const string path, set <char> setOfAnswers) {
  168.     ofstream fout;
  169.     fout.open(path);
  170.     fout << "Answer is:\n(";
  171.     set<char>::iterator number;
  172.     for (number = setOfAnswers.begin(); number != setOfAnswers.end(); number++) {
  173.         fout << " " << *number;  
  174.     }
  175.     fout << ")";
  176.     fout.close();
  177.     cout << "Done";
  178. }
  179.  
  180. void output(const char source, set <char> setOfAnswers) {
  181.     string outPath;
  182.     if (source == '1') {
  183.         outputToConsole(setOfAnswers);
  184.     }
  185.     else {
  186.         outPath = takeOutPath();
  187.         outputToFile(outPath, setOfAnswers);
  188.     }
  189. }
  190.  
  191. int main() {
  192.     cout << "Welcome to the program that will print a set, whose elements are signs of arithmetic operations and digits from a given set\nSelect the source for entering the sentence:\n1:Console\n2:File\nEnter 1 or 2: ";
  193.     char inputSource = takeSource();
  194.     set <char> inputSet = takeSet(inputSource);;
  195.     set <char> answer = findAnswer(inputSet);
  196.     cout << "Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ";
  197.     char outputSource = takeSource();
  198.     output(outputSource, answer);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement