Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
210-CW-INDIVIDUAL-CW/210 ct task1 and 2.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
106 lines (86 sloc)
3.66 KB
This file contains 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
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <cstring> | |
#include <algorithm> | |
#include <map> | |
#include <vector> | |
#define BEGIN 253 | |
#define END 2867 | |
using namespace std; | |
int bstSize, cnt, a; // Binary search tree size, counting variable, temporary variable a | |
map<string, int> bst; | |
vector<pair<int, string>> v; // two-dimensional vector for sorting purposes to identify the most frequently used words | |
string txt, str; // two string variables (txt and srt) | |
string result[66]; // save the 66 most frequently used words string array | |
bool isAlp(char c); // A function to determine whether a character is alphabetic or not by calling up a text. | |
int searchWord(string s); // calls up a string and expresses how many times it came out. | |
void show66words(); // function which cna find 66 most used words (alphabetically) | |
int main() | |
{ | |
ifstream file; // Declaring a pointer variable to open a file | |
file.open("txt.txt"); // Save Shakespeare txt in File Pointer | |
for (int i = 1; i < BEGIN; i++) // reading it from the 253rd, repeating it before that. | |
getline(file, txt); // Read to 252nd line | |
for (int i = BEGIN; i <= END; i++) // 253rd to range. | |
{ | |
int len; // integer variable len (to save string length) | |
getline(file, txt); // read a line of Shakespeare txt | |
len = txt.size(); // Save the length of the line you read | |
for (int j = 0; j < len; j++) // Repeat as long as string (from beginning to end of string, each word) | |
{ | |
if (isAlp(txt[j])) // if the word is Alphabet | |
str += txt[j]; // Add one read char to temporary string str | |
else if (!isAlp(txt[j]) && !str.empty()) // if it is not char + emporary string str is not empty -> if there string abc and abcd - it read abcd as a one char | |
{ | |
if (txt[j] == '\'') // \' = '' | |
{ | |
while (txt[j] && txt[j] != ' ') // Skip reading text until blank | |
j++; | |
} | |
if (str != "I" && str.size() == 1) // need to show one word 'i' if I do not do this it shows every single one word | |
continue; // | |
transform(str.begin(), str.end(), str.begin(), ::tolower); // Save map size in variable bstSize | |
bst[str]++; // received one word on the keyboard | |
str.clear(); // | |
} | |
} | |
} | |
bst.erase("a"); | |
bst.erase("the"); | |
bstSize = bst.size(); | |
cin >> str; | |
cout << searchWord(str) << endl; | |
puts("\n"); | |
show66words(); | |
return 0; | |
} | |
bool isAlp(char c) // a function that calls up a string and expresses how many numbers it came out. | |
{ | |
if (65 <= c && c <= 90) | |
return true; | |
else if (97 <= c && c <= 122) | |
return true; | |
else | |
return false; | |
} | |
int searchWord(string s) // a function that calls up a string and expresses how many numbers it came out. | |
{ | |
return bst[s]; // how many strings show up in the map | |
} | |
void show66words() // 66 words | |
{ | |
for (auto iter = bst.begin(); iter != bst.end(); ++iter) | |
v.push_back(make_pair(iter->second, iter->first)); | |
sort(v.begin(), v.end()); | |
for (auto iter = v.begin(); iter != v.end(); ++iter) | |
{ | |
cnt++; | |
if (bstSize - cnt < 66) | |
result[a++] = iter->second; | |
} | |
sort(result, result + 66); | |
for (int i = 0; i < 66; i++) | |
cout << result[i] << endl; | |
} | |
//this is the how to find 66 words to use vector fucntion. Words going to be counted and sorted.Sorted in ascending order, so sorted in reverse order of the least common words |