-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (159 loc) · 3.75 KB
/
main.cpp
File metadata and controls
180 lines (159 loc) · 3.75 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using std::vector;
int atoi(std::string str)
{
vector<int> vi;
int res = 0, temp = 1;
for(char &c : str)
{
if(isdigit(c))
vi.push_back(c-'0');
else return -1;
}
for(int i = 0; i < vi.size(); ++i)
std::cout << vi[i] << "\n";
for (int i = 0; i < vi.size(); ++i)
{
res += temp*vi[vi.size()-1-i];
temp *=10;
}
return res;
}
vector<std::string> getWordVec(std::string input)
{
vector<std::string> vstr;
std::ostringstream os;
std::ifstream ifs;
ifs.open(input, std::ifstream::in);
char c = ifs.get();
while (ifs.good())
{
while(isalpha(c) || c == '-'){
os << c;
c = ifs.get();
}
c = ifs.get();
if(os.str().size() > 0) vstr.push_back(os.str());
os.str(std::string());
}
ifs.close();
return vstr;
}
void countWords(std::string input)
{
vector<std::string> vstr = getWordVec(input);
vector<std::string> vstrclean;
vector<int> vcnt;
int cnt = 1;
std::sort(vstr.begin(), vstr.end());
for(int i = 0; i < vstr.size()-1; ++i)
{
if(vstr[i] != vstr[i+1])
{
vstrclean.push_back(vstr[i]);
vcnt.push_back(cnt);
cnt = 1;
}
if(vstr[i] == vstr[i+1]) ++cnt;
}
for (int i = 0; i < vstrclean.size(); ++i)
{
std::cout << vcnt[i] << "\t\t||\t\t'" << vstrclean[i] << "'\n";
}
}
int countStringInFile(std::string inputString, std::string textfile)
{
std::ostringstream os;
std::string str = inputString;
std::ifstream ifs;
ifs.open (textfile, std::ifstream::in);
char c = ifs.get();
while (ifs.good()) {
os << c;
c = ifs.get();
}
ifs.close();
std::size_t found = os.str().find(str);
size_t cnt = 0;
if (found!=std::string::npos){
//std::cout << "first '" << str<<"' found at: " << found << '\n';
++cnt;
}
while(found < os.str().size() - str.size()){
found=os.str().find(str,found+1);
if (found!=std::string::npos) ++cnt; //std::cout << "next '"<< str << "' found at: " << found << '\n';
}
//std::cout << cnt << " '" << str << "' gefunden";
return cnt;
}
void getLetterCount(std::string textfile)
{
std::string st = "";
std::vector<char> alpha;
for (int i = 0; i < 26; ++i)
alpha.push_back('A'+i);
for (int i = 0; i < 26; ++i)
alpha.push_back('a'+i);
std::vector<int> count;
for(int i = 0; i < 52; ++i){
st = alpha[i];
count.push_back(countStringInFile(st,textfile));
}
for(int i = 0; i < 52; ++i)
std::cout << count[i] << "\t||\t" << alpha[i] << "\n";
}
int euklid(int p, int q)
{
int r;
if (q == 0) return p;
r= p%q;
euklid(q,r);
}
std::vector<int> insertionSort(std::vector<int> v)
{
int key, i;
for (int j = 1; j < v.size(); ++j)
{
key = v[j];
i = j -1;
while(i >= 0 && v[i] > key)
{
v[i+1] = v[i];
--i;
}
v[i+1] = key;
}
return v;
}
std::vector<int> decInsertionSort(std::vector<int> v)
{
int key, i;
for(int j = 1; j < v.size(); ++j)
{
key = v[j];
i = j - 1;
while(i >= 0 && v[i] < key)
{
v[i+1] = v[i];
--i;
}
v[i+1] = key;
}
return v;
}
int main()
{
std::vector<int> vec{1,4,6,3,2,7,2,9,10,34,225};
std::vector<int> v1,v2;
v1 = insertionSort(vec);
v2 = decInsertionSort(vec);
for(int i = 0; i< v1.size(); ++i)
std::cout << "unsorted:\t" << vec[i] << "\tincreasing sorted:\t" << v1[i]
<< "\tdecreasing sorted:\t" << v2[i] << "\n";
return 0;
}