Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <vector>
- #define VOCALS "euioay"
- std::vector<std::string> getWordsFromStr(std::string str)
- {
- std::vector<std::string> vectorWords = {};
- std::istringstream ist(str);
- std::string word;
- while (ist >> word) {
- vectorWords.push_back(word);
- }
- return vectorWords;
- }
- std::string deleteNotLatinSymbols(std::string str)
- {
- for (int i = 0; i < str.length(); ) {
- if ((str[i] < *"A" or str[i] > *"Z") and (str[i] < *"a" or str[i] > *"z") and str[i] != *" ") {
- str.erase(i, 1);
- } else
- {
- i++;
- }
- }
- return str;
- }
- std::string inputOfStrFromConsole()
- {
- std::string str, newStr, word;
- bool isIncorrect;
- do
- {
- isIncorrect = false;
- getline(std::cin, str);
- newStr = deleteNotLatinSymbols(str);
- if (newStr != str) {
- std::cout << "Строка без лишних символов:\n" << newStr << "\n";
- }
- std::istringstream ist(newStr);
- if (!(ist >> word))
- {
- isIncorrect = true;
- std::cout << "\tПожалуйста, введите строку, содержащую слова:\n";
- }
- } while (isIncorrect);
- return newStr;
- }
- bool letterIsVocal(char ch)
- {
- bool vocalIsNotFound = true;
- ch = tolower(ch);
- for (int i = 0; i < strlen(VOCALS) and vocalIsNotFound; i++) {
- if (ch == VOCALS[i]) {
- vocalIsNotFound = false;
- }
- }
- return !vocalIsNotFound;
- }
- bool charIsInString(char ch, std::string str)
- {
- bool isNotInString = true;
- for (int i = 0; i < str.length() and isNotInString; i++) {
- if (ch == str[i]) {
- isNotInString = false;
- }
- }
- return !isNotInString;
- }
- std::string getVocalsInWord(std::string word)
- {
- std::string vocals = "";
- bool vocalIsAlreadyAdded, vocalIsFound;
- //find vocals
- for (int i = 0; i < word.length(); i++) {
- vocalIsFound = false;
- word[i] = tolower(word[i]);
- for (int j = 0; j < strlen(VOCALS) and !vocalIsFound; j++) {
- if (word[i] == VOCALS[j]) {
- vocalIsFound = true;
- vocalIsAlreadyAdded = false;
- vocalIsAlreadyAdded = charIsInString(word[i] , vocals);
- if (!vocalIsAlreadyAdded) {
- vocals += word[i];
- }
- }
- }
- }
- // sort vocals
- char temp;
- if (vocals.length() > 1) {
- for (int i = 0; i < vocals.length(); i++) {
- for (int j = 1; j < vocals.length() - i; j++) {
- if (vocals[j - 1] > vocals[j]) {
- temp = vocals[j];
- vocals[j] = vocals[j - 1];
- vocals[j - 1] = temp;
- }
- }
- }
- }
- return vocals;
- }
- void doTask1(std::vector<std::string> vectorWords, std::string lastWord)
- {
- std::string vocalsOfLastWord = getVocalsInWord(lastWord);
- std::cout << "\n(задание 19.1)\n\tНапечатать все слова, отличные от последнего слова,\n\tв которые входят те же гласные буквы, что и в первое слово.\n\tОтвет:\n\t";
- for (std::string word : vectorWords)
- {
- if (getVocalsInWord(word) == vocalsOfLastWord and word != lastWord) {
- std::cout << word << " ";
- }
- }
- std::cout << "\n\n";
- }
- void doTask2(std::vector<std::string> vectorWords, std::string lastWord)
- {
- std::string usedConsonants = "";
- std::cout << "\n(задание 19.2)\n\tНапечатать все слова, отличные от последнего слова,\n\tудаляя все согласные буквы, которые уже встречались раньше.\n\tОтвет:\n\t";
- for (std::string word : vectorWords)
- {
- if (word != lastWord)
- {
- for (int i = 0; i < word.length(); )
- {
- char ch = word[i];
- if (charIsInString(ch, usedConsonants))
- {
- word.erase(i, 1);
- } else
- {
- i++;
- }
- }
- std::cout << word << " ";
- for (char ch : word) {
- ch = tolower(ch);
- if (!letterIsVocal(ch) and !charIsInString(ch, usedConsonants))
- {
- usedConsonants += ch;
- }
- }
- }
- }
- std::cout << "\n\n";
- }
- void doTasks(std::string str)
- {
- std::vector<std::string> vectorWords = getWordsFromStr(str);
- std::string lastWord = vectorWords.back();
- vectorWords.pop_back();
- doTask1(vectorWords, lastWord);
- doTask2(vectorWords, lastWord);
- }
- int main(int argc, const char * argv[])
- {
- std::cout << "\nДанная программа принимает строку со словами.\n";
- std::cout << "\nВведите строку:\n";
- std::string strInput = inputOfStrFromConsole();
- doTasks(strInput);
- std::cout << "\nПрограмма успешно завершена\n";
- return 0;
- // abcd bc dthz веинт нвшщтнив 354№.:.№ oio bftdOI uoio GGiOOOiI GGiOOOiI
- // игшмыигы
- // oi OOII ooooooi
- }
Advertisement
Add Comment
Please, Sign In to add comment