Advertisement
Guest User

Untitled

a guest
May 24th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <string>
  5. #include <sstream>
  6. #include<bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. FILE* file=fopen("dane.txt","r");
  11.  
  12. struct stck
  13. {
  14. string word;
  15. stck* ptr=nullptr;
  16. };
  17.  
  18. struct element
  19. {
  20. string sentence;
  21. element *next=nullptr;
  22. };
  23.  
  24. struct que
  25. {
  26. element* head=nullptr;
  27. element* tail=nullptr;
  28. };
  29.  
  30. string readFile();
  31. string getMark(string &word);
  32. void ex1ex2(que &k);
  33. void patternPrint();
  34. void add2Stack(stck *&top,string word);
  35. void printStack(stck *top);
  36. void delStack(stck *& top);
  37. void stack2String(stck* top,string &text);
  38. bool ex2IfSentSusp(string text);
  39. void add2Q(que &k, string sentence2add);
  40. void printQ(que k);
  41. void kmp_array(string pattern, int* &t);
  42. int findPattern(string sentence, string pattern, int* t);
  43. void ex3(que k);
  44. string takeOneFromQ(que &k);
  45.  
  46. int main()
  47. {
  48. que k;
  49.  
  50. cout<<"ZAD 1"<<endl<<endl;
  51. ex1ex2(k);
  52. patternPrint();
  53.  
  54. cout<<"ZAD2"<<endl<<endl;
  55. printQ(k);
  56. patternPrint();
  57.  
  58. cout<<"ZAD3"<<endl<<endl;
  59. ex3(k);
  60.  
  61.  
  62. return 0;
  63. }
  64.  
  65. void ex3(que k)
  66. {
  67. int *t;
  68. string sentence;
  69. string pattern[5];
  70. string temp;
  71. pattern[0]="unique offer";
  72. pattern[1]="mega discounts";
  73. pattern[2]="super promotion";
  74. pattern[3]="low prices";
  75. pattern[4]="great sale";
  76. bool ifMatch=false;
  77. int results[5];
  78.  
  79. while(k.head!=nullptr)
  80. {
  81. sentence=takeOneFromQ(k);
  82. temp=sentence;
  83. transform(sentence.begin(), sentence.end(), sentence.begin(), ::tolower);
  84.  
  85. for (int i=0; i<5; i++)
  86. {
  87. kmp_array(pattern[i],t);
  88. results[i]=findPattern(sentence,pattern[i],t);
  89. if(results[i]>0)
  90. {
  91. ifMatch=true;
  92. }
  93.  
  94. delete []t;
  95. }
  96. if(ifMatch==true)
  97. {
  98. cout<<"Zdanie: "<<temp<<endl<<endl;
  99. for(int i=0; i<5; i++)
  100. {
  101. cout<<pattern[i]<<": "<<results[i]<<endl;
  102. }
  103. }
  104. ifMatch=false;
  105. sentence="";
  106. cout<<sentence<<endl;
  107. }
  108. }
  109.  
  110. string readFile()
  111. {
  112. string tempstr="";
  113. char tempChar;
  114. while(!feof(file))
  115. {
  116. tempChar=fgetc(file);
  117.  
  118. if(tempChar==' ')
  119. {
  120. break;
  121. }
  122.  
  123. tempstr+=tempChar;
  124. }
  125. return tempstr;
  126. }
  127.  
  128. string getMark(string &word)
  129. {
  130. char temp;
  131. stringstream ss;
  132. string char2String="";
  133. for(int i=0; i<word.length(); i++)
  134. {
  135. temp=word[i];
  136.  
  137. if(temp=='?'|| temp=='!' || temp=='.')
  138. {
  139. word=word.substr(0,i);
  140. ss<<temp;
  141. ss>>char2String;
  142. return char2String;
  143. }
  144. }
  145. return "0";
  146. }
  147.  
  148.  
  149. void ex1ex2(que &k)
  150. {
  151. string word="";
  152. string mark="";
  153. string text="";
  154. stck *ex1Stck=new stck;
  155.  
  156. while(!feof(file))
  157. {
  158. word=readFile();
  159. mark=getMark(word);
  160.  
  161. if(mark=="0")
  162. {
  163. add2Stack(ex1Stck,word);
  164. }
  165. else
  166. {
  167. add2Stack(ex1Stck,word);
  168. printStack(ex1Stck);
  169. cout<<mark<<" ";
  170. cout<<endl;
  171.  
  172. stack2String(ex1Stck,text);
  173. delStack(ex1Stck);
  174.  
  175. if(ex2IfSentSusp(text)==1)
  176. {
  177.  
  178. if(text[text.length()-1]==' ')
  179. {
  180. text[text.length()-1]=mark[0];
  181. }
  182. else
  183. {
  184. text+=mark;
  185. }
  186. add2Q(k,text);
  187. }
  188.  
  189. text="";
  190. word="";
  191. mark="";
  192. ex1Stck=new stck;
  193. }
  194. }
  195.  
  196. stack2String(ex1Stck,text);
  197. printStack(ex1Stck);
  198. delStack(ex1Stck);
  199. if(ex2IfSentSusp(text)==1)
  200. {
  201. text+=mark;
  202. add2Q(k,text);
  203.  
  204. }
  205. cout<<endl;
  206. }
  207.  
  208. bool ex2IfSentSusp(string text)
  209. {
  210. int length=text.length();
  211.  
  212. for(int i=0; i<length; i++)
  213. {
  214. if(text[i]==':' || text[i]==',' || text[i]=='%')
  215. {
  216. text[i]=' ';
  217. }
  218. }
  219.  
  220. int t[text.length()+2];
  221. int index=1;
  222. string tempword="";
  223. t[0]=1;
  224. for(int i=0; i<length; i++)
  225. {
  226. if(text[i]==' ')
  227. {
  228. t[index]=1;
  229. }
  230. else
  231. {
  232. t[index]=0;
  233. }
  234. index++;
  235. }
  236. t[length+1]=1;
  237.  
  238. for(int i=0; i<length+2; i++)
  239. {
  240. if(t[i]==1)
  241. {
  242. for(int j=i+1; j<length+2; j++)
  243. {
  244. if(t[j]==1)
  245. {
  246. tempword=text.substr(i,j-i-1);
  247. transform(tempword.begin(), tempword.end(), tempword.begin(), ::tolower);
  248.  
  249. if(tempword=="promotion" || tempword=="discount" || tempword=="sale" || tempword=="offer")
  250. {
  251. return 1;
  252. }
  253. i=j-1;
  254. break;
  255. }
  256. }
  257. }
  258. tempword="";
  259. }
  260. return 0;
  261. }
  262.  
  263.  
  264. void add2Stack(stck *&top,string word)
  265. {
  266. stck *element=new stck;
  267.  
  268. element->word=top->word;
  269. element->ptr=top->ptr;
  270. top->ptr=element;
  271. top->word=word;
  272. }
  273.  
  274. void delStack(stck *& top)
  275. {
  276. if(top->ptr!=nullptr)
  277. {
  278. stck *temp=top;
  279. top=top->ptr;
  280. delete temp;
  281. }
  282. }
  283.  
  284. void printStack(stck* top)
  285. {
  286. while(top->ptr!=nullptr)
  287. {
  288. if(top->ptr->ptr==nullptr)
  289. {
  290. cout<<top->word;
  291. top=top->ptr;
  292. }
  293.  
  294. else
  295. {
  296. cout<<top->word<<" ";
  297. top=top->ptr;
  298. }
  299. }
  300.  
  301. }
  302. void stack2String(stck* top,string &text)
  303. {
  304. while(top->ptr!=nullptr)
  305. {
  306. text+=top->word+" ";
  307. top=top->ptr;
  308. }
  309. }
  310.  
  311. void add2Q(que &k, string sentence2add)
  312. {
  313. element *qEl = new element;
  314. qEl->sentence=sentence2add;
  315. qEl->next=nullptr;
  316.  
  317. if(k.head!=nullptr)
  318. {
  319. k.tail->next=qEl;
  320. }
  321. k.tail=qEl;
  322.  
  323. if(k.head==nullptr)
  324. {
  325. k.head=qEl;
  326. }
  327. }
  328.  
  329. void printQ(que k)
  330. {
  331. while(k.head!=nullptr)
  332. {
  333. cout<<k.head->sentence<<endl;
  334. k.head=k.head->next;
  335. }
  336. }
  337.  
  338. string takeOneFromQ(que &k)
  339. {
  340. string temp="";
  341. if(k.head!=nullptr)
  342. {
  343. temp=k.head->sentence;
  344. k.head=k.head->next;
  345. }
  346. return temp;
  347. }
  348.  
  349. void patternPrint()
  350. {
  351. cout<<endl<<"***************************************************"<<endl<<endl;
  352. }
  353.  
  354. void kmp_array(string pattern, int* &t)
  355. {
  356. int patternLen=pattern.length();
  357.  
  358. t=new int[patternLen+1];
  359. int j=0;
  360.  
  361. t[0]=0;
  362. t[1]=0;
  363.  
  364. for(int i=1; i<=patternLen; i++)
  365. {
  366. while(j>0 && pattern[i]!=pattern[j])
  367. {
  368. j=t[j];
  369. }
  370. if(pattern[i]==pattern[j])
  371. {
  372. j++;
  373. }
  374. t[i+1]=j;
  375. }
  376. }
  377.  
  378. int findPattern(string sentence, string pattern, int* t)
  379. {
  380. int i=0;
  381. int j=0;
  382. int sentenceLen=sentence.length();
  383. int patternLen=pattern.length();
  384. int counter=0;
  385.  
  386. while(i<=sentenceLen-patternLen+1)
  387. {
  388. while(pattern[j]==sentence[i+j] && j<=patternLen)
  389. {
  390. j++;
  391. }
  392. if(j==patternLen)
  393. {
  394. counter++;
  395. }
  396. i=i+max(1,j-t[j]);
  397. j=t[j+1];
  398. }
  399. if(counter>0)
  400. {
  401. return counter;
  402. }
  403. else
  404. return 0;
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement