-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbersystemexercices.c
More file actions
115 lines (99 loc) · 2.01 KB
/
numbersystemexercices.c
File metadata and controls
115 lines (99 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#include <time.h>
void showBinaryI(int z){
if(z>>1){
showBinaryI(z>>1);
printf("%d", (z&1) > 0 ? 1 : 0);
}else{
printf("%d", (z&1) > 0 ? 1 : 0);
}
}
void showBinaryC(char z){
int l = sizeof(z)*8;
for (int i = l-1; i >= 0; --i)
{
int bit = (z>>i)&1;
printf("%d", bit);
}
}
void showBinaryF(float f){
char* cp = (char*)&f;
for (int i = sizeof(f)-1; i >= 0; --i)
{
showBinaryC(*(cp+i));
printf("");
}
}
void showBinaryD(double f){
char* cp = (char*)&f;
for (int i = sizeof(f)-1; i >= 0; --i)
{
showBinaryC(*(cp+i));
printf("");
}
}
float randKomma(){
int max = 3;
time_t t;
time(&t);
srand((unsigned int)t);
float kommaVals[] = {0.5, 0.25, 0.125, 0.0625};
float sum = 0;
for (int i = 0; i < 4; ++i)
{
int r = (rand() % (max+1));
if(r==2){
sum += kommaVals[i];
}
}
return sum;
}
float randI(int max){
// int max = 2000;
time_t t;
time(&t);
srand((unsigned int)t);
return rand() % (max+1);
}
void sampleRandToTen(){
time_t t;
time(&t);
srand((unsigned int)t);
int base = (rand() % 4)+2;
int secondBase[] = {2, 8, 10, 16};
printf("Umwandeln der Zahl ");
for (int i = 0; i < rand() % (5+2); ++i)
{
int digit = rand() % (base);
printf("%d", digit);
}
printf(" der Basis %d zur Basis %d\n", base, secondBase[rand() % (4)]);
}
int main(){
printf("\n");
printf("Aufgabe 1.\n");
sampleRandToTen();
// printf("\n");
// printf("Du entscheidest bei jeder folgenden Aufgabe ob du die obere in die Untere umwandelst, oder anders herum.\n");
printf("\n\n\n");
printf("Aufgabe 2. Binaer <-> Dezimal.\n");
int dez = randI(1000);
printf("%d\n", dez);
showBinaryI(dez);
printf("\n\n\n");
char s = randI(100);
if(randI(1) == 0){
s = -s;
}
printf("Aufgabe 3. Zweierkomplement (8bit) der Zahl\n%d\n", s);
showBinaryC(s);
printf("\n\n");
float randIEEE = randI(1000)+randKomma();
printf("Aufgabe 4. IEE754\nDez %.4f\nBin ", randIEEE);
showBinaryF(randIEEE);
// zahlenSystemUmrechnen(2, 6);
printf("\n");
return 0;
}