Advertisement
fatalryuu

Untitled

Oct 20th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <ctype.h>
  5. #include <algorithm>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. void outputOfTaskInfo() {
  11.     cout << "This program determines the number of vowels and consonants in sentence.";
  12. }
  13.  
  14. string inputPathToFile() {
  15.     bool isInCorrect;
  16.     string path;
  17.  
  18.     cout << "Enter the link to a file: ";
  19.  
  20.     do {
  21.         isInCorrect = false;
  22.         cin >> path;
  23.         ifstream file(path);
  24.  
  25.         if (!file.is_open()) {
  26.             cout << "The file is not found! Enter the right path: ";
  27.             isInCorrect = true;
  28.         }
  29.     } while (isInCorrect);
  30.  
  31.     return path;
  32. }
  33.  
  34. int getVerificationOfChoice() {
  35.     int choice;
  36.     bool isInCorrect;
  37.  
  38.     do {
  39.         isInCorrect = false;
  40.         cin >> choice;
  41.  
  42.         if (cin.fail()) {
  43.             isInCorrect = true;
  44.         }
  45.  
  46.         cin.clear();
  47.         cin.ignore(numeric_limits < streamsize>::max(), '\n');
  48.  
  49.         if (isInCorrect)
  50.             cout << "\nERROR! Try to enter the value again: ";
  51.  
  52.         if (isInCorrect || (choice != 1 && choice != 2)) {
  53.             cout << "\nERROR! Try to enter the value again: ";
  54.             isInCorrect = true;
  55.         }
  56.     } while (isInCorrect);
  57.  
  58.     return choice;
  59. }
  60.  
  61. string readSentenceFromConsole() {
  62.     string sentence;
  63.  
  64.     cout << "Enter your sentence: ";
  65.     cin >> sentence;
  66.     transform(sentence.begin(), sentence.end(), sentence.begin(), tolower);
  67.  
  68.     return sentence;
  69. }
  70.  
  71. string readSentenceFromFile(const string& path) {
  72.     string sentence;
  73.  
  74.     cout << "\nReading file...";
  75.     ifstream fin(path);
  76.     fin >> sentence;
  77.     transform(sentence.begin(), sentence.end(), sentence.begin(), tolower);
  78.  
  79.     fin.close();
  80.     return sentence;
  81. }
  82.  
  83. set<char> transformSentenceToSet(string sentence) {
  84.     set<char> sentenceSet;
  85.     char letter;
  86.  
  87.     for (int i = 0; i < sentence.length(); i++) {
  88.         letter = sentence[i];
  89.         sentenceSet.insert(letter);
  90.     }
  91.  
  92.     return sentenceSet;
  93. }
  94.  
  95. bool findOtherSymbols(const set<char> sentence) {
  96.     bool isIncorrect = true;
  97.     set<char> numsSet;
  98.     set<char> foundNums;
  99.  
  100.     set<char> russSet;
  101.     set<char> foundRuss;
  102.  
  103.     set<char> otherSet;
  104.     set<char> foundOther;
  105.  
  106.     string rus = "абвгдеёжзийклмнопрстуфхцчшщъэьэюяAБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЭЮЯ";
  107.     string other = "`~!@\"#№$;:%^&?*()-_+=";
  108.  
  109.  
  110.     for (int i = 48; i < 58; i++)
  111.         numsSet.insert((char)i);
  112.  
  113.     for (int i = 0; i < rus.length(); i++)
  114.         russSet.insert(rus[i]);
  115.  
  116.     for (int i = 0; i < other.length(); i++)
  117.         otherSet.insert(other[i]);
  118.  
  119.     set_intersection(numsSet.begin(), numsSet.end(), sentence.begin(), sentence.end(), inserter(foundNums, foundNums.begin()));
  120.     set_intersection(russSet.begin(), russSet.end(), sentence.begin(), sentence.end(), inserter(foundRuss, foundRuss.begin()));
  121.     set_intersection(otherSet.begin(), otherSet.end(), sentence.begin(), sentence.end(), inserter(foundOther, foundOther.begin()));
  122.  
  123.     if (foundNums.empty() && foundRuss.empty() && foundOther.empty())
  124.         isIncorrect = false;
  125.  
  126.     return isIncorrect;
  127. }
  128.  
  129. int* findResult(const set<char>& sogl, const set<char>& glas, const set<char>& sentenceSet) {
  130.     set<char> interSogl;
  131.     set<char> interGlas;
  132.     int* answerArray = new int[2];
  133.  
  134.     set_intersection(sogl.begin(), sogl.end(), sentenceSet.begin(), sentenceSet.end(), inserter(interSogl, interSogl.begin()));
  135.     set_intersection(glas.begin(), glas.end(), sentenceSet.begin(), sentenceSet.end(), inserter(interGlas, interGlas.begin()));
  136.  
  137.     answerArray[0] = interSogl.size();
  138.     answerArray[1] = interGlas.size();
  139.  
  140.     return answerArray;
  141. }
  142.  
  143. void outputToConsole(const int* answerArray) {
  144.     int numberOfSogl = answerArray[0];
  145.     int numberOfGlas = answerArray[1];
  146.  
  147.     cout << "Consonants: " << numberOfSogl;
  148.     cout << "\nVowels: " << numberOfGlas;
  149. }
  150.  
  151. void outputToFile(const int* answerArray, string& path, const int choice) {
  152.     int numberOfSogl = answerArray[0];
  153.     int numberOfGlas = answerArray[1];
  154.     bool isInCorrect;
  155.  
  156.     do {
  157.         isInCorrect = false;
  158.         ofstream fout(path);
  159.         try {
  160.             fout << "Consonants: " << numberOfSogl;
  161.             fout << "\nVowels: " << numberOfGlas;
  162.         }
  163.         catch (...) {
  164.             cout << "ERROR! Change file parameters or provide a link to a new one. \n";
  165.             isInCorrect = true;
  166.             path = inputPathToFile();
  167.         }
  168.         fout.close();
  169.     } while (isInCorrect);
  170.  
  171.     cout << "The data has been successfully written to a file. ";
  172. }
  173.  
  174. void processUserInput(set<char>& sSet)
  175. {
  176.     int choiceForInput;
  177.     string sentence;
  178.     bool isInCorrect;
  179.     string inputFile;
  180.  
  181.     cout << "\nDo you want to enter a sentence from console(1) or read from a file(2) ? ";
  182.     choiceForInput = getVerificationOfChoice();
  183.  
  184.     if (choiceForInput == 1) {
  185.         do {
  186.             sentence = readSentenceFromConsole();
  187.             sSet = transformSentenceToSet(sentence);
  188.             isInCorrect = findOtherSymbols(sSet);
  189.  
  190.             if (isInCorrect)
  191.                 cout << "\nMistakes in the text!";
  192.  
  193.         } while (isInCorrect);
  194.     }
  195.  
  196.     if (choiceForInput == 2) {
  197.         inputFile = inputPathToFile();
  198.         sentence = readSentenceFromFile(inputFile);
  199.         sSet = transformSentenceToSet(sentence);
  200.         isInCorrect = findOtherSymbols(sSet);
  201.  
  202.         if (isInCorrect) {
  203.             cout << "\nMistakes in the text! ";
  204.  
  205.             do {
  206.                 sentence = readSentenceFromConsole();
  207.                 sSet = transformSentenceToSet(sentence);
  208.                 isInCorrect = findOtherSymbols(sSet);
  209.  
  210.                 if (isInCorrect)
  211.                     cout << "\nMistakes in the text! ";
  212.             } while (isInCorrect);
  213.         }
  214.  
  215.         cout << "\nYour input: " << sentence;
  216.     }
  217. }
  218.  
  219. void processUserOutput(const int* answerArray)
  220. {
  221.     int choiceForOutput;
  222.     string outputFile;
  223.  
  224.     cout << "\nDo you want to get an answer in console(1) or in file(2) ? ";
  225.     choiceForOutput = getVerificationOfChoice();
  226.  
  227.     if (choiceForOutput == 1)
  228.         outputToConsole(answerArray);
  229.  
  230.     if (choiceForOutput == 2) {
  231.         outputFile = inputPathToFile();
  232.         outputToFile(answerArray, outputFile, choiceForOutput);
  233.     }
  234. }
  235.  
  236. int main() {
  237.     set<char> glas;
  238.     set<char> all;
  239.     set<char> sogl;
  240.     set<char> sSet;
  241.     int* answerArray = new int[2];
  242.  
  243.     outputOfTaskInfo();
  244.  
  245.     processUserInput(sSet);
  246.  
  247.     glas = { 'a', 'e', 'i', 'o', 'u' };
  248.     sogl = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' };
  249.  
  250.     answerArray = findResult(sogl, glas, sSet);
  251.  
  252.     processUserOutput(answerArray);
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement