C_CPP/02_MENT/main_02_MENT.cpp

164 lines
3.8 KiB
C++
Raw Normal View History

2018-03-09 09:47:53 +01:00
// file: main_02_MENT.cpp
// THIS IS C++, use clang++
#include <iostream>
2018-03-16 18:03:32 +01:00
#include "../helpers/println.hpp"
2018-03-09 09:47:53 +01:00
struct PascalString{
int length; // number of chars used
char characters[256]; // chars of some character string
};
2018-04-09 20:59:44 +02:00
char cStringArea[1024];
2018-03-16 18:03:32 +01:00
int hexDigitToInt(char hexDigit) {
int value = 0;
if(hexDigit > 47 && hexDigit < 58)
value = hexDigit - 48;
if(hexDigit > 96 && hexDigit < 103)
value = hexDigit - 97 + 10;
2018-03-09 09:47:53 +01:00
return value;
}
2018-03-16 18:03:32 +01:00
int hexStringToInt(PascalString binaryDigits) {
2018-03-27 21:35:41 +02:00
int returnValue = 0;
2018-03-20 12:16:03 +01:00
2018-03-27 21:35:41 +02:00
for(int i = 0; i <= binaryDigits.length; ++i) {
returnValue += binaryDigits.characters[i];
}
2018-03-09 09:47:53 +01:00
2018-03-27 21:35:41 +02:00
return returnValue;
2018-03-09 09:47:53 +01:00
}
2018-03-20 12:16:03 +01:00
void printPascalString(PascalString s) {
for(int i = 0; i <= s.length; i++) {
2018-04-09 21:04:31 +02:00
print(s.characters[i]);
2018-03-20 12:16:03 +01:00
}
2018-04-09 21:05:53 +02:00
println("");
2018-03-20 12:16:03 +01:00
}
2018-03-27 21:35:41 +02:00
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--;
}
2018-04-09 20:11:03 +02:00
return string;
}
char intToHexChar(int n) {
if(n > 9) {
switch(n) {
2018-04-09 20:59:44 +02:00
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
2018-04-09 20:11:03 +02:00
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'};
2018-03-20 21:00:20 +01:00
2018-04-09 20:11:03 +02:00
for(int i = longString.length; i >= 0; --i) {
2018-04-09 20:59:44 +02:00
if(shortString.length < i) result.characters[i] = longString.characters[i];
result.characters[i] = longString.characters[i] & shortString.characters[i];
2018-04-09 20:11:03 +02:00
}
2018-03-27 21:35:41 +02:00
2018-04-09 20:11:03 +02:00
return result;
2018-03-20 21:00:20 +01:00
}
2018-04-09 20:59:44 +02:00
PascalString bitwiseHexAnd(PascalString a, PascalString b) {
2018-04-09 21:04:31 +02:00
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];
}
2018-04-09 20:59:44 +02:00
2018-04-09 21:04:31 +02:00
return result;
2018-04-09 20:59:44 +02:00
}
int to_c_string(PascalString s) {
}
2018-04-09 21:04:31 +02:00
void line() {
println("===================================");
}
2018-03-16 18:03:32 +01:00
int main(int argc, char** argv, char** envp) {
2018-03-27 21:35:41 +02:00
2018-03-20 21:00:20 +01:00
PascalString s = {3, '1', '0', '0'};
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
2018-03-09 09:47:53 +01:00
println(hexStringToInt(s));
2018-04-09 21:04:31 +02:00
line();
2018-03-09 09:47:53 +01:00
println(hexStringToInt(s2));
2018-04-09 21:04:31 +02:00
line();
2018-03-20 12:16:03 +01:00
println(hexDigitToInt('d'));
2018-04-09 21:04:31 +02:00
line();
2018-03-16 18:03:32 +01:00
println(hexDigitToInt('9'));
2018-04-09 21:04:31 +02:00
line();
2018-03-16 18:03:32 +01:00
println(hexDigitToInt('2'));
2018-03-20 12:16:03 +01:00
printPascalString(s2);
2018-04-09 21:04:31 +02:00
line();
2018-03-20 12:16:03 +01:00
2018-03-20 21:00:20 +01:00
int controlRegister = 128;
controlRegister |= 64+32;
controlRegister ^=16;
controlRegister &= 128+64;
controlRegister <<= 1;
println(controlRegister);
2018-04-09 21:04:31 +02:00
line();
2018-04-09 20:11:03 +02:00
println(intToHexChar(6));
2018-04-09 21:04:31 +02:00
line();
2018-04-09 20:11:03 +02:00
println(intToHexChar(10));
2018-04-09 21:04:31 +02:00
line();
2018-04-09 20:11:03 +02:00
printPascalString(intToDual(4106));
2018-04-09 21:04:31 +02:00
line();
2018-04-09 20:11:03 +02:00
printPascalString(intToHex(965));
2018-04-09 21:04:31 +02:00
line();
2018-04-09 20:11:03 +02:00
printPascalString(intToHex(1956));
2018-04-09 21:04:31 +02:00
line();
2018-04-09 20:11:03 +02:00
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {4, '1', '1', '1', '0'}));
2018-04-09 21:04:31 +02:00
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'}));
2018-03-27 21:35:41 +02:00
2018-03-20 21:00:20 +01:00
return 0;
2018-03-09 09:47:53 +01:00
}