Skip to content

Commit 4e03ebe

Browse files
author
Eugene Yurtaev
committed
seminar 3
1 parent b25d910 commit 4e03ebe

File tree

10 files changed

+326
-0
lines changed

10 files changed

+326
-0
lines changed

arena.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <ctype.h>
5+
6+
using namespace std;
7+
8+
struct Item {
9+
Item *next;
10+
long long value;
11+
};
12+
13+
class Arena {
14+
15+
};
16+
17+
18+
int main(int argc, char *argv[]) {
19+
char str[] = "first line \nsecond line";
20+
normalize_file(str);
21+
for (int i = 0; str[i] != '\0';i++){
22+
printf ("%c", str[i]);
23+
}
24+
}
25+

besum.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <string>
5+
6+
using namespace std;
7+
8+
int main(int argc, char *argv[]) {
9+
for (size_t i = 1; i < (size_t) argc; ++i) {
10+
string filename(argv[i]);
11+
ifstream file(filename, ios::in | ios::binary);
12+
13+
char * buffer = new char [4];
14+
do {
15+
file.read(buffer, 4);
16+
cout << buffer[0] << buffer[1] << buffer[2] << buffer[3] << endl;
17+
unsigned int x;
18+
stringstream ss;
19+
ss << std::hex << buffer[0] << buffer[1] << buffer[2] << buffer[3];
20+
ss >> x;
21+
cout << x;
22+
} while (!file.eof());
23+
}
24+
}
25+

bitcount.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
int main(int argc, char *argv[]) {
7+
for (size_t i = 1; i < (size_t) argc; ++i) {
8+
string str(argv[i]);
9+
unsigned int number;
10+
size_t pos = 0;
11+
size_t arg_len = str.size();
12+
try {
13+
number = stoul(str, &pos);
14+
}
15+
catch (...) {
16+
cout << -1 << endl;
17+
continue;
18+
}
19+
if (pos != arg_len) {
20+
cout << -1 << endl;
21+
continue;
22+
}
23+
// first method
24+
unsigned int bitscount = 0;
25+
for (size_t k = 0; k < 32; ++k) {
26+
unsigned int shifted = number >> k;
27+
if (shifted == 0) {
28+
break;
29+
}
30+
if (shifted & 1) {
31+
++bitscount;
32+
}
33+
}
34+
// second method
35+
unsigned int bitscount2 = __builtin_popcount(number);
36+
if (bitscount == bitscount2)
37+
cout << bitscount << endl;
38+
else
39+
cout << "-1";
40+
}
41+
}
42+

fclassify.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <bitset>
5+
#include <string>
6+
7+
using namespace std;
8+
9+
bitset<32> getbits(float f) {
10+
union {
11+
float input;
12+
unsigned int output;
13+
} data;
14+
data.input = f;
15+
bitset<32> bits(data.output);
16+
return bits;
17+
}
18+
19+
enum class FPClass
20+
{
21+
ZERO, DENORMALIZED, NORMALIZED, INF, NAN
22+
};
23+
24+
25+
int main() {
26+
27+
float f;
28+
while (cin >> f) {
29+
bits = getbits(f);
30+
unsigned int exponent = 0;
31+
for (int i = 0; i < 8; ++i) {
32+
exponent += bits[23 + i] * (1 << i);
33+
}
34+
unsigned int frac = 0;
35+
for (int i = 0; i < 23; ++i) {
36+
frac += bits[i] * (1 << i);
37+
}
38+
cout << dec << bits[31] << " " << exponent << " ";
39+
cout << hex << frac << endl;
40+
}
41+
}

file

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ffff000031f21010

fsplit.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <bitset>
5+
#include <string>
6+
7+
using namespace std;
8+
9+
10+
int main() {
11+
float f;
12+
while (cin >> f) {
13+
union {
14+
float input;
15+
unsigned int output;
16+
} data;
17+
18+
data.input = f;
19+
20+
bitset<32> bits(data.output);
21+
22+
unsigned int exponent = 0;
23+
for (int i = 0; i < 8; ++i) {
24+
exponent += bits[23 + i] * (1 << i);
25+
}
26+
unsigned int frac = 0;
27+
for (int i = 0; i < 23; ++i) {
28+
frac += bits[i] * (1 << i);
29+
}
30+
cout << dec << bits[31] << " " << exponent << " ";
31+
cout << hex << frac << endl;
32+
}
33+
}

