C_CPP/02_MENT/main_02_MENT.cpp

83 lines
1.5 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-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++) {
println(s.characters[i]);
}
}
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-03-20 21:00:20 +01:00
2018-03-27 21:35:41 +02:00
return string;
2018-03-20 21:00:20 +01:00
}
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));
println(hexStringToInt(s2));
2018-03-16 18:03:32 +01:00
2018-03-20 12:16:03 +01:00
println(hexDigitToInt('d'));
2018-03-16 18:03:32 +01:00
println(hexDigitToInt('9'));
println(hexDigitToInt('2'));
2018-03-20 12:16:03 +01:00
printPascalString(s2);
2018-03-20 21:00:20 +01:00
int controlRegister = 128;
controlRegister |= 64+32;
controlRegister ^=16;
controlRegister &= 128+64;
controlRegister <<= 1;
println(controlRegister);
2018-03-27 21:35:41 +02:00
printPascalString(intToDual(41));
2018-03-20 21:00:20 +01:00
return 0;
2018-03-09 09:47:53 +01:00
}