-
Notifications
You must be signed in to change notification settings - Fork 0
/
二进制.c
78 lines (63 loc) · 991 Bytes
/
二进制.c
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
// 这是原始阶段, 我的第一想法;
#include <iostream.h>
#include <fstream.h>
#include <cmath>
void writefile(char* str)
{
ofstream out1;
out1.open("modi2.txt", ios::binary|ios::app);
for (int i = 0; str[i] != 0; i++)
{
out1.put(str[i]);
}
out1.close();
}
void fun(int x)
{
cout<<"请输入一个整数:";
cin>>x;
int a[1024];
int i = 0;
while (x > 0)
{
a[i++] = x % 2;
x = x / 2;
}
int len = i;
char b[1024];
for (int j = 0; j < len; j++, i--)
{
b[j] = a[i-1] + '0';
cout<<b[j];
}
b[j] = 0;
writefile(b);
}
void clearfile()
{
ofstream out1;
out1.open("modi2.txt");
out1.close();
}
int main()
{
clearfile();
fun(13);
return 0;
}
// 下面程序显示了指针的妙用。
char str[32];
int i;
for (i = 30; i >= 0; i--)
{
if (x % 2)
str[i] = '1'; // str[i] = (x%2) + '0';
else
str[i] = '0';
x = x / 2;
if (!x)
break;
}
str[31] = '\0';
cout<<&str[i]<<endl;
writefile(&str[i]);