04_UDEF_MP BinaryOctet funktionierende Subtraktion
This commit is contained in:
parent
3f7f982922
commit
cb3cb443cd
|
@ -19,6 +19,7 @@ void printPascalString(PascalString s) {
|
|||
}
|
||||
|
||||
PascalString bitwiseDualOr(PascalString a, PascalString b) {
|
||||
|
||||
PascalString shortString = a.length <= b.length ? a : b;
|
||||
PascalString longString = a.length <= b.length ? b : a;
|
||||
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
#!/bin/bash
|
||||
clang++ -std=c++14 -o controlFlowIntr main_ISR.cpp
|
Binary file not shown.
|
@ -1,27 +0,0 @@
|
|||
// file main_ISR.cpp
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
volatile int j;
|
||||
|
||||
//extern "C" {
|
||||
void ISR(int iarg){
|
||||
std::cout << "ISR j=" << j << "\n";
|
||||
}
|
||||
//}
|
||||
|
||||
void install_ISR(void){
|
||||
signal(SIGUSR1, ISR);
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
std::cout << "process ID = " << getpid() << "\n";
|
||||
install_ISR();
|
||||
for(int i=0; i<300*1000*1000; i++){
|
||||
i= i +10; i-=10; j=i;
|
||||
}
|
||||
std::cout << "done.\n";
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
#!/bin/bash
|
||||
./controlFlowIntr &
|
||||
echo $!
|
||||
sleep 1
|
||||
kill -SIGUSR1 $!
|
||||
sleep 1
|
||||
kill -SIGUSR1 $!
|
||||
kill -SIGUSR1 $!
|
||||
|
|
@ -1,239 +0,0 @@
|
|||
// based on http://www.stroustrup.com/dc.c
|
||||
// by Bjarne Stroustrup
|
||||
// The desk calculator
|
||||
// pp 107-117, sec 6.1, A Desk calculator
|
||||
// No guarantees offered. Constructive comments to bs@research.att.com
|
||||
|
||||
|
||||
/*
|
||||
expr_list:
|
||||
expression PRINT // PRINT is \n
|
||||
expression PRINT expr_list
|
||||
|
||||
expression:
|
||||
expression + term
|
||||
expression - term
|
||||
term
|
||||
|
||||
term:
|
||||
term / power
|
||||
term * power
|
||||
power
|
||||
|
||||
power:
|
||||
power # primary // char '^' produces problems with terminal input
|
||||
primary
|
||||
|
||||
|
||||
primary:
|
||||
NUMBER
|
||||
( expression )
|
||||
|
||||
|
||||
while statt for
|
||||
erweitern um o,i oder HexaDezimal mit &, |, x,
|
||||
*/
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int no_of_errors; // note: default initialized to 0
|
||||
bool doPrintCalculations = true;
|
||||
|
||||
|
||||
unsigned int error(const char* s)
|
||||
{
|
||||
no_of_errors++;
|
||||
cerr << "error: " << s << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
enum Token_value {
|
||||
NUMBER, // '0' ... '9'
|
||||
PLUS='+', MINUS='-', MUL='*', DIV='/',
|
||||
PRINT='\n', POWER='#', LP='(', RP=')'
|
||||
};
|
||||
|
||||
Token_value global_token = PRINT;
|
||||
unsigned int number_value;
|
||||
|
||||
unsigned int asciToInt(string string_value){
|
||||
unsigned int result = 0;
|
||||
for(size_t i=0; i<string_value.length();i++){
|
||||
result *= 10;
|
||||
result += string_value[i] - 48;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Token_value get_token()
|
||||
{
|
||||
char ch;
|
||||
|
||||
do { // skip whitespace except '\n'
|
||||
if(!cin.get(ch)) {error("EOF"); exit(1);} //return curr_tok = END;
|
||||
} while (ch!='\n' && isspace(ch));
|
||||
|
||||
switch (ch) {
|
||||
//case ';':
|
||||
case '\n':
|
||||
return global_token=PRINT;
|
||||
case '*':
|
||||
case '/':
|
||||
case '+':
|
||||
case '-':
|
||||
case '(':
|
||||
case ')':
|
||||
case '#':
|
||||
return global_token=Token_value(ch);
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':{
|
||||
// the easy way: cin.putback(ch);
|
||||
// the easy way: cin >> number_value;
|
||||
string string_value;
|
||||
string_value = ch;
|
||||
while (cin.get(ch) && isdigit(ch))
|
||||
string_value += ch; // string_value.push_back(ch); // to work around library bug
|
||||
cin.putback(ch);
|
||||
number_value = asciToInt(string_value);
|
||||
return global_token=NUMBER;
|
||||
}
|
||||
default:
|
||||
error("bad token");
|
||||
return global_token=PRINT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned int expression(); // cannot do without (indirect recursion)
|
||||
|
||||
|
||||
unsigned int primary() // handle primaries
|
||||
{
|
||||
Token_value current_token = get_token();
|
||||
|
||||
switch (current_token) {
|
||||
case NUMBER:{
|
||||
unsigned int v = number_value;
|
||||
get_token(); // proceed global_token to next token (i.e. operator or '\n')
|
||||
return v;
|
||||
}
|
||||
case LP:{
|
||||
unsigned int e = expression();
|
||||
if (global_token != RP) return error(") expected");
|
||||
get_token(); // eat ')' in order to proceed global_token to next token
|
||||
return e;
|
||||
}
|
||||
default:
|
||||
return error("primary expected");
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int power() // 2 ^ 3
|
||||
{
|
||||
unsigned int left = primary();
|
||||
unsigned int right = 0;
|
||||
|
||||
for (;;)
|
||||
switch (global_token) {
|
||||
case POWER:{
|
||||
right = primary();
|
||||
if (doPrintCalculations) printf("%u # %u\n", left, right);
|
||||
unsigned int base = left;
|
||||
left = 1;
|
||||
for (int i=0; i<right; i++)
|
||||
left *= base;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return left;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned int term() // multiply or divide
|
||||
{
|
||||
unsigned int left = power();
|
||||
unsigned int right = 0;
|
||||
|
||||
for (;;)
|
||||
switch (global_token) {
|
||||
case MUL:{
|
||||
right = power();
|
||||
if (doPrintCalculations) printf("%u * %u\n", left, right);
|
||||
left *= right;
|
||||
break;
|
||||
}
|
||||
case DIV:{
|
||||
if ((right = power())) {
|
||||
if (doPrintCalculations) printf("%u / %u\n", left, right);
|
||||
left /= right;
|
||||
break;
|
||||
}
|
||||
return error("divide by 0");
|
||||
}
|
||||
default:
|
||||
return left;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
unsigned int ops_level_a() // e.g. add or subtract
|
||||
{
|
||||
unsigned int left = ops_level_b(); // e.g. multiply or divide (which in turn calls ops_level_c() immediately)
|
||||
unsigned int right = 0;
|
||||
|
||||
for (;;)
|
||||
switch (global_token) {
|
||||
case TOKEN_LEVELA_1:{
|
||||
right = ops_level_b(); // e.g. multiply or divide
|
||||
left = left TOKEN_LEVELA_1 right;
|
||||
break;
|
||||
}
|
||||
case TOKEN_LEVELA_2:{
|
||||
right = ops_level_b(); // e.g. multiply or divide
|
||||
left = left TOKEN_LEVELA_1 right;
|
||||
break;
|
||||
}
|
||||
default: // an operator of another (lower) level, or \n
|
||||
return left; // there is no further operator (and operands) of this level
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
unsigned int expression() // add or subtract
|
||||
{
|
||||
unsigned int left = term();
|
||||
unsigned int right = 0;
|
||||
|
||||
for (;;) // ``forever''
|
||||
switch (global_token) {
|
||||
case PLUS:{
|
||||
right = term();
|
||||
if (doPrintCalculations) printf("%u + %u\n", left, right);
|
||||
left += right;
|
||||
break;
|
||||
}
|
||||
case MINUS:{
|
||||
right = term();
|
||||
if (doPrintCalculations) printf("%u - %u\n", left, right);
|
||||
left -= right;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return left;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
while (cin){
|
||||
//cout << " > ";
|
||||
cout << expression() << '\n';
|
||||
}
|
||||
return no_of_errors;
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
#!/bin/bash
|
||||
clang++ -std=c++14 -o dc.out AP2-dc.cpp
|
|
@ -1,30 +1,20 @@
|
|||
// file main_04_UDEF_e.cpp
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
const int bitsPerOctet = 8;
|
||||
|
||||
struct BinaryOctet {
|
||||
public:
|
||||
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
|
||||
char bitsAsDigits[bitsPerOctet]{}; // bit values as chars
|
||||
BinaryOctet();
|
||||
BinaryOctet(int x);
|
||||
BinaryOctet(const BinaryOctet &) = default;
|
||||
BinaryOctet &operator=(int const &a);
|
||||
const BinaryOctet operator--(int);
|
||||
const BinaryOctet operator-(BinaryOctet a);
|
||||
const BinaryOctet operator+(BinaryOctet a);
|
||||
bool evenParity;
|
||||
char bitsAsDigits[bitsPerOctet]{};
|
||||
BinaryOctet(int x = 0);
|
||||
BinaryOctet(const BinaryOctet&) = default;
|
||||
BinaryOctet &operator=(int &a);
|
||||
BinaryOctet operator--(int);
|
||||
BinaryOctet operator-(BinaryOctet &a);
|
||||
BinaryOctet operator+(BinaryOctet a);
|
||||
BinaryOctet operator/(BinaryOctet &a);
|
||||
};
|
||||
|
||||
BinaryOctet::BinaryOctet() {
|
||||
for (char &bitsAsDigit : bitsAsDigits) {
|
||||
bitsAsDigit = '0';
|
||||
}
|
||||
evenParity = true;
|
||||
}
|
||||
|
||||
|
||||
BinaryOctet::BinaryOctet(int x) {
|
||||
for (char &bitsAsDigit : bitsAsDigits) {
|
||||
bitsAsDigit = '0';
|
||||
|
@ -43,41 +33,43 @@ BinaryOctet::BinaryOctet(int x) {
|
|||
}
|
||||
|
||||
evenParity = i % 2 == 0;
|
||||
//std::cout << evenParity << std::endl;
|
||||
}
|
||||
|
||||
|
||||
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
||||
if (a.evenParity != b.evenParity) return false;
|
||||
for (int i = 0; i < bitsPerOctet; ++i) {
|
||||
if (a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
||||
BinaryOctet &BinaryOctet::operator=(int &a) {
|
||||
std::string tmp = std::to_string(a);
|
||||
for (int i = 0; i >= tmp.length(); i++) {
|
||||
bitsAsDigits[i] = tmp[i];
|
||||
}
|
||||
return true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//TODO: this method
|
||||
const BinaryOctet BinaryOctet::operator--(const int) {
|
||||
return operator-(1);
|
||||
BinaryOctet BinaryOctet::operator--(int) {
|
||||
return operator-(*new BinaryOctet(1));
|
||||
}
|
||||
|
||||
//FIXME: returns wrong result
|
||||
//using complement method here
|
||||
const BinaryOctet BinaryOctet::operator-(BinaryOctet a) {
|
||||
BinaryOctet BinaryOctet::operator-(BinaryOctet &a) {
|
||||
BinaryOctet result;
|
||||
//reverse a
|
||||
for(int i = 0; i <= bitsPerOctet; i++) {
|
||||
a.bitsAsDigits[i] = a.bitsAsDigits[i] == '0' ? '1' : '0';
|
||||
bool over = false;
|
||||
for(int i = bitsPerOctet - 1; i >= 0; --i) {
|
||||
int tmp;
|
||||
int aI = a.bitsAsDigits[i] - '0';
|
||||
int bI = bitsAsDigits[i] - '0';
|
||||
|
||||
if(over) tmp = bI - aI - 1;
|
||||
else tmp = bI - aI;
|
||||
over = false;
|
||||
|
||||
if(tmp < 0) {
|
||||
over = true;
|
||||
tmp = tmp + 1;
|
||||
}
|
||||
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
|
||||
}
|
||||
a + 1;
|
||||
|
||||
result = *this + a;
|
||||
std::cout << result.evenParity << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
const BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
|
||||
BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
|
||||
BinaryOctet result;
|
||||
bool over = false;
|
||||
for (int i = bitsPerOctet - 1; i >= 0; --i) {
|
||||
|
@ -104,23 +96,22 @@ const BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
||||
BinaryOctet BinaryOctet::operator/(BinaryOctet &a) {
|
||||
BinaryOctet result;
|
||||
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
||||
std::string tmp = std::to_string(a);
|
||||
for (int i = 0; i >= tmp.length(); i++) {
|
||||
bitsAsDigits[i] = tmp[i];
|
||||
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
||||
if (a.evenParity != b.evenParity) return false;
|
||||
for (int i = 0; i < bitsPerOctet; ++i) {
|
||||
if (a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
||||
}
|
||||
return *this;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
|
||||
BinaryOctet result;
|
||||
for (; a != b; b--) {
|
||||
|
@ -152,7 +143,6 @@ std::string as_string(BinaryOctet *a) {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
// for std::cout
|
||||
std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
|
||||
os << as_string(toBePrinted);
|
||||
|
@ -163,7 +153,6 @@ std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
|
|||
std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
|
||||
os << as_string(toBePrinted);
|
||||
return os;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@ add_executable(01_ENV_Testat 01_ENV/Testat/main.c 01_ENV/Testat/func1.c)
|
|||
add_executable(02_MENT_MP 02_MENT/MP/main_02_MENT.cpp)
|
||||
add_executable(02_MENT_Testat 02_MENT/Testat/main_02_MENT.cpp)
|
||||
|
||||
add_executable(03_FLOW_MP 03_FLOW_a/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
|
||||
add_executable(03_FLOW_Testat 03_FLOW_a/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
|
||||
add_executable(03_FLOW_MP 03_FLOW/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
|
||||
add_executable(03_FLOW_Testat 03_FLOW/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
|
||||
|
||||
add_executable(04_UDEF_MP 04_UDEF/MP/main_04_UDEF_e.cpp)
|
||||
add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp)
|
||||
|
|
Loading…
Reference in New Issue