Advertisement
Guest User

Untitled

a guest
May 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <cstdlib>
  6. #include <utility>
  7.  
  8. using namespace std;
  9.  
  10. struct element {
  11.     string s;
  12.     element *next;
  13. };
  14.  
  15. struct list {
  16.     element *head;
  17.     element *tail;
  18.     int elNum = 0;
  19. };
  20. struct queue {
  21.     element *head;
  22.     element *tail;
  23.  
  24. };
  25.  
  26. void load_data(const string &path, string &text);
  27.  
  28. void split(string &text, string &s, const string &delimEnd, char &delim);
  29.  
  30. void split(string &text, string &s, char delim = ' ');
  31.  
  32. void add_head(string arg, list *ml);
  33.  
  34. void delete_head(list *ml);
  35.  
  36. int* create_temporary_array(string pattern);
  37.  
  38. int KMP(string pattern, string target);
  39.  
  40. queue *new_queue();
  41.  
  42. void add_to_queue(string newData, queue *q);
  43.  
  44. void delete_from_queue(queue *q);
  45.  
  46. void showQueue(queue *q);
  47.  
  48.  
  49. int main() {
  50.     string text;
  51.     load_data("inversed.txt", text);
  52.     list *listOfSentences = new list;
  53.     queue *que = new_queue();
  54.  
  55.     while (!text.empty()) {
  56.         string s;
  57.         char delim = ' ';
  58.         split(text, s, "!?.", delim);
  59.         list *listOfWords = new list;
  60.         while (!s.empty()) {
  61.             string temp;
  62.             split(s, temp);
  63.             add_head(temp, listOfWords);
  64.  
  65.         }
  66.  
  67.         string sentence;
  68.         int i = listOfWords->elNum;
  69.         while (i > 0) {
  70.             sentence += listOfWords->head->s + +" ";
  71.             delete_head(listOfWords);
  72.             i--;
  73.  
  74.  
  75.         }
  76.         delete listOfWords;
  77.  
  78.         sentence.back() = delim;
  79.  
  80.         cout << sentence << endl;
  81.         add_head(sentence, listOfSentences);
  82.         if(sentence.find("sale")!=string::npos||sentence.find("promotion")!=string::npos||sentence.find("discount")!=string::npos||sentence.find("offer")!=string::npos){
  83.             add_to_queue(sentence,que);
  84.         }
  85.  
  86.  
  87.  
  88.  
  89.     }
  90.     cout << "_________________________" << endl;
  91.  
  92.     showQueue(que);
  93.  
  94.     cout << "_________________________" << endl;
  95.  
  96.     while(que->head!= nullptr){
  97.         int r1 = KMP("unique offer",que->head->s);
  98.         int r2 = KMP("mega discounts",que->head->s);
  99.         int r3 = KMP("super promotion",que->head->s);
  100.         int r4 = KMP("low prices",que->head->s);
  101.         int r5 = KMP("great sale",que->head->s);
  102.         cout << r1+r2+r3+r4+r5 << endl;
  103.         if(r1+r2+r3+r4+r5!=0){
  104.             cout << que->head->s << endl;
  105.             cout << "unique offer = " << r1 << endl;
  106.             cout << "mega discounts = " << r2 << endl;
  107.             cout << "super promotion = " << r3 << endl;
  108.             cout << "low prices = " <<  r4 << endl;
  109.             cout << "great sale = " <<  r5 << endl;
  110.         }
  111.  
  112.         delete_from_queue(que);
  113.         cout << endl;
  114.  
  115.     }
  116.  
  117.  
  118.  
  119.  
  120.     delete listOfSentences;
  121.     delete que;
  122.     return 0;
  123. }
  124.  
  125. void load_data(const string &path, string &text) {
  126.  
  127.     ifstream file;
  128.     file.open(path);
  129.  
  130.     getline(file, text);
  131.  
  132.     file.close();
  133. }
  134.  
  135. void split(string &text, string &s, const string &delimEnd, char &delim) {
  136.     size_t start = 0;
  137.     size_t finish = text.find_first_of(delimEnd, start);
  138.     if (text[finish] == '!' || text[finish] == ',' || text[finish] == '.') {
  139.         delim = text[finish];
  140.     }
  141.  
  142.  
  143.     if (finish != string::npos) {
  144.         s = text.substr(start, finish - start);
  145.         text.erase(0, finish + 1);
  146.  
  147.     } else {
  148.         s = text.substr(start, s.length() - 1 - start);
  149.         text.clear();
  150.     }
  151. }
  152.  
  153. void split(string &text, string &s, char delim) {
  154.     size_t start = text.find_first_not_of(delim);
  155.     size_t finish = text.find_first_of(delim, start);
  156.  
  157.     if (finish != string::npos) {
  158.         s = text.substr(start, finish - start);
  159.         text.erase(0, finish + 1);
  160.  
  161.  
  162.     } else {
  163.         s = text.substr(start, s.length() - 1 - start);
  164.         text.clear();
  165.     }
  166.  
  167. }
  168.  
  169. void add_head(string arg, list *ml) {
  170.     auto *el = new element;
  171.     el->s = std::move(arg);
  172.     el->next = nullptr;
  173.     if (ml->head == nullptr) {
  174.         ml->tail = el;
  175.     } else {
  176.         el->next = ml->head;
  177.         ml->head = el;
  178.         ml->elNum++;
  179.     }
  180. }
  181.  
  182. void delete_head(list *ml) {
  183.     if (ml->elNum == 0) {
  184.         cout << "lista jest pusta!" << endl;
  185.         return;
  186.     }
  187.     element *temp = ml->head;
  188.     ml->head = temp->next;
  189.     ml->elNum--;
  190.     delete temp;
  191. }
  192.  
  193. int* create_temporary_array(string pattern) {
  194.     int m = pattern.length();
  195.     int* f = new int[m];
  196.     f[0]=0;
  197.     f[1]=0;
  198.     int t = 0;
  199.     for(int i=1;i<m;i++){
  200.         while(t>0&&pattern[t]!=pattern[i]){
  201.             t=f[t];
  202.         }
  203.         if(pattern[t]==pattern[i]){
  204.             t++;
  205.  
  206.         }
  207.         f[i+1]=t;
  208.     }
  209.  
  210.     return f;
  211. }
  212.  
  213. int KMP(string pattern, string target){
  214.     int *f = create_temporary_array(pattern);
  215.     int count = 0;
  216.  
  217.     int n = target.length();
  218.     int m = pattern.length();
  219.  
  220.     int i = 0;
  221.     int j = 0;
  222.     while (i < n - m + 1) {
  223.         while((pattern[j]==target[i+j])&&j<m){
  224.             j++;
  225.  
  226.         }
  227.         if(j==m){
  228.             count++;
  229.         }
  230.         i=i+max(1,j-f[j]);
  231.  
  232.         j=f[j+1];
  233.  
  234.     }
  235.     delete[] f;
  236.     return count;
  237.  
  238. }
  239.  
  240. queue *new_queue() {
  241.     queue *q = new queue;
  242.     q->tail = nullptr;
  243.     q->head = nullptr;
  244.     return q;
  245.  
  246. }
  247.  
  248. void add_to_queue(string newData, queue *q) {
  249.     element *el = new element;
  250.     el->s = newData;
  251.     el->next = nullptr;
  252.     if (q->tail != nullptr) {
  253.         q->tail->next = el;
  254.  
  255.  
  256.     } else {
  257.         q->head = el;
  258.  
  259.     }
  260.     q->tail = el;
  261. }
  262.  
  263. void delete_from_queue(queue *q) {
  264.     if (q->tail != nullptr) {
  265.         element *temp = q->head;
  266.         q->head = temp->next;
  267.         if (q->head == nullptr) {
  268.             q->tail = nullptr;
  269.         }
  270.         delete temp;
  271.  
  272.     }else{
  273.         cout << "Kolejka jest pusta!" << endl;
  274.     }
  275. }
  276. void showQueue(queue *q){
  277.     element *el = q->head;
  278.     while(el->next!=nullptr){
  279.         cout << el->s << endl;
  280.         el = el->next;
  281.     }
  282.     cout << el->s << endl;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement