嗨...需要帮助进行十进制到二进制转换 [英] Hi...need help with Decimal to Binary Conversion

查看:82
本文介绍了嗨...需要帮助进行十进制到二进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我对C ++相对较新,但我想在其中变得相当出色
而不仅仅是为了通过一些主题.

因此,现在每天我都会改善我的程序,以尝试学习更多而不是不了解什么
我已经学过了.

因此,现在我正在执行从十进制到二进制的转换,但是我没有使用任何预先创建的库"或类",否则我觉得我还没有真正做任何事情.

我已经有一段代码可以对点" xxx''.xxx"前面的任何内容进行二进制处理,但是
现在我正在处理点后面的部分.

所以我拿出我的数字书,并检查了如何用2步相乘来进行乘法并进行编码,以使其在某些分数上能很好地工作.
HowEver的使用最终会导致无限循环分配,因为每次我得到0.60并将其计算出来时,我沿着该行的某个地方最终都以0.60结束,从而结束了无限循环.

我不知道如何让程序感知im创建的字符串中的重复.

改进的问题在这里.
我的问题是...要使程序意识到运行和无止境循环的一种巧妙方法是什么?
有什么办法可以使我感觉到它正在解决某些已经解决的问题,并且应该停止吗?

这是到目前为止的代码.


Hello

Im relatively new to C++ but i want to become quite good in it
and not just do it to pass some subject.

So now everyday im improving a piece of my program to try and learn more and not unlearn what
I have already learned.

So now im making a Decimal to Binary conversion but im not using any pre made "Libraries" or "Classes" otherwise I feel like I havent really done anything.

I already have a piece of code that works out the Binary for anything INfront of a dot " ''xxx''.xxx " but
now im working on the part after the dot.

So I took out my Digital book and checked up how to do it with the multiplication by 2 move and coded it so it works on some fractions quite well.
HowEver using that ends up in an infinite loop allot of the times because every time I get something like 0.60 and work it out I somewhere along the line end up with 0.60 again thus ending in an infinite loop.

I have no idea how to let the program sense a repetition in the string that im creating.

Improved Question here.
My Question is...What would be a neat method of making the program realize hes running into and Endless Loop?
Is there some way I can make it sense that its working out something that it already worked out and that it should stop?

Here is the Code so far.


#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
double a;

double fracvalue(double x) 
{
 int z;            
       z = (x/1);
       a = x-z;
        return (a);
       }




int main(int argc, char** argv) {
    double x;
    int z, c;    
    cin >> x;
    double a =fracvalue(x)*2;
    cout << fracvalue(x) << "      " << a  << endl;//Checking if A and fracvalue are giving correct readings 
    string Bin = ".";
    
    for (int = i=;i < 20;i++)  //<<-- THIS was while(a != 0.0) but it never seemed to 
                      //stop even when it gets to 0.0 also it doesnt write out the complete binary this way
                      //sometimes only leaving dots or partial BIN numbers.
    {
          if (a > 1)
          {
                Bin = Bin +'1';
                a = a-1;
                cout << a << endl;
                a = a*2;
                cout << a << endl;}
                
             if (a < 1)
                {
                    Bin = Bin + '0';
                    a = a*2;
                    
                    }
                    cout << Bin << endl;
          }
          
          
          
    system("PAUSE");
    return 0;
}




由于某些原因,它一直在抱怨这条线
z =(x/1);在函数fracvalue中,有趣的是我没有碰过这段代码:s它只是不想再运行了.

无论如何...如果我插入较大的分数,似乎可以给出正确的Binary值,但是简单的分数只会出现0或1s而不是两者.
但是,较大的永远(a!= 0.0)"将永远不会结束,因为它本身会无限循环.

谢谢




For some reason it keeps on moaning about the line
z = (x/1); in the function fracvalue,the funny thing is I didnt touch this code :s it just didnt want to run anymore.

Anyway...It seems to give the right Binary if i insert large fractions but simple ones would come out only 0s or 1s and not both.
Larger ones however "Under while (a != 0.0)" will never end because it math itself loops infinitely.

Thanks

推荐答案



您可以将小数转换为二进制,如下所示,
40.25

对于40,等效二进制为101000.

对于分数部分,只需使用以下逻辑

0.25 x 2 = 0.5
0.5 x 2 = 1.0

从答案中取整数部分.

所以最终答案将是(101000.01)

问候,
Satheesh
Hi,

you can convert a decimal to a binary as follows ,
40.25

for 40 the equivalent binary is 101000.

for the fraction part just use the following logic

0.25 x 2 = 0.5
0.5 x 2 = 1.0

from the answer take the integer part..

so the final answer will be (101000.01)

Regards,
Satheesh


嗯,无限循环的大小取决于插入的值.
BTW

我在MAIN函数的2个iF之间做了一个ELSE语句.
Well the endless loops all come in different sizes depending on what value insert.
BTW

I made an ELSE statement between the 2 iFs in the MAIN function.


自1966年我参加第一门计算机科学课程以来,处理浮点数一直是一个危险信号.

切勿测试两个浮点数的精确相等性,尤其是如果其中一个被计算出的话

仅当计算值"a"恰好等于"0.0"时,原始循环才结束.那是一个危险信号.浮动操作始终会留下一些累积的小数部分.

此代码可能还有其他问题,我没有尝试调试它,仅对循环结束条件"进行了评论

--------------------------
好的,我运行了您的程序并得到了这个奇怪的输出.我不知道该程序应该做什么,也不知道它有什么问题或期望得到什么输出.对我来说根本没有意义.

Since my 1st computer science class in 1966, there has always been a red flag in dealing with floating point numbers.

Never test for exact equality of two floating point numbers, especially if one of them is computed

Your original loop only ended when the computed value of "a" was precisely equal to "0.0". That''s a red flag. Floating operations always leave some cumulative fractional part laying around.

There may be other things wrong with this code, I didn''t try to debug it, only comment on the "loop ending condition"

--------------------------
OK, I ran your program and got this strange output. I have no idea what this program is supposed to do and have no idea what''s wrong with it or what output you expect. It makes no sense to me at all.

123.456
0.456      0.912
.0
0.824
1.648
.01
0.648
1.296
.011
0.296
0.592
.01110
0.184
0.368
.0111010
.01110100
0.472
0.944
.0111010010
0.888
1.776
.01110100101
0.776
1.552
.011101001011
0.552
1.104
.0111010010111
0.104
0.208
.011101001011110
.0111010010111100
.01110100101111000
0.664
1.328
.011101001011110001
0.328
0.656
.01110100101111000110
0.312
0.624
.0111010010111100011010
0.248
0.496
.011101001011110001101010
.0111010010111100011010100
0.984
1.968
.01110100101111000110101001


这篇关于嗨...需要帮助进行十进制到二进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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