这是去年的一个填空题,当然,只要知道换算二进制就完全没问题的~ 1 #include<iostream> 2 #include<cstdio>
3 using namespace std; 4 5 void to2(int x) 6 { 7 int flage = 0; 8 flage = (x>0)?1:-1; 9 if(x == 0) flage = 0; 10 switch(flage) 11 { 12 case 0:cout<<" ";break; 13 case 1:{ // 正数二进制 14 int s[8] = {0}, e = 7; 15 while(x!=0) 16 { 17 s[e] = x%2; 18 x /= 2; 19 e--; 20 } 21 for(int i=0;i<8;i++){ 22 if(s[i] == 1) cout<<"*"; 23 else cout<<" "; 24 } 25 26 break; 27 } 28 case -1:{ // 负数二进制 29 x = -x; 30 int s[8] = {0}, e = 7; 31 while(x!=0) 32 { 33 // 取反码然后取补码 34 s[e] = ((x%2) == 1)?0:1; 35 x /= 2; 36 e--; 37 } 38 // 补码 39 int t = 1;
40 // 这个地方修改了
for(int i=7;i>=0;i--){
if(!t) break;
if(s[i]+t == 2) s[i] = 0, t = 1;
else s[i]++, t = 0;
}
50 for(int i=0;i<8;i++){ 51 if(s[i] == 1) cout<<"*"; 52 else cout<<" "; 53 } 54 55 break; 56 } 57 defualt:break; 58 } 59 } 60 61 void show(int a[][32]) 62 { 63 for(int i=0;i<10;i++){ 64 for(int j=0;j<32;j++){ 65 to2(a[i][j]); 66 if((j+1)%2 == 0) 67 cout<<endl; 68 } 69 cout<<"-------------------"<<endl; 70 } 71 } 72 73 int main() 74 { 75 FILE *fp; 76 int a[10][32] = {0}; 77 fp = fopen("test.txt", "r+"); 78 if(fp == NULL){ 79 // printf("NULL "); 80 return 0; 81 } 82 while(!feof(fp)) 83 { 84 for(int i=0;i<10;i++) 85 for(int j=0;j<32;j++){ 86 fscanf(fp, "%d", &a[i][j]); 87 } 88 } 89 fclose(fp); 90 /* 91 for(int i=0;i<10;i++){ 92 for(int j=0;j<32;j++){ 93 cout<<a[i][j]<<" "; 94 } 95 cout<<endl; 96 } 97 */ 98 99 /*转换成8位二进制*/ 100 101 show(a); 102 // 九的九次方等于多少...... 103 getchar(); 104 return 0; 105 }
下面是数据:
1 4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 2 16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16 3 4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 4 0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4 5 4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64 6 16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128 7 0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0 8 2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0 9 1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0 10 0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0