Advertisement
Vladislav8653

laba_3_2_c++

Dec 7th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <set>
  4.  
  5. using namespace std;
  6. ifstream fin;
  7. ofstream fout;
  8.  
  9. bool choose() {
  10.     int inputNumber;
  11.     bool isIncorrect;
  12.     const int MIN_NUM = 0;
  13.     const int MAX_NUM = 1;
  14.     do {
  15.         cin >> inputNumber;
  16.         isIncorrect = false;
  17.         if (cin.fail()) {
  18.             isIncorrect = true;
  19.             cout << "Please, enter a number." << endl;
  20.             cin.clear();
  21.             while (cin.get() != '\n');
  22.         }
  23.         if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
  24.             cout << "You are out of input range!" << endl;
  25.             isIncorrect = true;
  26.         }
  27.     } while (isIncorrect);
  28.     if (inputNumber == 1)
  29.         return true;
  30.     else
  31.         return false;
  32. }
  33.  
  34. string inputFilePath() {
  35.     const int EXTENSION_SIZE = 4;
  36.     string path;
  37.     string ext;
  38.     bool isIncorrect;
  39.     do {
  40.         isIncorrect = false;
  41.         cout << "Input path to file: " << endl;
  42.         cin >> path;
  43.         fin.open(path);
  44.         if (path.size() > EXTENSION_SIZE) {
  45.             ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
  46.         }
  47.         else {
  48.             cout << "Incorrect file name." << endl;
  49.             isIncorrect = true;
  50.         } if (!isIncorrect && ext != ".txt") {
  51.             cout << "Must have .txt!" << endl;
  52.             isIncorrect = true;
  53.         }
  54.         else if (!isIncorrect && !fin.is_open()) {
  55.             cout << "Wrong way to file." << endl;
  56.             isIncorrect = true;
  57.         }
  58.     } while (isIncorrect);
  59.     fin.close();
  60.     return path;
  61. }
  62.  
  63. string inputSequence(){
  64.     string sequence;
  65.     cout << "Input Sequence" << endl;
  66.     cin >> sequence;
  67.     return sequence;
  68. }
  69.  
  70. string inputSequenceFromFile(string path) {
  71.     string sequence;
  72.     fin.open(path);
  73.     fin >> sequence;  
  74.     return sequence;
  75. }
  76.  
  77. void fileOutput(set <char> setOfSymbols) {
  78.     set <char>::iterator output;
  79.     string path = inputFilePath();
  80.     fout.open(path);
  81.     if (setOfSymbols.empty())
  82.         fout << "You have not entered any signs of arithmetic operations or digits.";
  83.     else{
  84.         fout << "The resulting set" << endl;
  85.         for (output = setOfSymbols.begin(); output != setOfSymbols.end(); ++output)
  86.             fout << *output << " ";
  87.     }
  88.     cout << "Successful output in file." << endl;
  89.     fout.close();
  90. }
  91.  
  92. void consoleOutput(set <char> setOfSymbols) {
  93.     set <char>::iterator output;
  94.     if (setOfSymbols.empty())
  95.         cout << "You have not entered any signs of arithmetic operations or digits." << endl;
  96.     else{
  97.         cout << "The resulting set" << endl;
  98.         for (output = setOfSymbols.begin(); output != setOfSymbols.end(); ++output)
  99.             cout << *output << " ";
  100.     }
  101.  
  102. }
  103.  
  104. set <char> makeSet(string sequence){
  105.     set <char> setOfSymbols{};
  106.     set <char> symbols{ '/', '*', '-', '+', '%', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  107.     for (int i = 0; i < size(sequence); i++){
  108.         if (symbols.count(sequence[i]) == 1)
  109.             setOfSymbols.insert(sequence[i]);
  110.     }
  111.     return setOfSymbols;
  112. }
  113.  
  114. int main() {
  115.     cout << "Given a non-empty sequence of characters, it is required to construct and print a set whose elements are signs of arithmetic operations and digits that occur in the sequence." << endl;
  116.     string sequence;
  117.     set <char> setOfSymbols{};
  118.     cout << "Type 1 - console input, type 0 - file input." << endl;
  119.     bool choice = choose();
  120.     if (choice)
  121.         sequence = inputSequence();
  122.     else
  123.     {
  124.         string path = inputFilePath();
  125.         sequence = inputSequenceFromFile(path);
  126.     }
  127.  
  128.     setOfSymbols = makeSet(sequence);
  129.  
  130.     cout << "Type 1 - console output, type 0 - file output." << endl;
  131.     choice = choose();
  132.     if (choice)
  133.         consoleOutput(setOfSymbols);
  134.     else
  135.         fileOutput(setOfSymbols);
  136.     return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement