Last active
February 25, 2018 15:35
-
-
Save Pyroarsonist/061aa05fd4270ac6f53046df0668099e to your computer and use it in GitHub Desktop.
1 лаба по ОП, 2 семестр
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Лабораторна #1, 2 семестр | |
| //Увести з клавіатури рядки і записати їх у текстовий файл. | |
| // У кожному непарному рядку визначити слово, що має найбільшу кількість головних. | |
| // Дописати знайдені слова в кожний рядок файлу. | |
| #include <iostream> | |
| #include <fstream> | |
| #include <cstring> | |
| using namespace std; | |
| const string FILENAME = "text.txt"; | |
| void outputFromFile(); // print all from file | |
| void saveString(string); // save string into file | |
| int vowelCount(char *); // count of vowels in word | |
| void appendingWords(); // append words to lines | |
| int initMenu(); // menu | |
| int main() { | |
| bool inMenu = true; | |
| int symb; | |
| while (inMenu) { | |
| symb = initMenu(); | |
| switch (symb) { | |
| case 1: { | |
| string str; | |
| cout << "Enter string till '/': \n"; | |
| cin.get(); | |
| getline(cin, str, '/'); | |
| saveString(str); | |
| break; | |
| } | |
| case 2: { | |
| cout << "File contents: \n"; | |
| outputFromFile(); | |
| break; | |
| } | |
| case 3: { | |
| appendingWords(); | |
| break; | |
| } | |
| case 4: { | |
| inMenu = false; | |
| break; | |
| } | |
| default: { | |
| break; | |
| } | |
| } | |
| } | |
| return 0; | |
| } | |
| int initMenu() { | |
| cout | |
| << "Menu\n-------------------------\nWrite strings to file\t1\nRead text from file\t\t2\nAppend specific words\t3\nExit\t\t\t\t\t4\n-------------------------\n"; | |
| string input; | |
| int ret = -1; | |
| bool uncatched = true; | |
| while (uncatched) { | |
| try { | |
| uncatched = false; | |
| cin >> input; | |
| ret = stoi(input); | |
| } | |
| catch (std::exception &e) { | |
| uncatched = true; | |
| cout << "Not number\nTry again\n"; | |
| } | |
| } | |
| return ret; | |
| } | |
| void saveString(string saving) { | |
| ofstream outfile(FILENAME); | |
| outfile << saving; | |
| outfile.close(); | |
| } | |
| void outputFromFile() { | |
| string temp; | |
| ifstream file(FILENAME); | |
| if (file) { | |
| while (getline(file, temp)) { | |
| cout << temp << endl; | |
| } | |
| } | |
| } | |
| void appendingWords() { | |
| string s; | |
| ifstream file(FILENAME); | |
| int counter = 1; | |
| string new_str; | |
| if (file) { | |
| while (getline(file, s, '\n')) { | |
| if (counter++ % 2 == 1) { | |
| string word; | |
| int counterVowel = -1; | |
| char *temp = new char[s.length() + 1]; | |
| memcpy(temp, s.c_str(), s.length() + 1); | |
| char *pch = strtok(temp, " ,.-"); | |
| while (pch != NULL) { | |
| int count = vowelCount(pch); | |
| if (count > counterVowel) { | |
| counterVowel = count; | |
| word = pch; | |
| } | |
| pch = strtok(NULL, " ,.-"); | |
| } | |
| new_str += s + " " + word + "\n"; | |
| } else { | |
| new_str += s + "\n"; | |
| } | |
| } | |
| } | |
| file.close(); | |
| saveString(new_str); | |
| } | |
| int vowelCount(char *pCh) { | |
| int vowels = 0; | |
| while (*pCh) { | |
| if (strspn(pCh, "aeiou")) | |
| vowels++; | |
| pCh++; | |
| } | |
| return vowels; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment