-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLibConverter.c
More file actions
103 lines (78 loc) · 2.27 KB
/
Copy pathMyLibConverter.c
File metadata and controls
103 lines (78 loc) · 2.27 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
#include "MyLibConverter.h"
char str[100]; char to, from; int num = 0; char strbin[100];int sayi; float fsayi;
int FromDecimal(float sayi) {
if (to == 'B'){
int tam = (int)sayi;
float kesir = sayi - tam;
if (to == 'D') {
if (kesir >= 0.5)
printf("%d", (tam + 1));
else if (kesir < 0.5)
printf("%d", tam);
}
else if (to == 'B') {
_itoa(tam, str, 2);
printf("%s", str);
printf(".");
for (int j = 0;j < 8;j++) {
kesir *= 2;
int intkesir = (int)kesir;
float kesirkesir = kesir - intkesir;
if (intkesir == 1)
printf("1");
else printf("0");
kesir = kesirkesir;
}
}
else printf("not valid \"to\"");
return 0;
}
else if (to == 'O'){
_itoa(sayi, str, 8); puts(str);
}
else if (to == 'H'){
_itoa(sayi, str, 16); puts(str);
}
else printf("not valid \"to\"");
return 0;
}
int FromBinary(int sayi) {
int sayi2 = sayi; int j = 0; int num1 = 0;
while (sayi2 > 0) {
sayi2 /= 10;
j++;
}
for (int i = 0;i < j;i++) {
if (sayi % 10)
num1 += (int)pow(2, i);
sayi /= 10;
}
if (to == 'O') {
_itoa(num1, str, 8); puts(str);
}
else if (to == 'D')
printf("%d", num1);
else if (to == 'H') {
_itoa(num1, str, 16); puts(str);
}
else printf("not valid \"to\"");
return 0;
}
int FromHexadecimal() {
unsigned int number;
scanf_s("%x", &number);
if (to == 'D') printf("%d\n", number);
else if (to == 'B') { _itoa(number, str, 2); puts(str); }
else if (to == 'O') { _itoa(number, str, 8); puts(str); }
else printf("not valid \"to\"");
return 0;
}
int FromOctal() {
int number;
scanf_s("%o", &number);
if (to == 'D') printf("%d\n", number);
else if (to == 'B') { _itoa(number, str, 2); puts(str); }
else if (to == 'H') { _itoa(number, str, 16); puts(str); }
else printf("not valid \"to\"");
return 0;
}