Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Варіант 8
- * Написати програму, яка:
- * • Виводить текст на екран дисплея;
- * • Визначає кількість символів в найкоротшому слові;
- * • По натисненню довільної клавіші по черзі виділяє кожне слово тексту, що містить мінімальну кількість символів.
- */
- #include <iostream>
- // to read from file
- #include <fstream>
- #include <string>
- #include <vector>
- // to use istringstream which facilitates spliting string on words
- #include <sstream>
- // used in clearScreen
- #include <stdio.h>
- #include <algorithm>
- using namespace std;
- // read string from file
- string getContent();
- // split string on words
- vector<string> stringToWords(string content);
- // print vector and CAPITALIZE highlight word
- void printVector(vector<string> vec, string highlight);
- // convert string to lower case
- string toLowerCase(string s);
- // convert string to UPPER case
- string toUpperCase(string s);
- // convert vector to vector with unique values
- vector<string> uniqueVectorKeys(vector<string>);
- // sort vector by string length
- // result vector will have smaller strings at the beginning
- vector<string> sortVectorByWordLength(vector<string> vec);
- // function used in sortVectorByWordLength
- // to compare 2 strings
- // returns true if a is shorter then b
- bool sortWordByLength(string a, string b);
- // removes content from screen
- // will be used to rewrite console screen
- void clearScreen();
- int main() {
- // read and convert to lower case
- // we convert to lower case because we highlight words
- // by making them uppercase
- // It will be more visual if else in lower case
- string content = toLowerCase(getContent());
- vector<string> vec = stringToWords(content);
- // unique words from file ordered in ascending way: a, aa, aaa, aaaa
- vector<string> words = sortVectorByWordLength(uniqueVectorKeys(vec));
- // word which we will be highlighting
- string highlight = "";
- // iterator to run over words
- vector<string>::iterator it = words.begin();
- do {
- // clean screen before use
- // it will allow to rewrite output when user clicks ENTER
- clearScreen();
- // reach current word
- highlight = *it;
- cout << "Current smallest word: \"" << highlight << "\", length: " << highlight.length() << endl << endl;
- // print input vector and UPPERCASE current word
- printVector(vec, highlight);
- cout << endl << endl;
- // So, just press enter on your keyboard
- cout << "Press ENTER key to highligh next min word";
- // To wait for enter
- cin.get();
- // To read next word
- it++;
- } while(it != words.end());
- return 0;
- }
- vector<string> uniqueVectorKeys(vector<string> vec) {
- sort(vec.begin(), vec.end());
- vec.erase(unique( vec.begin(), vec.end() ), vec.end());
- return vec;
- }
- bool sortWordByLength(string a, string b) {
- return a.length() < b.length();
- }
- vector<string> sortVectorByWordLength(vector<string> vec) {
- sort(vec.begin(), vec.end(), sortWordByLength);
- return vec;
- }
- void printVector(vector<string> vec, string highlight) {
- string word;
- for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it) {
- word = *it;
- if(word == highlight) {
- word = toUpperCase(word);
- }
- cout << word << " ";
- }
- }
- vector<string> stringToWords(string content) {
- vector<string> vec;
- istringstream iss(content);
- copy(istream_iterator<string>(iss),
- istream_iterator<string>(),
- back_inserter(vec));
- return vec;
- }
- string getContent() {
- // make this path your own!
- // I cound not place file inside project's dir
- // because I have different development environment
- ifstream in("/Users/oknyga/data.dat");
- string contents((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
- return contents;
- }
- string toLowerCase(string s) {
- transform(s.begin(), s.end(), s.begin(), ::tolower);
- return s;
- }
- string toUpperCase(string s) {
- transform(s.begin(), s.end(), s.begin(), ::toupper);
- return s;
- }
- // OS dependent code
- // Because I was developing on OSX
- // and code will be run on Windows
- // I created function which can clean
- // on both Windows and Unix (in teory)
- #ifdef __cplusplus__
- void clearScreen() {
- cout << string( 100, '\n' );
- }
- #else
- void clearScreen() {
- int n;
- for (n = 0; n < 10; n++)
- printf( "\n\n\n\n\n\n\n\n\n\n" );
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement