-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusefulfunc.cpp
More file actions
160 lines (126 loc) · 3 KB
/
Copy pathusefulfunc.cpp
File metadata and controls
160 lines (126 loc) · 3 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
#ifndef USEFUL_H
#include "usefulfunc.h"
#endif
int atoi(char *a) {
int n = strlen(a);
int i = 0;
bool negative = false;
int j = 0;
if (a[0] == '-') {
negative = true;
j++;
} else if (a[0] < '0' || a[0] > '9') return -1;
for ( ; j < n; j++) i = i * 10 + (int)(a[j]) - (int)('0');
if (negative) i *= -1;
return i;
}
int atoi(string a) {
int n = a.size();
int i = 0;
if (a[0] < '0' || a[0] > '9') return -1;
for (int j = 0; j < n; j++) {
i = i * 10 + (int)(a[j]) - (int)('0');
}
return i;
}
string itoa(int i) {
if (i == 0) {
return "0";
}
string temp = "";
if (i < 0) {
temp = "-";
i = -i;
}
int j = i;
while (j > 0) {
int t = j % 10;
temp = temp + (char)(t + (int)'0');
j -= t;
j /= 10;
}
string temp2 = "";
for (int k = temp.size()-1; k >= 0; k--) temp2 += temp[k];
return temp2;
}
string trim(string s) {
int startstring = 0;
while (startstring < s.size() && (s[startstring] < '!' || s[startstring] > '~')) startstring++;
int endstring = s.size()-1;
while (endstring >= startstring && (s[endstring] < '!' || s[endstring] > '~')) endstring--;
string result = "";
for (int i = startstring; i <= endstring; i++) result += s[i];
return result;
}
string round(double v, int s) {
int i = (int)v;
double f = v - (double)i;
double size = pow(10.0,s);
f *= size;
int ff = (int)f;
string result = itoa(i) + "." + itoa(ff);
return result;
}
vector<string> tokenizer(string s) {
vector<string> result(0);
string temp = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
result.push_back(temp);
temp = "";
} else temp += s[i];
}
if (temp.size() > 0) result.push_back(temp);
return result;
}
string tolower(string s) {
int diff = 'a' - 'A';
string temp = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
char newchar = s[i] + diff;
temp = temp + newchar;
} else temp += s[i];
}
return temp;
}
vector< vector<bool> > buildpmask(string filename) {
ifstream infile(filename.c_str());
char buffer[10000];
infile.getline(buffer, 10000);
string temp(buffer);
int kcount = atoi(temp.c_str());
infile.getline(buffer, 10000);
temp = string(buffer);
int excount = atoi(temp.c_str());
vector< vector<bool> > pmask(0);
pmask.reserve(kcount);
for (int i = 0; i < kcount; i++) {
vector<bool> pmask2(0);
pmask2.reserve(excount);
for (int j = 0; j < excount; j++) {
infile.getline(buffer, 10000);
temp = string(buffer);
int val = atoi(temp.c_str());
if (val == 0) {
pmask2.push_back(false);
} else pmask2.push_back(true);
}
pmask.push_back(pmask2);
}
infile.close();
return pmask;
}
void savepmask(vector< vector<bool> > pmask, string filename) {
ofstream ofile(filename.c_str());
ofile << pmask.size() << endl << pmask[0].size() << endl;
for (int i = 0; i < pmask.size(); i++) {
for (int j = 0; j < pmask[i].size(); j++) {
if (pmask[i][j]) {
ofile << "1" << endl;
} else ofile << "0" << endl;
}
}
ofile.close();
}
// !strcasecmp(tempv[0].c_str(), "polynomial")