想要更好的版本 [英] want a better version of this

查看:66
本文介绍了想要更好的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我使用以下更好的版本

函数,它们只将十进制整数转换为相应的

二进制形式。我现在拥有的问题是我不知道如何处理0



作为参数传递在一个函数中,我的代码必须添加

另外一个函数来处理这种情况,任何帮助都表示赞赏,

thanx。

char * Dec2Bin(const int decimal){


char * binary = new char [64];

int dividend,i;


dividend = decimal;

for(i = 0; dividend!= 0; ++ i){

binary [i] =(dividend& 1)+ '' 0 ''; //(a%b)==(a&(b-1))

被除数>> = 1;

}

binary [i] =''\''';


返回strrev(二进制);

}


char * Decimal2Binary(const int decimal){

return(decimal == 0)?" 0":Dec2Bin(decimal);

}

解决方案

在''comp.lang.c''中, ru ** **@sohu.com (sugaray)写道:

char * binary = new char [64];




C中没有新。请重新发布到相应的新闻组。看来

C不是你的语言。


如果你坚持用C编写你的代码,请更改它以便它使用C

功能和说明。


-

-ed- em ********** @ noos.fr [在回答我之前删除YOURBRA]

C语言常见问题: http://www.eskimo.com/~scs/C- faq / top.html

C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp

FAQ de fclc: http://www.isty-info.uvsq.fr/~rumeau/fclc/


Emmanuel Delahaye写道:

在''comp.lang.c'', ru **** @ sohu.com (糖ay)写道:

char * binary = new char [64];



C中没有''new'' / blockquote>


这是一个完全有效的标识符名称(虽然在你引用的代码中以一种相当奇怪的和/ b
语法错误的方式使用)。事实上,我经常在代码中使用它,例如:


FOO * prefix_CreateFoo(args ...)

{

FOO * new = malloc(sizeof * new);

if(new!= NULL)

{

...设置有效的FOO实例...

}

返回新的;

}


-

Richard Heathfield: bi****@eton.powernet.co .uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


sugaray写道:


有人可以帮我解决以下更好的版本以下仅转换十进制整数的函数它是相应的二进制形式。我现在遇到的问题是,当我只在一个函数中作为参数传递0
时,我无法弄清楚如何处理,我的代码必须添加<另外还有一个功能来处理这种情况,感谢任何帮助,


char * Dec2Bin(const int decimal){



/ * BEGIN bitstr.c * /


#include< stdio.h>

#include< limits.h>


#define E_TYPE浮动

#define STRING" %s =%f \ n"


typedef E_TYPE e_type;


char * bitstr(char *,void const *,size_t) ;


int main(无效)

{

e_type e,d;

char ebits [CHAR_BIT * sizeof e + 1],* s;


s = STRING;

for(e = 0.2f; 0.75> e; e + = 0.125){

bitstr(ebits,& e,sizeof e);

printf(s,ebits,e);

}

for(d = 2; 20000> d; d * = 2){

for(e = d - 1; 0.75> e - d; e + = 0.5){

bitstr(ebits,& e,sizeof e);

printf(s,ebits,e);

}

}

返回0;

}


char * bitstr(char * str,const void * obj,size_t n)

{

unsigned char mask;

const unsigned char * byte = obj;

char * const ptr = str;


while(n--!= 0){

mask =((unsigned char)-1>> ; 1)+ 1;

do {

* str ++ =(char)(mask& amp; ; byte [n]? ''1'':''0'');

mask>> = 1;

} while(mask!= 0);

}

* str =''\ 0'';

返回ptr;

}


/ * END bitstr.c * /

-

pete


Hi, can somebody help me out with a better version of the following
functions which only convert decimal integer to it''s corresponding
binary form. the
problem i''m having now is that I can''t figure out how to handle when 0
is
passed as parameter in only one function, my code below have to add
one more function to handle this situation, any help is appreciated,
thanx.
char *Dec2Bin(const int decimal) {

char *binary=new char[64];
int dividend,i;

dividend=decimal;
for(i=0;dividend!=0;++i) {
binary[i]=(dividend&1)+''0''; // (a % b) == (a & (b-1))
dividend>>=1;
}
binary[i]=''\0'';

return strrev(binary);
}

char *Decimal2Binary(const int decimal) {
return (decimal==0)?"0":Dec2Bin(decimal);
}

解决方案

In ''comp.lang.c'', ru****@sohu.com (sugaray) wrote:

char *binary=new char[64];



There is no ''new'' in C. Please repost to the appropriate newsgroup. It seems
that C is not your langage.

If you insist in writing your code in C, please change it so that it uses C
functions and instructions.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/


Emmanuel Delahaye wrote:

In ''comp.lang.c'', ru****@sohu.com (sugaray) wrote:

char *binary=new char[64];



There is no ''new'' in C.



It''s a perfectly valid identifier name (albeit used in a rather strange and
syntactically erroneous manner in the code you quote here). In fact, I
often use it myself in code such as:

FOO *prefix_CreateFoo(args...)
{
FOO *new = malloc(sizeof *new);
if(new != NULL)
{
...set up valid FOO instance...
}
return new;
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


sugaray wrote:


Hi, can somebody help me out with a better version of the following
functions which only convert decimal integer to it''s corresponding
binary form. the
problem i''m having now is that I can''t figure out how to handle when 0
is
passed as parameter in only one function, my code below have to add
one more function to handle this situation, any help is appreciated,
thanx.

char *Dec2Bin(const int decimal) {



/* BEGIN bitstr.c */

#include <stdio.h>
#include <limits.h>

#define E_TYPE float
#define STRING " %s = %f\n"

typedef E_TYPE e_type;

char *bitstr(char *, void const *, size_t);

int main(void)
{
e_type e, d;
char ebits[CHAR_BIT * sizeof e + 1], *s;

s = STRING;
for (e = 0.2f; 0.75 > e; e += 0.125) {
bitstr(ebits, &e, sizeof e);
printf(s, ebits, e);
}
for (d = 2; 20000 > d; d *= 2) {
for (e = d - 1; 0.75 > e - d; e += 0.5) {
bitstr(ebits, &e, sizeof e);
printf(s, ebits, e);
}
}
return 0;
}

char *bitstr(char *str, const void *obj, size_t n)
{
unsigned char mask;
const unsigned char *byte = obj;
char *const ptr = str;

while (n-- != 0) {
mask = ((unsigned char)-1 >> 1) + 1;
do {
*str++ = (char)(mask & byte[n] ? ''1'' : ''0'');
mask >>= 1;
} while (mask != 0);
}
*str = ''\0'';
return ptr;
}

/* END bitstr.c */
--
pete


这篇关于想要更好的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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