1sem, 7 work in octave

This commit is contained in:
Andrew 2020-01-11 13:11:02 +07:00
parent 3038462693
commit b68323cca1
5 changed files with 127 additions and 0 deletions

BIN
1sem/octave/07/07.zip Normal file

Binary file not shown.

17
1sem/octave/07/main.m Normal file
View file

@ -0,0 +1,17 @@
# 33 - additive - 4
len = 4;
outf = fopen('results.txt', 'w');
fprintf(outf, 'dk %d\n', len);
maxn = 2^(len - 1) - 1;
for n = randi([-maxn maxn], 1, 100)
nstr = print_num_code(n, len);
nback = read_num_code(nstr);
fprintf(outf, '%d %s\n', n, nstr);
if nback ~= n
error("`%d` not equal to `%d` (converted from `%s`)\n", n, nback, nstr);
end
end
fclose(outf);

View file

@ -0,0 +1,3 @@
function [s] = print_num_code(n, len)
s = dec2bin(mod(n, 2^len), len);
end

View file

@ -0,0 +1,6 @@
function [n] = read_num_code(s)
n = bin2dec(s);
if s(1) == '1'
n -= 2^length(s);
end
end

101
1sem/octave/07/results.txt Normal file
View file

@ -0,0 +1,101 @@
dk 4
-7 1001
7 0111
-3 1101
0 0000
6 0110
3 0011
-2 1110
-2 1110
-7 1001
-5 1011
2 0010
4 0100
4 0100
-4 1100
-4 1100
-4 1100
0 0000
6 0110
-6 1010
-3 1101
2 0010
-6 1010
-1 1111
-2 1110
-2 1110
-3 1101
2 0010
7 0111
-6 1010
0 0000
2 0010
2 0010
-2 1110
-6 1010
4 0100
-6 1010
4 0100
3 0011
4 0100
-3 1101
-4 1100
1 0001
-3 1101
3 0011
-4 1100
-1 1111
6 0110
1 0001
1 0001
7 0111
2 0010
3 0011
-2 1110
0 0000
5 0101
5 0101
-4 1100
-7 1001
-4 1100
0 0000
2 0010
-6 1010
-6 1010
-3 1101
-5 1011
4 0100
-7 1001
1 0001
-6 1010
-7 1001
-5 1011
5 0101
-5 1011
-6 1010
-7 1001
0 0000
1 0001
5 0101
2 0010
-3 1101
2 0010
-4 1100
0 0000
1 0001
-7 1001
0 0000
0 0000
7 0111
5 0101
3 0011
-5 1011
-4 1100
0 0000
-3 1101
-1 1111
-5 1011
-1 1111
-2 1110
7 0111
-6 1010