Praktikum 2: Testat
This commit is contained in:
parent
73e17aae12
commit
199ca3ece1
Binary file not shown.
|
@ -10,11 +10,48 @@ struct PascalString{
|
||||||
char characters[256]; // chars of some character string
|
char characters[256]; // chars of some character string
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv, char** envp) {
|
void printPascalString(PascalString string) {
|
||||||
|
for(int i = 0; i <= string.length; ++i) {
|
||||||
PascalString s = {3, '1', '0', '0'};
|
print(string.characters[i]);
|
||||||
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
|
}
|
||||||
|
println("");
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PascalString dualOr(PascalString a, PascalString b) {
|
||||||
|
PascalString longString = a.length >= b.length ? a : b;
|
||||||
|
PascalString shortString = a.length >= b.length ? b : a;
|
||||||
|
|
||||||
|
|
||||||
|
PascalString result = {longString.length};
|
||||||
|
|
||||||
|
/*
|
||||||
|
for(int i = longString.length; i >= 0; --i) {
|
||||||
|
if(i >= shortString.length) result.characters[i] = '0';
|
||||||
|
else result.characters[i] = shortString.characters[i];
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
for(int i = 0; i < longString.length; i++) {
|
||||||
|
if(i <= shortString.length) {
|
||||||
|
result.characters[i] = longString.characters[i];
|
||||||
|
//println("lang ", longString.characters[i]);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
result.characters[i] = longString.characters[i] | shortString.characters[i];
|
||||||
|
//println("beides ", shortString.characters[i] | longString.characters[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv, char** envp) {
|
||||||
|
printPascalString(dualOr({2, '0', '1'}, {2, '1', '0'}));
|
||||||
|
printPascalString(dualOr({2, '0', '1'}, {3, '1', '1', '1'}));
|
||||||
|
printPascalString(dualOr({3, '1', '1', '1'}, {4, '1', '1', '0', '1'}));
|
||||||
|
printPascalString(dualOr({3, '0', '0', '0'}, {5, '1', '1', '0', '1', '0'}));
|
||||||
|
printPascalString(dualOr({3, '0', '1', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||||
|
printPascalString(dualOr({2, '0', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||||
|
printPascalString(dualOr({2, '1', '1'}, {6, '1', '0', '0', '1', '0', '0'}));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,7 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
rm *.out
|
||||||
|
|
||||||
|
clang++-6.0 -std=c++14 main_02_MENT.cpp
|
||||||
|
|
||||||
|
./a.out
|
|
@ -0,0 +1,51 @@
|
||||||
|
// file: main_02_MENT.cpp
|
||||||
|
// THIS IS C++, use clang++
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "../../helpers/println.hpp"
|
||||||
|
|
||||||
|
struct PascalString{
|
||||||
|
int length; // number of chars used
|
||||||
|
char characters[256]; // chars of some character string
|
||||||
|
};
|
||||||
|
|
||||||
|
void printPascalString(PascalString s) {
|
||||||
|
for(int i = 0; i <= s.length; ++i) {
|
||||||
|
print(s.characters[i]);
|
||||||
|
}
|
||||||
|
println("");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
PascalString bitwiseDualOr(PascalString a, PascalString b) {
|
||||||
|
PascalString shortString = a.length <= b.length ? a : b;
|
||||||
|
PascalString longString = a.length <= b.length ? b : a;
|
||||||
|
|
||||||
|
PascalString result = {longString.length};
|
||||||
|
|
||||||
|
for(int i = 1; i <= shortString.length; ++i) {
|
||||||
|
result.characters[result.length -i] = longString.characters[longString.length -i] | shortString.characters[shortString.length -i];
|
||||||
|
//println(result.length -i, ": ",result.characters[result.length -i]," l=",longString.length -i, ":",longString.characters[longString.length -i]," s=",shortString.length -i,":", shortString.characters[shortString.length -i]);
|
||||||
|
}
|
||||||
|
for(int i = (longString.length - shortString.length)-1; i >= 0 ; --i) {
|
||||||
|
result.characters[i] = longString.characters[i];
|
||||||
|
//println(i, ": ", result.characters[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv, char** envp) {
|
||||||
|
|
||||||
|
printPascalString(bitwiseDualOr({2, '0', '1'}, {2, '1', '0'}));
|
||||||
|
printPascalString(bitwiseDualOr({2, '0', '1'}, {3, '1', '1', '1'}));
|
||||||
|
printPascalString(bitwiseDualOr({3, '1', '1', '1'}, {4, '1', '1', '0', '1'}));
|
||||||
|
printPascalString(bitwiseDualOr({3, '0', '0', '0'}, {5, '1', '1', '0', '1', '0'}));
|
||||||
|
printPascalString(bitwiseDualOr({3, '0', '1', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||||
|
printPascalString(bitwiseDualOr({2, '0', '1'}, {6, '1', '1', '0', '1', '0', '1'}));
|
||||||
|
printPascalString(bitwiseDualOr({2, '1', '1'}, {6, '1', '0', '0', '1', '0', '0'}));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
|
@ -92,26 +92,30 @@ int getInt(char character) {
|
||||||
}
|
}
|
||||||
|
|
||||||
PascalString bitwiseDualAnd(PascalString a, PascalString b) {
|
PascalString bitwiseDualAnd(PascalString a, PascalString b) {
|
||||||
PascalString result = {a.length, '0'};
|
PascalString result = {b.length};
|
||||||
|
|
||||||
for(int i = b.length; i >= 0; --i) {
|
for(int i = 0; i <= b.length; ++i) {
|
||||||
if(a.length < i) result.characters[i] = a.characters[i];
|
result.characters[i] = a.characters[i] & b.characters[i];
|
||||||
println("i: ", i, " ", intToHexChar(getInt(a.characters[i]) & getInt(b.characters[i])));
|
|
||||||
result.characters[i] = intToHexChar(getInt(a.characters[i]) & getInt(b.characters[i]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PascalString bitwiseHexAnd(PascalString a, PascalString b) {
|
PascalString bitwiseOctalAnd(PascalString a, PascalString b) {
|
||||||
PascalString result = {a.length, '0'};
|
PascalString result = {b.length, '0'};
|
||||||
|
|
||||||
for(int i = b.length; i >= 0; --i) {
|
for(int i = 0; i <= b.length; ++i) {
|
||||||
if(a.length < i) result.characters[i] = a.characters[i];
|
|
||||||
println("i: ", i, " ", a.characters[i] & b.characters[i]);
|
|
||||||
result.characters[i] = a.characters[i] & b.characters[i];
|
result.characters[i] = a.characters[i] & b.characters[i];
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PascalString bitwiseHexAnd(PascalString a, PascalString b) {
|
||||||
|
a = intToDual(hexStringToInt(a));
|
||||||
|
b = intToDual(hexStringToInt(b));
|
||||||
|
PascalString result = bitwiseDualAnd(a, b);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +128,7 @@ println("===================================");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv, char** envp) {
|
int main(int argc, char** argv, char** envp) {
|
||||||
|
|
||||||
PascalString s = {3, '1', '0', '0'};
|
PascalString s = {3, '1', '0', '0'};
|
||||||
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
|
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
|
||||||
println(hexStringToInt(s));
|
println(hexStringToInt(s));
|
||||||
|
@ -159,13 +163,19 @@ int main(int argc, char** argv, char** envp) {
|
||||||
printPascalString(intToHex(1956));
|
printPascalString(intToHex(1956));
|
||||||
|
|
||||||
line();
|
line();
|
||||||
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '0'}));
|
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {3, '1', '1', '1'}));
|
||||||
line();
|
line();
|
||||||
printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {4, '1', '1', '1', '0'}));
|
printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {3, '1', '1', '1'}));
|
||||||
line();
|
line();
|
||||||
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '1'}));
|
printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {3, '1', '0', '1'}));
|
||||||
line();
|
line();
|
||||||
printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {4, '1', '0', '1', '1'}));
|
printPascalString(bitwiseDualAnd({3, '1', '0', '0'}, {3, '1', '1', '1'}));
|
||||||
|
line();
|
||||||
|
printPascalString(bitwiseOctalAnd({2, '6', '3'}, {2, '1', '2'}));
|
||||||
|
|
||||||
|
|
||||||
|
line();
|
||||||
|
printPascalString(bitwiseHexAnd({2, 'A', '1'}, {2, 'C', '2'}));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,164 @@
|
||||||
|
// file: main_02_MENT.cpp
|
||||||
|
// THIS IS C++, use clang++
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "../helpers/println.hpp"
|
||||||
|
|
||||||
|
struct PascalString{
|
||||||
|
int length; // number of chars used
|
||||||
|
char characters[256]; // chars of some character string
|
||||||
|
};
|
||||||
|
|
||||||
|
char cStringArea[1024];
|
||||||
|
|
||||||
|
int hexDigitToInt(char hexDigit) {
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
if(hexDigit > 47 && hexDigit < 58)
|
||||||
|
value = hexDigit - 48;
|
||||||
|
|
||||||
|
if(hexDigit > 96 && hexDigit < 103)
|
||||||
|
value = hexDigit - 97 + 10;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hexStringToInt(PascalString binaryDigits) {
|
||||||
|
int returnValue = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i <= binaryDigits.length; ++i) {
|
||||||
|
returnValue += binaryDigits.characters[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printPascalString(PascalString s) {
|
||||||
|
for(int i = 0; i <= s.length; i++) {
|
||||||
|
print(s.characters[i]);
|
||||||
|
}
|
||||||
|
println(""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
PascalString intToDual(int n) {
|
||||||
|
|
||||||
|
int i = std::to_string(n).length() * 4;
|
||||||
|
PascalString string = {i};
|
||||||
|
while(n >= 1) {
|
||||||
|
string.characters[i] = (n % 2) + '0';
|
||||||
|
n = n / 2;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
char intToHexChar(int n) {
|
||||||
|
if(n > 9) {
|
||||||
|
switch(n) {
|
||||||
|
case 10: return 'A';
|
||||||
|
case 11: return 'B';
|
||||||
|
case 12: return 'C';
|
||||||
|
case 13: return 'D';
|
||||||
|
case 14: return 'E';
|
||||||
|
case 15: return 'F';
|
||||||
|
default: return 'X';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else return n + '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
PascalString intToHex(int n) {
|
||||||
|
int i = std::to_string(n).length();
|
||||||
|
PascalString string = {i};
|
||||||
|
while(n >= 1) {
|
||||||
|
string.characters[i] = intToHexChar(n % 16);
|
||||||
|
n = n / 16;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
PascalString bitwiseDualAnd(PascalString a, PascalString b) {
|
||||||
|
PascalString shortString = a.length < b.length ? a : b;
|
||||||
|
PascalString longString = a.length > b.length ? a : b;
|
||||||
|
PascalString result = {longString.length, '0'};
|
||||||
|
|
||||||
|
for(int i = longString.length; i >= 0; --i) {
|
||||||
|
if(shortString.length < i) result.characters[i] = longString.characters[i];
|
||||||
|
result.characters[i] = longString.characters[i] & shortString.characters[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
PascalString bitwiseHexAnd(PascalString a, PascalString b) {
|
||||||
|
PascalString shortString = a.length < b.length ? a : b;
|
||||||
|
PascalString longString = a.length > b.length ? a : b;
|
||||||
|
PascalString result = {longString.length, '0'};
|
||||||
|
|
||||||
|
for(int i = longString.length; i >= 0; --i) {
|
||||||
|
if(shortString.length < i) result.characters[i] = longString.characters[i];
|
||||||
|
result.characters[i] = longString.characters[i] & shortString.characters[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int to_c_string(PascalString s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void line() {
|
||||||
|
println("===================================");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv, char** envp) {
|
||||||
|
|
||||||
|
PascalString s = {3, '1', '0', '0'};
|
||||||
|
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
|
||||||
|
println(hexStringToInt(s));
|
||||||
|
line();
|
||||||
|
println(hexStringToInt(s2));
|
||||||
|
line();
|
||||||
|
println(hexDigitToInt('d'));
|
||||||
|
line();
|
||||||
|
println(hexDigitToInt('9'));
|
||||||
|
line();
|
||||||
|
println(hexDigitToInt('2'));
|
||||||
|
|
||||||
|
printPascalString(s2);
|
||||||
|
line();
|
||||||
|
|
||||||
|
int controlRegister = 128;
|
||||||
|
controlRegister |= 64+32;
|
||||||
|
controlRegister ^=16;
|
||||||
|
controlRegister &= 128+64;
|
||||||
|
controlRegister <<= 1;
|
||||||
|
|
||||||
|
println(controlRegister);
|
||||||
|
line();
|
||||||
|
println(intToHexChar(6));
|
||||||
|
line();
|
||||||
|
println(intToHexChar(10));
|
||||||
|
line();
|
||||||
|
printPascalString(intToDual(4106));
|
||||||
|
line();
|
||||||
|
printPascalString(intToHex(965));
|
||||||
|
line();
|
||||||
|
printPascalString(intToHex(1956));
|
||||||
|
|
||||||
|
line();
|
||||||
|
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '0'}));
|
||||||
|
line();
|
||||||
|
printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {4, '1', '1', '1', '0'}));
|
||||||
|
line();
|
||||||
|
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '1'}));
|
||||||
|
line();
|
||||||
|
printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {4, '1', '0', '1', '1'}));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue