Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <ctype.h>
- #include <algorithm>
- #include <set>
- using namespace std;
- void outputOfTaskInfo() {
- cout << "This program determines the number of vowels and consonants in sentence.";
- }
- string inputPathToFile() {
- bool isInCorrect;
- string path;
- cout << "Enter the link to a file: ";
- do {
- isInCorrect = false;
- cin >> path;
- ifstream file(path);
- if (!file.is_open()) {
- cout << "The file is not found! Enter the right path: ";
- isInCorrect = true;
- }
- } while (isInCorrect);
- return path;
- }
- int getVerificationOfChoice() {
- int choice;
- bool isInCorrect;
- do {
- isInCorrect = false;
- cin >> choice;
- if (cin.fail()) {
- isInCorrect = true;
- }
- cin.clear();
- cin.ignore(numeric_limits < streamsize>::max(), '\n');
- if (isInCorrect)
- cout << "\nERROR! Try to enter the value again: ";
- if (isInCorrect || (choice != 1 && choice != 2)) {
- cout << "\nERROR! Try to enter the value again: ";
- isInCorrect = true;
- }
- } while (isInCorrect);
- return choice;
- }
- string readSentenceFromConsole() {
- string sentence;
- cout << "Enter your sentence: ";
- cin >> sentence;
- transform(sentence.begin(), sentence.end(), sentence.begin(), tolower);
- return sentence;
- }
- string readSentenceFromFile(const string& path) {
- string sentence;
- cout << "\nReading file...";
- ifstream fin(path);
- fin >> sentence;
- transform(sentence.begin(), sentence.end(), sentence.begin(), tolower);
- fin.close();
- return sentence;
- }
- set<char> transformSentenceToSet(string sentence) {
- set<char> sentenceSet;
- char letter;
- for (int i = 0; i < sentence.length(); i++) {
- letter = sentence[i];
- sentenceSet.insert(letter);
- }
- return sentenceSet;
- }
- bool findOtherSymbols(const set<char> sentence) {
- bool isIncorrect = true;
- set<char> numsSet;
- set<char> foundNums;
- set<char> russSet;
- set<char> foundRuss;
- set<char> otherSet;
- set<char> foundOther;
- string rus = "абвгдеёжзийклмнопрстуфхцчшщъэьэюяAБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЭЮЯ";
- string other = "`~!@\"#№$;:%^&?*()-_+=";
- for (int i = 48; i < 58; i++)
- numsSet.insert((char)i);
- for (int i = 0; i < rus.length(); i++)
- russSet.insert(rus[i]);
- for (int i = 0; i < other.length(); i++)
- otherSet.insert(other[i]);
- set_intersection(numsSet.begin(), numsSet.end(), sentence.begin(), sentence.end(), inserter(foundNums, foundNums.begin()));
- set_intersection(russSet.begin(), russSet.end(), sentence.begin(), sentence.end(), inserter(foundRuss, foundRuss.begin()));
- set_intersection(otherSet.begin(), otherSet.end(), sentence.begin(), sentence.end(), inserter(foundOther, foundOther.begin()));
- if (foundNums.empty() && foundRuss.empty() && foundOther.empty())
- isIncorrect = false;
- return isIncorrect;
- }
- int* findResult(const set<char>& sogl, const set<char>& glas, const set<char>& sentenceSet) {
- set<char> interSogl;
- set<char> interGlas;
- int* answerArray = new int[2];
- set_intersection(sogl.begin(), sogl.end(), sentenceSet.begin(), sentenceSet.end(), inserter(interSogl, interSogl.begin()));
- set_intersection(glas.begin(), glas.end(), sentenceSet.begin(), sentenceSet.end(), inserter(interGlas, interGlas.begin()));
- answerArray[0] = interSogl.size();
- answerArray[1] = interGlas.size();
- return answerArray;
- }
- void outputToConsole(const int* answerArray) {
- int numberOfSogl = answerArray[0];
- int numberOfGlas = answerArray[1];
- cout << "Consonants: " << numberOfSogl;
- cout << "\nVowels: " << numberOfGlas;
- }
- void outputToFile(const int* answerArray, string& path, const int choice) {
- int numberOfSogl = answerArray[0];
- int numberOfGlas = answerArray[1];
- bool isInCorrect;
- do {
- isInCorrect = false;
- ofstream fout(path);
- try {
- fout << "Consonants: " << numberOfSogl;
- fout << "\nVowels: " << numberOfGlas;
- }
- catch (...) {
- cout << "ERROR! Change file parameters or provide a link to a new one. \n";
- isInCorrect = true;
- path = inputPathToFile();
- }
- fout.close();
- } while (isInCorrect);
- cout << "The data has been successfully written to a file. ";
- }
- void processUserInput(set<char>& sSet)
- {
- int choiceForInput;
- string sentence;
- bool isInCorrect;
- string inputFile;
- cout << "\nDo you want to enter a sentence from console(1) or read from a file(2) ? ";
- choiceForInput = getVerificationOfChoice();
- if (choiceForInput == 1) {
- do {
- sentence = readSentenceFromConsole();
- sSet = transformSentenceToSet(sentence);
- isInCorrect = findOtherSymbols(sSet);
- if (isInCorrect)
- cout << "\nMistakes in the text!";
- } while (isInCorrect);
- }
- if (choiceForInput == 2) {
- inputFile = inputPathToFile();
- sentence = readSentenceFromFile(inputFile);
- sSet = transformSentenceToSet(sentence);
- isInCorrect = findOtherSymbols(sSet);
- if (isInCorrect) {
- cout << "\nMistakes in the text! ";
- do {
- sentence = readSentenceFromConsole();
- sSet = transformSentenceToSet(sentence);
- isInCorrect = findOtherSymbols(sSet);
- if (isInCorrect)
- cout << "\nMistakes in the text! ";
- } while (isInCorrect);
- }
- cout << "\nYour input: " << sentence;
- }
- }
- void processUserOutput(const int* answerArray)
- {
- int choiceForOutput;
- string outputFile;
- cout << "\nDo you want to get an answer in console(1) or in file(2) ? ";
- choiceForOutput = getVerificationOfChoice();
- if (choiceForOutput == 1)
- outputToConsole(answerArray);
- if (choiceForOutput == 2) {
- outputFile = inputPathToFile();
- outputToFile(answerArray, outputFile, choiceForOutput);
- }
- }
- int main() {
- set<char> glas;
- set<char> all;
- set<char> sogl;
- set<char> sSet;
- int* answerArray = new int[2];
- outputOfTaskInfo();
- processUserInput(sSet);
- glas = { 'a', 'e', 'i', 'o', 'u' };
- sogl = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' };
- answerArray = findResult(sogl, glas, sSet);
- processUserOutput(answerArray);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement