更好的方法来编程吗? [英] Better way to program this?

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

问题描述

嗨。


(发布到这两个新闻组,因为我不确定哪个适合这个问题的

或者给定语言的具体程度如何?它是

是。如果其中一个不合适,就不要回复它。)


我正在制作一个bignum套餐在我有一个程序中使用(这是一个与你可能听说过的pi程序不同的东西)。

包将支持操纵长浮点数。


我现在设置的方式是有一个原始数据包。 unsigned

整数包,从中构建浮点包。

of。我的问题是关于编写较低级别的包。


请参阅,我这里有一个例程,它将两个无符号整数相加,但它也是
必须能够添加一个从一个点开始的一个数字

的数据,从另一个点开始的另一个

点的数字它的数据,最后在结果中存储了如此多的数字

数据缓冲区中的另一个数字保存结果。


这是什么我有(它用C ++,而不是C,顺便说一下)。问题是

它看起来不像是整洁或漂亮。一段代码。任何人

知道更好的方法吗?:


/ *添加两个RawInts。

*参数:

* a:第一个RawInt

* b:第二个RawInt

* rOrigin:此处存储结果的数字偏移量

* rLength:此存储区域的长度结果为

* aOrigin:a中的数字偏移量开始添加

* aLength:a中的区域长度添加

* bOrigin:b中的数字偏移量开始添加

* bLength:b中要添加的区域长度

*

*返回:随身携带。

*

*操作:* this = a + b。

*

*使用?Origin = 0和?Length = -1进行简单整数添加

*操作。

* /

DIGIT RawInt :: rawAdd(const RawInt& a,

const RawInt& b,

int rOrigin,int rLength,

int aOrigin,int aLength,

int bOrigin,int bLength)

{

int i;

DIGIT tmp,carry = 0;

int rlen2,alen2,blen2;

std :: vector< DIGIT> :: iterator di,de;

std :: vector< DIGIT> :: const_iterator ai,ae;

std :: vector< DIGIT> :: const_iterator bi,be;


/ *确保我们不要超过边界

*的数字数组。

* /

if(rLength!= -1)

{

rlen2 = rLength;

if(rlen2(length - rOrigin))

rlen2 =(length - rOrigin);

} else {

rlen2 =(length - rOrigin);

}


if(aLength!= -1)

{

alen2 = aLength;

if(alen2(a.length - aOrigin))

alen2 =(a.length - aOrigin);

}否则{

alen2 =(a.length - aOrigin);

}


if(bLength! = -1)

{

blen2 = bLength;

if(blen2(b.length - bOrigin))

blen2 =(b.length - bOrigin) ;

}否则{

blen2 =(b.length - bOrigin);

}


if(rOrigin + alen2> = length)

alen2 = length-rOrigin;

if(rOrigin + blen2> = length)

blen2 = length-rOrigin;


if(alen2 rlen2)alen2 = rlen2;

if(blen2 rlen2)blen2 = rlen2;


if(alen2< 0)alen2 = 0;

if(blen2< 0)blen2 = 0;


/ *设置迭代器* /

di = digits.begin()+ rOrigin; de = digits.end();

ai = a.digits.begin()+ aOrigin; ae = a.digits.end();

bi = b.digits.begin()+ bOrigin; be = b.digits.end();


/ *现在添加* /

if(alen2> = blen2)

{

/ *案例1:a至少和b * /
一样长

/ *加一个's'和b ('= 0; i< blen2; i ++,++ di,++ ai,++ bi)

{

tmp = * ai + * bi + carry;

if(carry)carry =(tmp< = * bi)? 1:0;

else carry =(tmp< * bi)? 1:0;

* di = tmp;

}


/ *现在解决一个比b * /

for(i = blen2; i< alen2; i ++,++ di,++ ai)

{

tmp = * ai + carry;

if(carry)

carry =(tmp == 0)? 1:0;

* di = tmp;

}


/ *将其余部分归零。 * /

for(i = alen2; i< rlen2; i ++,++ di)

{

* di = carry;

carry = 0;

}

}否则{

/ *案例2:b长于* /


/ *加上'和b'的数字* /

for(i = 0; i< alen2; i ++,++ di,++ ai,++ bi)

{

tmp = * ai + * bi + carry;

if(carry)carry =(tmp< = * bi)? 1:0;

else carry =(tmp< * bi)? 1:0;

* di = tmp;

}


/ *现在处理比b更长的b部分a * /

for(i = alen2; i< blen2; i ++,++ di,++ bi)

{

tmp = * bi + carry;

if(carry)carry =(tmp< = * bi)? 1:0;

else carry =(tmp< * bi)? 1:0;

* di = tmp;

}


/ *将其余部分归零。 * /

for(i = blen2; i< rlen2; i ++,++ di)

{

* di = carry;

carry = 0;

}

}


/ *完成!退回任何剩余的携带物。 * /

返回(随身携带);

}

Hi.

(posted to both newsgroups since I was not sure of which would be
appropriate for this question or how specific to the given language it
is. If one of them is inappropriate, just don''t send replies to it.)

I''m making a bignum package for use in a program I''ve got (this is
something different from the pi program you may have heard about). The
package is going to support manipulating long floating point numbers.

The way I have this set up right now is that there is a "raw" unsigned
integer package, from which the floating point package is built on top
of. The questions I have are about coding that lower-level package.

See, I have a routine here that adds up two unsigned integers, but it
also must be able to add so many digits of one starting at one point
in it''s data, to so many digits of the other starting at a different
point in it''s data, and finally store so many digits of the result at
yet another point in the data buffer holding the result.

This is what I''ve got (it''s in C++, not C, by the way). The problem is
it just doesn''t seem like a "neat", or "nice" piece of code. Anyone
know of a better way to approach this?:

/* Add two RawInts.
* Parameters:
* a: First RawInt
* b: Second RawInt
* rOrigin: offset of digit in this to store result at
* rLength: length of region in this to store result in
* aOrigin: offset of digit in a to start addition at
* aLength: length of region in a to add
* bOrigin: offset of digit in b to start addition at
* bLength: length of region in b to add
*
* Returns: carry.
*
* Operation: *this = a + b.
*
* Use ?Origin = 0 and ?Length = -1 for a simple integer addition
* operation.
*/
DIGIT RawInt::rawAdd(const RawInt &a,
const RawInt &b,
int rOrigin, int rLength,
int aOrigin, int aLength,
int bOrigin, int bLength)
{
int i;
DIGIT tmp, carry = 0;
int rlen2, alen2, blen2;
std::vector<DIGIT>::iterator di, de;
std::vector<DIGIT>::const_iterator ai, ae;
std::vector<DIGIT>::const_iterator bi, be;

/* Make sure we don''t exceed the boundaries
* of the digit arrays.
*/
if(rLength != -1)
{
rlen2 = rLength;
if(rlen2 (length - rOrigin))
rlen2 = (length - rOrigin);
} else {
rlen2 = (length - rOrigin);
}

if(aLength != -1)
{
alen2 = aLength;
if(alen2 (a.length - aOrigin))
alen2 = (a.length - aOrigin);
} else {
alen2 = (a.length - aOrigin);
}

if(bLength != -1)
{
blen2 = bLength;
if(blen2 (b.length - bOrigin))
blen2 = (b.length - bOrigin);
} else {
blen2 = (b.length - bOrigin);
}

if(rOrigin+alen2 >= length)
alen2 = length-rOrigin;
if(rOrigin+blen2 >= length)
blen2 = length-rOrigin;

if(alen2 rlen2) alen2 = rlen2;
if(blen2 rlen2) blen2 = rlen2;

if(alen2 < 0) alen2 = 0;
if(blen2 < 0) blen2 = 0;

/* Set up the iterators */
di = digits.begin()+rOrigin; de = digits.end();
ai = a.digits.begin()+aOrigin; ae = a.digits.end();
bi = b.digits.begin()+bOrigin; be = b.digits.end();

/* Now do the addition */
if(alen2 >= blen2)
{
/* Case 1: a is at least as long as b */

/* Add up a''s and b''s digits */
for(i=0;i<blen2;i++,++di,++ai,++bi)
{
tmp = *ai + *bi + carry;
if(carry) carry = (tmp <= *bi) ? 1 : 0;
else carry = (tmp < *bi) ? 1 : 0;
*di = tmp;
}

/* Now tackle the part of a that is longer than b */
for(i=blen2;i<alen2;i++,++di,++ai)
{
tmp = *ai + carry;
if(carry)
carry = (tmp == 0) ? 1 : 0;
*di = tmp;
}

/* Zeroize the rest of this. */
for(i=alen2;i<rlen2;i++,++di)
{
*di = carry;
carry = 0;
}
} else {
/* Case 2: b is longer than a */

/* Add up a''s and b''s digits */
for(i=0;i<alen2;i++,++di,++ai,++bi)
{
tmp = *ai + *bi + carry;
if(carry) carry = (tmp <= *bi) ? 1 : 0;
else carry = (tmp < *bi) ? 1 : 0;
*di = tmp;
}

/* Now tackle the part of b that is longer than a */
for(i=alen2;i<blen2;i++,++di,++bi)
{
tmp = *bi + carry;
if(carry) carry = (tmp <= *bi) ? 1 : 0;
else carry = (tmp < *bi) ? 1 : 0;
*di = tmp;
}

/* Zeroize the rest of this. */
for(i=blen2;i<rlen2;i++,++di)
{
*di = carry;
carry = 0;
}
}

/* Done! Return any leftover carry. */
return(carry);
}

推荐答案

mike3写道:
mike3 wrote:

>
>



.... snip ...

.... snip ...


>

我正在制作一个bignum包,用于我已经拥有的程序(这是一个与你可能听说过的pi程序不同的东西)。

该软件包将支持操纵长浮点

数字。
>
I''m making a bignum package for use in a program I''ve got (this is
something different from the pi program you may have heard about).
The package is going to support manipulating long floating point
numbers.



为什么?您想要更多有效数字吗?你想要更大的

范围吗?你想做什么。


-

Chuck F(cinefalconer at maineline dot net)

< http: //cbfalconer.home.att.net>

尝试下载部分。

-

通过 http://www.teranews.com

Why? Do you want more significant digits? Do you want a larger
range? What are you trying to do.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com


11月3日下午3:40,CBFalconer< cbfalco ... @ yahoo.comwrote:
On Nov 3, 3:40 pm, CBFalconer <cbfalco...@yahoo.comwrote:

mike3写道:


... snip ...
mike3 wrote:

... snip ...

我正在制作一个bignum包用于我已经拥有的程序(这是一个与你可能听说过的pi程序不同的东西。

该程序包将支持操纵长浮动点

数字。
I''m making a bignum package for use in a program I''ve got (this is
something different from the pi program you may have heard about).
The package is going to support manipulating long floating point
numbers.



为什么?您想要更多有效数字吗?你想要更大的

范围吗?你想做什么。


Why? Do you want more significant digits? Do you want a larger
range? What are you trying to do.



这是对的 - 更精确。更重要的数字。这是一个

分形生成器,所以你可以在内心深处放大。

That''s right -- more precision. More significant digits. It''s for a
fractal generator, so you can zoom real deep down.


-

Chuck F(cinefalconer at maineline dot net)

< http://cbfalconer.home.att.net>

尝试下载部分。


-

通过免费的Usenet帐户发布来自http://www.teranews.com
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account fromhttp://www.teranews.com





mike3在留言中写道...

mike3 wrote in message...

>

DIGIT RawInt :: rawAdd(const RawInt& a,

const RawInt& b,int rOrigin,int rLength,

int aOrigin,int aLength,int bOrigin,int bLength){
>
DIGIT RawInt::rawAdd(const RawInt &a,
const RawInt &b, int rOrigin, int rLength,
int aOrigin, int aLength, int bOrigin, int bLength){



// int i;

// int i;


DIGIT tmp,carry = 0;

int rlen2,alen2,blen2;
DIGIT tmp, carry = 0;
int rlen2, alen2, blen2;



// std :: vector< DIGIT> :: iterator di,de;

// std :: vector< DIGIT>: :const_iterator ai,ae;

// std :: vector< DIGIT> :: const_iterator bi,be;


[注意''评论''以上]

你似乎有''C''背景< G>。声明并初始化那些使用它们的
。请参阅下面的''di'','de''。

// std::vector<DIGIT>::iterator di, de;
// std::vector<DIGIT>::const_iterator ai, ae;
// std::vector<DIGIT>::const_iterator bi, be;

[ note ''commented'' above ]
You seem to have a ''C'' background <G>. Declare and initialize those where
you use them. See ''di'', ''de'' below.


>

/ *确保我们不要超过数字阵列的边界

*。 * /

if(rLength!= -1){

rlen2 = rLength;

if(rlen2(length - rOrigin))

rlen2 =(长度 - rOrigin);

}否则{

rlen2 =(长度 - rOrigin);

}


if(aLength!= -1){

alen2 = aLength;

if(alen2(a.length - aOrigin) ))

alen2 =(a.length - aOrigin);

} else {

alen2 =(a.length - aOrigin);

}


if(bLength!= -1){

blen2 = bLength;

if (blen2(b.length - bOrigin))

blen2 =(b.length - bOrigin);

}否则{

blen2 =( b.length - bOrigin);

}
>
/* Make sure we don''t exceed the boundaries
* of the digit arrays. */
if(rLength != -1){
rlen2 = rLength;
if(rlen2 (length - rOrigin))
rlen2 = (length - rOrigin);
} else {
rlen2 = (length - rOrigin);
}

if(aLength != -1){
alen2 = aLength;
if(alen2 (a.length - aOrigin))
alen2 = (a.length - aOrigin);
} else {
alen2 = (a.length - aOrigin);
}

if(bLength != -1){
blen2 = bLength;
if(blen2 (b.length - bOrigin))
blen2 = (b.length - bOrigin);
} else {
blen2 = (b.length - bOrigin);
}



这三个if-else块是相同的。我会将它们移到自己的

小功能中。那么你只需要称它们为:


// make''MyFunc()''返回一个int类型。

int rlen2(MyFunc (rLength,length,rOrigin));

int alen2(MyFunc(aLength,a.length,aOrigin));

int blen2(MyFunc(bLength,b.length) ,bOrigin));

Those three if-else blocks are identical. I''d move them into their own
little function. Then you''d just call them something like:

// make ''MyFunc()'' return an type int.
int rlen2( MyFunc( rLength, length, rOrigin ) );
int alen2( MyFunc( aLength, a.length, aOrigin ) );
int blen2( MyFunc( bLength, b.length, bOrigin ) );


>

if(rOrigin + alen2> = length)

alen2 = length-rOrigin;

if(rOrigin + blen2> = length)

blen2 = length-rOrigin;


if (alen2 rlen2)alen2 = rlen2;

if(blen2 rlen2)blen2 = rlen2;

if(alen2< 0)alen2 = 0;

if(blen2< 0)blen2 = 0;


/ *设置迭代器* /

di = digits.begin()+ rOrigin ; de = digits.end();
>
if(rOrigin+alen2 >= length)
alen2 = length-rOrigin;
if(rOrigin+blen2 >= length)
blen2 = length-rOrigin;

if(alen2 rlen2) alen2 = rlen2;
if(blen2 rlen2) blen2 = rlen2;
if(alen2 < 0) alen2 = 0;
if(blen2 < 0) blen2 = 0;

/* Set up the iterators */
di = digits.begin()+rOrigin; de = digits.end();



这是你使用''di'''''''的第一个地方。我会这样做的。


std :: vector< DIGIT> :: iterator di(digits.begin()+ rOrigin),

de(digits.end());

This is the first place you use ''di'', ''de''. I''d do it this way.

std::vector<DIGIT>::iterator di( digits.begin() + rOrigin ),
de( digits.end() );


ai = a.digits.begin()+ aOrigin; ae = a.digits.end();
ai = a.digits.begin()+aOrigin; ae = a.digits.end();



std :: vector< DIGIT> :: const_iterator ai(a.digits.begin()+ aOrigin),

ae(a .digits.end());

std::vector<DIGIT>::const_iterator ai( a.digits.begin() + aOrigin ),
ae( a.digits.end() );


bi = b.digits.begin()+ bOrigin; be = b.digits.end();
bi = b.digits.begin()+bOrigin; be = b.digits.end();



std :: vector< DIGIT> :: const_iterator bi(b.digits.begin()+ bOrigin),

be(b .digits.end());

std::vector<DIGIT>::const_iterator bi( b.digits.begin() + bOrigin ),
be( b.digits.end() );


>

/ *现在添加* /

if(alen2> = blen2){

/ *案例1:a至少和b * /
一样长*
/ *加一个' 's和b'的数字* /
>
/* Now do the addition */
if(alen2 >= blen2){
/* Case 1: a is at least as long as b */

/* Add up a''s and b''s digits */



// for(i = 0; i< blen2; i ++,++ di,++ ai,++ bi){


你不要在for()循环之外使用''int i',所以,在*

中声明它们为*( ):


for(int i(0); i< blen2; ++ i,++ di,++ ai,++ bi){

// for(i=0;i<blen2;i++,++di,++ai,++bi){

You do not use ''int i'' outside the for() loops, so, declare them *in* the
for():

for( int i(0); i < blen2; ++i, ++di, ++ai, ++bi ){


tmp = * ai + * bi + carry;

if(carry)carry =(tmp< = * bi)? 1:0;

else carry =(tmp< * bi)? 1:0;

* di = tmp;

}


/ *现在解决一个比b * /
tmp = *ai + *bi + carry;
if(carry) carry = (tmp <= *bi) ? 1 : 0;
else carry = (tmp < *bi) ? 1 : 0;
*di = tmp;
}

/* Now tackle the part of a that is longer than b */



// for(i = blen2; i< alen2; i ++,++ di,++ ai){


for(int i(blen2); i< alen2; ++ i,++ di,++ ai){

// for(i=blen2;i<alen2;i++,++di,++ai){

for( int i( blen2 ); i < alen2; ++i, ++di, ++ai ){


tmp = * ai +携带;

if(carry)

carry =(tmp == 0)? 1:0;

* di = tmp;

}


/ *将其余部分归零。 * /
tmp = *ai + carry;
if(carry)
carry = (tmp == 0) ? 1 : 0;
*di = tmp;
}

/* Zeroize the rest of this. */



// for(i = alen2; i< rlen2; i ++,++ di){


for (int i(alen2); i< rlen2; ++ i,++ di){

..... etc.

那只是我的建议(多个)。也许没有杂乱,你会看到其他

的地方''清理''。

-

Bob R
POVrookie

// for(i=alen2;i<rlen2;i++,++di){

for( int i( alen2 ); i < rlen2; ++i, ++di ){
..... etc.
That''s just my suggestion(s). Maybe with less clutter you''ll see other
places to ''clean up''.
--
Bob R
POVrookie


这篇关于更好的方法来编程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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