intfit.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <string>
5+
6+
using namespace std;
7+
8+
bool isProperlyRepresented(unsigned int n) {
9+
if (n <= 16777216) {
10+
return 1;
11+
}
12+
bool bits[32];
13+
for (size_t k = 0; k < 32; ++k) {
14+
unsigned int shifted = n >> k;
15+
if (shifted & 1) {
16+
bits[k] = 1;
17+
} else {
18+
bits[k] = 0;
19+
}
20+
}
21+
int left_shift = 0, right_shift = 32;
22+
for (int i = 0; i < 32; ++i) {
23+
if (bits[i] == 1) {
24+
right_shift = i;
25+
}
26+
}
27+
for (int j = 31; j >= 0; --j) {
28+
if (bits[j] == 1) {
29+
left_shift = j;
30+
}
31+
}
32+
// cout << left_shift << " " << right_shift << endl;
33+
if (right_shift - left_shift + 1 > 24) {
34+
return 0;
35+
}
36+
return 1;
37+
}
38+
39+
int main() {
40+
unsigned int n;
41+
42+
while (cin >> n) {
43+
// float f = (float) n;
44+
// unsigned int nn = (unsigned int) f;
45+
// cout << n << " " << nn << endl;
46+
// cout << bitset<32>(n) << endl;
47+
cout << (isProperlyRepresented(n) ? "1\n":"0\n");
48+
}
49+
50+
// for (int i = 2000000000; i < 9000000000; ++i) {
51+
// float f = (float) i;
52+
// int ii = (int) f;
53+
// if (i != ii && isProperlyRepresented(i)) {
54+
// cout << i << " " << ii << "wuuut" << endl;
55+
// }
56+
// // cout << (isProperlyRepresented(n) ? "1\n":"0\n");
57+
// }
58+
}

norm_path.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <string>
5+
6+
using namespace std;
7+
8+
9+
void normalize_path(char *buf) {
10+
int cur = 0;
11+
bool is_slash = false;
12+
for (int pos = 0; buf[pos] != '\0'; ++pos) {
13+
if (!is_slash && buf[pos] == '/') {
14+
buf[cur] = '/';
15+
is_slash = true;
16+
++cur;
17+
} else if (buf[pos] != '/') {
18+
buf[cur] = buf[pos];
19+
++cur;
20+
is_slash = false;
21+
}
22+
}
23+
buf[cur] = '\0';
24+
}
25+
26+
int main(int argc, char *argv[]) {
27+
char str[] = "///a//b///c/";
28+
normalize_path(str);
29+
for (int i = 0; str[i] != '\0';i++){
30+
printf ("%c", str[i]);
31+
}
32+
}
33+

normalize_file.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// #include <iostream>
2+
// #include <fstream>
3+
// #include <sstream>
4+
#include <ctype.h>
5+
6+
// using namespace std;
7+
8+
void normalize_file(char *buf) {
9+
if (buf[0] == '\0')
10+
return;
11+
int cur = 0;
12+
int pos = 0;
13+
int spaces_started = -1;
14+
while (pos == 0 || buf[pos - 1] != '\0') {
15+
if (buf[pos] == '\n' || buf[pos] == '\0') {
16+
if (buf[pos] == '\0') {
17+
if (buf[pos - 1] != '\n') {
18+
buf[cur++] = '\n';
19+
buf[cur++] = '\0';
20+
} else {
21+
buf[cur++] = '\0';
22+
}
23+
break;
24+
} else {
25+
buf[cur++] = '\n';
26+
}
27+
spaces_started = -1;
28+
} else if (isspace(buf[pos])) {
29+
if (spaces_started == -1) {
30+
spaces_started = pos;
31+
}
32+
} else {
33+
if (spaces_started != -1) {
34+
for (int j = spaces_started; j <= pos; ++j) {
35+
buf[cur++] = buf[j];
36+
}
37+
spaces_started = -1;
38+
} else {
39+
buf[cur++] = buf[pos];
40+
}
41+
}
42+
++pos;
43+
}
44+
}
45+
46+
47+
48+
// int main(int argc, char *argv[]) {
49+
// char str[] = "first line \nsecond line";
50+
// normalize_file(str);
51+
// for (int i = 0; str[i] != '\0';i++){
52+
// printf ("%c", str[i]);
53+
// }
54+
// }
55+

satsum.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// #include <iostream>
2+
3+
// using namespace std;
4+
5+
6+
unsigned satsum(unsigned v1, unsigned v2) {
7+
unsigned v3 = v1 + v2;
8+
if (v3 < v1) {
9+
return -1;
10+
} else {
11+
return v3;
12+
}
13+
}

0 commit comments

Comments
 (0)