C_CPP/02_MENT/Testat/main_02_MENT.cpp

53 lines
1.9 KiB
C++
Raw Normal View History

2018-04-09 20:11:03 +02:00
// 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]);
2018-04-17 14:14:38 +02:00
}
println("");
2018-04-17 14:14:38 +02:00
}
2018-04-09 20:11:03 +02:00
PascalString bitwiseDualOr(PascalString a, PascalString b) {
PascalString shortString = a.length <= b.length ? a : b;
PascalString longString = a.length <= b.length ? b : a;
2018-04-17 14:14:38 +02:00
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]);
2018-04-17 14:14:38 +02:00
}
for(int i = (longString.length - shortString.length)-1; i >= 0 ; --i) {
result.characters[i] = longString.characters[i];
//println(i, ": ", result.characters[i]);
2018-04-17 14:14:38 +02:00
}
return result;
2018-04-09 20:11:03 +02:00
}
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'}));
2018-04-17 14:14:38 +02:00
return 0;
}