想转换基数10没有。进入二进制文件,我的代码有什么问题..它运行不正常。 [英] Want to convert base 10 no. Into binary, , what is wrong in my code.. It dont run properly.

查看:55
本文介绍了想转换基数10没有。进入二进制文件,我的代码有什么问题..它运行不正常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *您好,它刚刚在C编程世界中度过了一周。我试图构建一个

程序将base10转换为二进制(只有7个位置)。

但是我没有得到输出..每件事看起来都很完美。

还有一件事。我不想直接解决这个问题。如果需要,我会

已经在书中或在线阅读。我想自己解决它。告诉我

我在哪里弄错了。在算术上,每个人看起来都很具体。 * /

  #include   <   iostream.h  >  
#include < stdio.h >
#include < conio.h >
void main()
{
int in ,b [ 6 ],日志;
clrscr();
cout<< 输入一个基数为10的数字:;
cin>> in;
for int i = 64 ; i> 0; i = i / 2)
{
if (in> = i&& in< 128)
{
/ * 我想使用LOG基础2功能,
但是我不知道该怎么做。
所以我尝试了这个冗长的LOGS东西* /

如果(i == 64
logs = 6 ;
else if (i == 32
logs = 5 ;
else if (i == 16
logs = 4 ;
else if (i == 8
logs = 3 ;
else if (i == 4
logs = 2 ;
else if (i == 2
logs = 1 ;
else if (i == 1
logs = 0 ;
b [logs] = 1 ;
in = in%i;
}
else if (in< = i)
{
if (i == 64
logs = 6 ;
else if (i == 32
logs = 5 ;
else if (i == 16
logs = 4 ;
else if (i == 8
logs = 3 ;
else if (i == 4
logs = 2 ;
else if (i == 2
logs = 1 ;
else if (i == 1
logs = 0 ;
b [logs] = 0 ;
}
}
cout<< endl;
for int z = 0 ; z< = 6 ; z = z ++)
{
printf( %d,b [i]);
}
getch();
}





我的尝试:



消除所有可能的错误..

即使我可以在线搜索代码,但我不想复制。请告诉我这个代码有什么不对。Alogorithamically everythhing对我来说很好。

(这个C世界只有一个星期)

解决方案

不要使用日志:有一种更简单的方法。

当你将十进制数字读成一个整数时,它已经是二进制数 - 因为这就是计算机存储信息的地方.C / C ++提供了几个允许您直接访问和操作二进制值的运算符:AND'&',左移'>>',右移'<<'

如果设置一个循环来为每个位运行 - 在你的情况下,一个简单的for循环0到6将覆盖所有7位 - 并使用AND来提取最高位:

< pre lang =c ++> bit = value & 128 ;

因为128只设置了一个位(在第七位位置,它的二进制值为1000000)并且使用您的值对其进行清除所有其他位。

然后,您可以将值中的位移到左侧的下一个循环:

  value  =  value <<  1 ; 

如果位为0,那么值的最高位也是如此,如果它是128则那么最高位是1。因此打印变得简单:

 printf( % c,位==  128 '  1''  0'); 





 cout<< bit ==  128 '  1''  0'; 


有一种简单的方法:

你有一个值 N 基数10转换为基数2.

N1 基数10将 ABCDEFGH ,基数为2的数字。

单位数 H = N1 模2和 N2 = N1 / 2(整数除法)。

N2 基数10将是 ABCDEFG ,基数为2的数字。

单位数 G = N2 modulo 2和 N3 = N2 / 2(整数除法)。

N3 基数10将是 ABCDEF ,基数为2的数字。

这是一个递归解决方案。



有一个工具可以让你看到代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


/* Hello, Its just been a week in C programming world. I tried to built a
program to convert base10 to binary (Only upto 7 positions).
But am not getting output.. Every thing seems perfect.
And one more thing. I dont want direct solution for this. If needed so I would
have read it in book or online. I want to solve it on my own. Just tell me
where am I getting it wrong. Alogorithmically everythings looks concrete to me. */

#include<iostream.h>
#include<stdio.h>
#include <conio.h>
void main()
{
int in,b[6],logs;
clrscr();
cout<<"Input a base 10 number:";
cin>>in;
for(int i=64; i>0;i=i/2)
{
if(in>=i && in<128)
{
/* I wanted to use LOG to base 2 function,
but I dont know how to.
So instead I tried this lengthy LOGS thing */
if(i==64)
logs=6;
else if(i==32)
logs=5;
else if(i==16)
logs=4;
else if(i==8)
logs=3;
else if(i==4)
logs=2;
else if(i==2)
logs=1;
else if(i==1)
logs=0;
b[logs]=1;
in=in%i;
}
else if(in<=i)
{
if(i==64)
logs=6;
else if(i==32)
logs=5;
else if(i==16)
logs=4;
else if(i==8)
logs=3;
else if(i==4)
logs=2;
else if(i==2)
logs=1;
else if(i==1)
logs=0;
b[logs]=0;
}
}
cout<<endl;
for(int z=0; z<=6;z=z++)
{
printf("%d",b[i]);
}
getch();
}



What I have tried:

All possible errors are eliminated ..
Even I could search code online but , I dont want to copy. Just please tell me whats wrong in this code.. Alogorithamically everythhing looks fine to me.
( Am just a week old in this C World )

解决方案

Don't use logs: there is a much simpler way.
When you read an base ten number into an integer, it is already in binary - because that is what computers store information in. And C / C++ provide a couple of operators that allow you to access and manipulate that binary value directly: AND '&', Shift left '>>', and Shift right '<<'
If you set up a loop to run for each bit - in your case a simple for loop 0 to 6 will cover all 7 bits - and use AND to extract the top bit:

bit = value & 128;

Because 128 has only one bit set (in the seventh bit position, it's binary value is 1000000) ANDing it with your value clears all other bits.
You can then shift the bits in your value ready for the next loop with shift left:

value = value << 1;

If bit is 0, then so was the top bit of the value, if it is 128 then that top bit was one. So printing it becomes simple:

printf("%c", bit == 128 ? '1' : '0');


Or

cout << bit == 128 ? '1' : '0';


There is a simple way:
You have a value N base 10 to convert to base 2.
N1 base 10 will be ABCDEFGH, the digits in base 2.
The digit of units H = N1 modulo 2 and N2 = N1 / 2 (integer division).
N2 base 10 will be ABCDEFG, the digits in base 2.
The digit of units G = N2 modulo 2 and N3 = N2 / 2 (integer division).
N3 base 10 will be ABCDEF, the digits in base 2.
This is a recursive solution.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于想转换基数10没有。进入二进制文件,我的代码有什么问题..它运行不正常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