-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext1.cc
More file actions
143 lines (125 loc) · 3.15 KB
/
text1.cc
File metadata and controls
143 lines (125 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "text.hh"
#include <iostream>
#include <sstream>
using namespace std;
bool text::cmp(pair<int,string> a, pair<int,string> b) {
if (a.first != b.first) return a.first > b.first;
else if (a.second.length() != b.second.length()) return a.second.length() < b.second.length();
else return a.second < b.second;
}
void text::input_text(){
string s;
string frase;
nfrases = 0;
nparaules = 0;
cin >> s;
bool principi = true;
while (s!="****") {
if(not principi) frase += ' ';
else principi = false;
frase += s;
nparaules ++;
if (s[s.length()-1] == '.' or s[s.length()-1] == '!' or s[s.length()-1] == '?') { // ultima palabra de frase
contingut.push_back(frase);
frase = "";
principi = true;
nfrases++;
}
cin >> s;
}
}
void text::substituir(string a, string b){
int it =0;
while (it < nfrases){
istringstream frase (contingut[it]);
string s, novafrase;
bool first = true;
while (frase >> s){
char c = deformatword(s);
if(s==a) s=b;
if (c!=0) s+=c;
if (not first) novafrase += ' ';
novafrase += s;
first = false;
}
contingut[it] = novafrase;
++it;
}
}
void text::escriure() const {
int c=0;
while (c<nfrases) {
cout << c+1 << ' ' << contingut[c] << endl;
c++;
}
}
void text::escriurefrases(int x, int y) const {
if (y < x or x < 1 or y > contingut.size()) {cout << "error" << endl; return;}
while (x <= y) {cout << x << ' ' << contingut [x-1] << endl; x++;}
}
vector<string> text::retornarfrases (int x, int y) const {
vector<string> toreturn;
while (x <= y) {toreturn.push_back(contingut[x-1]); x++;}
return toreturn;
}
bool text::buscar_paraula(string buscar) const {
bool b = false;
int i = 0;
while (not b and i < nfrases){
istringstream frase(contingut[i]);
string s;
while (not b and frase >> s) {
deformatword(s);
if(s==buscar) b=true;
}
++i;
}
return b;
}
int text::return_nparaules() const {
return nparaules;
}
int text::return_nfrases() const {
return nfrases;
}
vector<string> text::return_referencies() const {
return referencies;
}
vector< pair <int, string> > text::return_tdf() const {
vector< pair <int, string> > taularetorn;
for (int i=0; i<nfrases; i++) {
istringstream frase(contingut[i]);
string s;
while (frase >> s){
deformatword(s);
int i=0;
bool found=false;
while(not found and i < taularetorn.size()) {
if(taularetorn[i].second == s) {
found = true;
taularetorn[i].first++;
}
i++;
}
if (not found){
pair<int,string> topush (1,s);
taularetorn.push_back(topush);
}
}
}
sort(taularetorn.begin(), taularetorn.end(), cmp);
return taularetorn;
}
void text::afegir_cita(string referencia){
referencies.push_back(referencia);
}
void text::borrar_cita(string referencia){
int i = 0;
while(i < referencies.size() and referencies[i] != referencia) i++;
if (i == referencies.size()) { cout << "Error" << endl; return;}
while(i < referencies.size()-1){
referencies[i] = referencies[i+1];
i++;
}
referencies.pop_back();
}