用C ++写了一个二进制转换器程序,是不是很好? [英] Wrote a number to binary converter program in C++ , is it good?

查看:52
本文介绍了用C ++写了一个二进制转换器程序,是不是很好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是C ++的新手(大约3或4个月前开始。我发布了一个以前的代码但做了很多改动,直到现在才发布新代码。我总是知道会有改进的空间所以请我非常积极地评价,因为我一直在寻找改进。



这是:



Hello, i'm fairly new to C++ (started about 3 or 4 months ago. I posted a previous code but made many changes and haven't came around to posting the new code until now. I always know there will be room for improvement so please judge very aggressively with me as I am always looking for improvements.

Here it is:

#include <iostream>
void recur(int convert)
{
    if(convert == 0) //if input is 0 , return nothing.
    {
            return;
    }
        recur(convert/2); // divide convert by 2, get only a 1 or 0
std::cout<<convert % 2; //write remainder
}


void send(int convert)
{
    while (convert != 0)
    {//while convert is not equal to the value we initialized
    std::cin.ignore();//clears previous input before taking new input
    }
    std::cin>>convert;
recur(convert);//call recursive function to do the conversion for us
}


int main()
{
int convert = 0; //this is to be converted, in the meantime it must be initialized to a value
    while (convert == 0)
    { //this while gives us a loop for a immediate other input
send(0);
std::cout<<""<<std::endl;
    }
}

推荐答案

不,一点都不好。首先,它不返回任何内容,甚至不允许用户将数据放入字符串中。其次,与最简单的合理实施相比,实施效率太低。最后,函数可以是通用的,否则如何重用 long int unsigned int 等代码?你可以使用C ++模板。



首先,你需要有一个功能配置文件(让我们暂时忘记模板)。

No, not good at all. First of all, it does not return anything, does not even allow the user to put data in a string. Second of all, the implementation is way too inefficient, compared even to the simplest reasonable implementation. Finally, the function could be generic, otherwise how would yo reuse the code for long int, unsigned int, and so on? You could use a C++ template.

So first, you need to have a function profile like (let's forget templates for now).
std::string ToBinary(int value);



或者,只使用以C结尾的空终止字符串:


or, to use just the null-terminated strings, in C style:

void ToBinary(char* string, int value);



在第二种情况下,用户需要负责提供对字符串的引用,该字符串为所有提供足够的空间二进制数字,根据数据的大小。



最后,学习从数据中提取单独位的配方。

首先,使用二进制'<<'和'&'运营商。如果你需要提取第N个字节,其中N是该位的整数索引,从最低有效位#0(最右边)到最高有效位计数,首先得到掩码等于 1<< Ñ。如果你随后使用你的值和这个掩码,你将得到0值:


In the second case, the user needs to be held responsible for providing a reference to a string which provides enough room for all the binary digits, according to the size of data.

Finally, learn a recipe for extracting separate bits form data.
First, use binary '<<' and '&' operators. If you need to extract N-th byte, where N is the integer index of the bit, counted from least-significant bit #0 (rightmost) toward a most-significant bit, first get the mask equals to 1 << N. If you then AND this mask with your value, you will get either 0 value or not:

if (value & (1 << N) == 0)
   // the extracted Nth bit is 0
else
   // the extracted Nth bit is 1

(把真实的代码放在我的评论上。)

使用这些想法,请写下整个实现。



另请参阅:

http://www.cplusplus.com / reference / string / string [ ^ ] ,

http://www.learn cpp.com/cpp-tutorial/38-bitwise-operators [ ^ ]。



-SA

(Put real code instead of my comments.)
Using these ideas, please write the whole implementation.

See also:
http://www.cplusplus.com/reference/string/string[^],
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators[^].

—SA


numbers_game写道:
numbers_game wrote:

还要回到主题,当你写 void ToBinary(char * string,int value); 只是优先编写 char * 字符串而不是char * string ?并且 char * string表示字符串的指针我是否正确?为什么我需要在这里使用指针?

And also to go back into topic, when you wrote "void ToBinary(char* string, int value);" is it just a matter of preference to write char* string instead of char *string? And char* string means pointer of a string am I correct? Why would I ever need to use a pointer for in this?

让我们看看...



声明 char * string char * string char * string 在此内容中是相同的,但如果您需要键入,例如在 typedef 中,它将是 char * ,这使得事物符合逻辑。由于变量或成员声明语法是 typeName variableName ,因此使用指针声明的含义是(char *)变量。 (括号是多余的,但我把它们用来表示构造的含义。



但这不是那么简单,因为它也可以写:

Let's see…

The declarations char *string, char * string and char* string are identical in this content, but if you need to have type along, for example in typedef, it will be char*, which makes thing logical. As the variable or member declaration syntax is "typeName variableName", the meaning of declaration with pointer is (char *) variable. (Brackets are redundant, but I put them to show what's the meaning of the construct.

But this is not so trivial, because it's also possible to write:

char *stringValue, characterValue, *anotherStringValue;



,相当于


which is equivalent to

char* stringValue;
char characterValue;
char* anotherStringValie;





你能来吗看点了吗?



无论如何,我强烈建议从一开始到最后阅读一些教科书,最好在你去的时候从书中练习。 br $> b $ b

对于在线教程,我推荐这个: http://www.learncpp.com [ ^ ]。



它也是很高兴通过 Bjarne Stroustrup 阅读这些书籍,这些并不是最容易理解的,但是他们可以给你关于C ++基本原理的想法。



-SA


这篇关于用C ++写了一个二进制转换器程序,是不是很好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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