Egor_Vakar

lab3_1(C++)

Nov 7th, 2021 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. char takeSource() {
  10.     char choice;
  11.     cin >> choice;
  12.     while ((choice != '1') && (choice != '2')) {
  13.         while (cin.get() != '\n') {
  14.             cin.clear();
  15.         }
  16.         cout << "Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ";
  17.         cin >> choice;
  18.     }
  19.     return choice;
  20. }
  21.  
  22. string takeInPath() {
  23.     string path;
  24.     bool isIncorrect;
  25.     cout << "Enter file path: ";
  26.     do {
  27.         isIncorrect = true;
  28.         cin >> path;
  29.         ifstream fin(path);
  30.         if (fin.is_open()) {
  31.             isIncorrect = false;
  32.             fin.close();
  33.         }
  34.         else {
  35.             cout << "File is not found\nEnter file path: ";
  36.         }
  37.         if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
  38.             cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
  39.             isIncorrect = true;
  40.         }
  41.     } while (isIncorrect);
  42.     return path;
  43. }
  44.  
  45. string takeOutPath() {
  46.     string path;
  47.     bool isIncorrect;
  48.     cout << "Enter file path: ";
  49.     do{
  50.         isIncorrect = false;
  51.         cin >> path;
  52.         if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
  53.             cout << "It should be a \".txt\" file\nEnter file path: ";
  54.             isIncorrect = true;
  55.         }
  56.     } while (isIncorrect);
  57.     return path;
  58. }
  59.  
  60. string takeSentenceFromFile(const string path) {
  61.     ifstream fin(path);
  62.     string sentence;
  63.     getline(fin, sentence);
  64.     if (!fin.eof()) {
  65.         sentence = "";
  66.     }
  67.     fin.close();
  68.     return sentence;
  69. }
  70.  
  71. string takeSentenceFromConsole() {
  72.     string sentence;
  73.     string temp;
  74.     cout << "Enter the sentence: ";
  75.     getline(cin, temp);
  76.     getline(cin, sentence);
  77.     return sentence;
  78. }
  79.  
  80. string takeSentence (const char source) {
  81.     string inPath;
  82.     string sentence;
  83.     if (source == '1') {
  84.         sentence = takeSentenceFromConsole();
  85.     }
  86.     else {
  87.         inPath = takeInPath();
  88.         sentence = takeSentenceFromFile(inPath);
  89.         if (sentence == "") {
  90.             inPath = takeInPath();
  91.             sentence = takeSentenceFromFile(inPath);
  92.         }
  93.     }
  94.     return sentence;
  95. }
  96.  
  97. string replaceAllToSpace(string sentence, char symbol) {
  98.     int index = sentence.find(symbol);
  99.     while (index != -1) {
  100.         sentence.replace(index, 1, " ");
  101.         index = sentence.find(symbol);
  102.     }
  103.     return sentence;
  104. }
  105.  
  106. string makeSentenceNormal(string sentence) {
  107.     sentence = replaceAllToSpace(sentence, '.');
  108.     sentence = replaceAllToSpace(sentence, ',');
  109.     sentence = replaceAllToSpace(sentence, '-');
  110.     sentence = replaceAllToSpace(sentence, ';');
  111.     sentence = replaceAllToSpace(sentence, '!');
  112.     sentence = replaceAllToSpace(sentence, '?');
  113.     sentence = replaceAllToSpace(sentence, ':');
  114.     sentence = replaceAllToSpace(sentence, '—');
  115.     return sentence;
  116. }
  117.  
  118. vector <string> findArrayOfWords(string sentence) {
  119.     sentence = makeSentenceNormal(sentence);
  120.     vector <string> arrayOfWords(100);
  121.     int wordsCounter = 0;
  122.     string word;
  123.     int index = sentence.find(" ");
  124.     while (index != -1) {
  125.         word = sentence.substr(0, index);
  126.         if (word != "") {
  127.             arrayOfWords[wordsCounter] = word;
  128.             wordsCounter++;
  129.         }
  130.         sentence = sentence.substr(index + 1);
  131.         index = sentence.find(" ");
  132.     }
  133.     if (sentence != "") {
  134.         arrayOfWords[wordsCounter] = sentence;
  135.         wordsCounter++;
  136.     }
  137.     vector <string> totalArrayOfWords(wordsCounter);
  138.     for (int i = 0; i < wordsCounter; i++) {
  139.         totalArrayOfWords[i] = arrayOfWords[i];
  140.     }
  141.     return totalArrayOfWords;
  142. }
  143.  
  144. bool areWordsSame(string word, string lastWord){
  145.     transform(word.begin(), word.end(), word.begin(), tolower);
  146.     transform(lastWord.begin(), lastWord.end(), lastWord.begin(), tolower);
  147.     if (word == lastWord) {
  148.         return true;
  149.     }
  150.     else {
  151.         return false;
  152.     }
  153. }
  154.  
  155. int findAnswer(vector <string> array) {
  156.     int counter = 0;
  157.     for (int i = 0; i < array.size(); i++) {
  158.         if (areWordsSame(array[i], array[array.size() - 1])){
  159.             counter++;
  160.         }
  161.     }
  162.     return counter;
  163. }
  164.  
  165. void outputToFile(const string path, const string sentence, const int answer) {
  166.     ofstream fout;
  167.     fout.open(path);
  168.     fout << "Sentence is:\n" << sentence << "\nLast word founded " << answer << " times.";
  169.     fout.close();
  170.     cout << "Done";
  171. }
  172.  
  173. void output(const char source, const string sentence, const int answer) {
  174.     string outPath;
  175.     if (source == '1') {
  176.         cout << "Sentence is:\n" << sentence << "\nLast word founded " << answer << " times.";
  177.  
  178.     }
  179.     else {
  180.         outPath = takeOutPath();
  181.         outputToFile(outPath, sentence, answer);
  182.     }
  183. }
  184.  
  185. int main(){
  186.     cout << "Welcome to the program that count how many times the last word occurs in a sentence.\nSelect the source for entering the sentence:\n1:Console\n2:File\nEnter 1 or 2: ";
  187.     char inputSource = takeSource();
  188.     string sentence = takeSentence(inputSource);
  189.     vector <string> arrayOfWords = findArrayOfWords(sentence);
  190.     int answer = findAnswer(arrayOfWords);
  191.     cout << "Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ";
  192.     char outputSource = takeSource();
  193.     output(outputSource, sentence, answer);
  194. }
Add Comment
Please, Sign In to add comment