C_CPP/02_MENT/main_02_MENT.cpp

167 lines
3.7 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-04-09 21:19:20 +02:00
struct PascalString{
int length; // number of chars used
char characters[256]; // chars of some character string
2018-03-09 09:47:53 +01:00
};
2018-04-09 20:59:44 +02:00
char cStringArea[1024];
2018-03-16 18:03:32 +01:00
int hexDigitToInt(char hexDigit) {
2018-04-09 21:19:20 +02:00
int value = 0;
2018-03-16 18:03:32 +01:00
2018-04-09 21:19:20 +02:00
if(hexDigit > 47 && hexDigit < 58)
value = hexDigit - 48;
2018-03-16 18:03:32 +01:00
2018-04-09 21:19:20 +02:00
if(hexDigit > 96 && hexDigit < 103)
value = hexDigit - 97 + 10;
2018-03-16 18:03:32 +01:00
2018-04-09 21:19:20 +02:00
return value;
2018-03-09 09:47:53 +01:00
}
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-04-09 21:19:20 +02:00
for(int i = 0; i <= binaryDigits.length; ++i) {
2018-03-27 21:35:41 +02:00
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) {
2018-04-09 21:19:20 +02:00
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) {
2018-04-09 21:18:33 +02:00
2018-03-27 21:35:41 +02:00
int i = std::to_string(n).length() * 4;
PascalString string = {i};
2018-04-09 21:19:20 +02:00
while(n >= 1) {
2018-03-27 21:35:41 +02:00
string.characters[i] = (n % 2) + '0';
n = n / 2;
i--;
}
2018-04-09 20:11:03 +02:00
return string;
}
char intToHexChar(int n) {
2018-04-09 21:23:22 +02:00
if(n < 9) {
2018-04-09 21:19:20 +02:00
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';
2018-04-09 20:11:03 +02:00
}
2018-04-09 21:19:20 +02:00
}
else return n + '0';
2018-04-09 20:11:03 +02:00
}
PascalString intToHex(int n) {
int i = std::to_string(n).length();
PascalString string = {i};
2018-04-09 21:19:20 +02:00
while(n >= 1) {
2018-04-09 20:11:03 +02:00
string.characters[i] = intToHexChar(n % 16);
n = n / 16;
i--;
}
return string;
}
2018-04-09 21:22:13 +02:00
int getInt(char character) {
if(character == '0') return character;
else return '1';
}
2018-04-09 20:11:03 +02:00
PascalString bitwiseDualAnd(PascalString a, PascalString b) {
2018-04-09 21:18:33 +02:00
PascalString result = {a.length, '0'};
2018-04-09 21:19:20 +02:00
for(int i = b.length; i >= 0; --i) {
if(a.length < i) result.characters[i] = a.characters[i];
2018-04-09 21:22:13 +02:00
println("i: ", i, " ", intToHexChar(getInt(a.characters[i]) & getInt(b.characters[i])));
result.characters[i] = intToHexChar(getInt(a.characters[i]) & getInt(b.characters[i]));
2018-04-09 20:11:03 +02:00
}
2018-04-09 21:18:33 +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:12:50 +02:00
PascalString result = {a.length, '0'};
2018-04-09 21:04:31 +02:00
2018-04-09 21:19:20 +02:00
for(int i = b.length; i >= 0; --i) {
if(a.length < i) result.characters[i] = a.characters[i];
println("i: ", i, " ", a.characters[i] & b.characters[i]);
2018-04-09 21:12:50 +02:00
result.characters[i] = a.characters[i] & b.characters[i];
2018-04-09 21:04:31 +02:00
}
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() {
2018-04-09 21:19:20 +02:00
println("===================================");
2018-04-09 21:04:31 +02:00
}
2018-04-09 21:19:20 +02:00
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;
2018-03-09 09:47:53 +01:00
}