整数基函数 [英] Integer Base Function

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

问题描述

为了您的乐趣,一个函数表示任何整数,其中
绝对值小于或等于9个quintillion在任何

基础上从2到36 。(对于较大的基数,你可以用

变音符号扩展

数字字符串,如果你不介意iso -8895-1而不是ASCII。

如果你不喜欢非std类型long long,你可以随时将
改为b long。很长的;但是它只能处理大约20亿美元的数字,而不是9亿美元。

///////////// ////////////////////////////////////////////////// ////////////

// //

//基数//

//表示整数在2到36的任何基地// //

// //

//////////////////////////////////////// ///////////////////////////////////


std :: string

Base



int base,//必须> = 2且< = 36

int precision,//必须是> = 1且< = 63

long long number,//必须是> = -9E18和< = + 9E18

bool leading_zeros //用户想要前导零吗?



{

const long long MAX = 9223372036854775807LL;

const long long MIN = -9223372036854775808LL;

double maximum = pow(base,precision) - 1;

if



base< 2 || base 36 //如果base超出范围

||精度< 1 ||精度63 //或精度超出范围

||数字< MIN ||数字MAX //或数字超出范围

||最大MAX //或基本/精度组合超出范围

||最大的<数字//或基数/精度组合不能表示数字



{

return std :: string(" ERROR" ); //然后返回ERROR。

}


std :: ostringstream代表;

if(number< 0 )

{

number * =( - 1);

repre<< '' - '';

}


long long place = 1LL;

for(int i = 1; i< ; = precision - 1; ++ i)

{

place * = base;

}


long long value = 0LL;

std :: string digits =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;;

bool first_non_zero = false;

for (;地点0;地点/ =基地)

{

value =数字/地点;

if(value 0)first_non_zero = true;

if(leading_zeros || first_non_zero)repre<<数字[值];

数字 - =价值*地方;

}

返回repre.str();

}

-

干杯,

Robbie Hatley

美国加利福尼亚州塔斯廷

lonewolfintj在pacbell dot net

(将[usenet]置于旁路垃圾邮件过滤器中)
http://home.pacbell.net/earnur/

解决方案

Robbie Hatley发布:


const long long MAX = 9223372036854775807LL;



删除LL在最后标记,它没有任何意义。事实上,如果你决定更改你正在使用的整数类型,它只会阻碍你。

有关整数文字的信息,请转到第20页以下文件:

http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf

MAX和MIN广泛用作宏其他东西的名字 - 期待

冲突。

numeric_limits< long long> :: max()会更好(除非你需要一个

编译时常量)。我没有检查标准,但我认为

有一个LONGLONG_MAX宏。


const long long MIN = -9223372036854775808LL;




而不是:


-9223372036854775808LL

我认为您需要:


-9223372036854775807 -1

(如果一个程序包含一个整数字面值,则程序格式不正确

范围如果你写的:


-9223372036854775808


它被解释为:


- (9223372036854775808)


而且,正如你所看到的那样,这个正数超出了范围。所以你需要写一下b $ b:


-9223372036854775807 -1

(但不用担心,这是编译时常量。)


double double = pow(base,precision) - 1;

if



base< 2 || base 36 //如果base超出范围

|| precision< 1 || precision 63 //或精度是

||超出范围的数字< ; MIN ||数字MAX //或数字

||是超出范围的最大MAX //或基数/精度

||组合超出范围最大<数//或

|| base / precision combo不能表示数字



{

return std :: string(" ERROR"); //然后返回ERROR。

}




我会认为效率低下,并会使用断言相反。


std :: ostringstream repre;

if(number< 0)

{

number * =( - 1);



你有一个错误。


机器使用的数字系统可能有不对称的范围

正负整数。可能会有负值

没有相应的正值。


repre << '' - '';

}


long long place = 1LL;

for(int i = 1; i< ; = precision - 1; ++ i)

{

place * = base;

}


long long value = 0LL;

std :: string digits =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;;

bool first_non_zero = false;

for (;地点0;地点/ =基地)

{

value =数字/地点;

if(value 0)first_non_zero = true;

if(leading_zeros || first_non_zero)repre<<数字[值];

数字 - =价值*地方;

}

返回repre.str();

}



效率低下;没有必要使用ostringstream。对象。

-


Frederick Gotham


Frederick Gotham写道:
< blockquote class =post_quotes>
Robbie Hatley发布:


> const long long MAX = 9223372036854775807LL;




删除LL在最后标记,它没有任何意义。事实上,如果你决定更改你正在使用的整数类型,它只会阻碍你。

有关整数文字的信息,请转到第20页以下文件:

http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf


MAX和MIN是广泛用作其他东西的宏名称 - 期望

冲突。


numeric_limits< long long> :: max()会更好(除非你需要a

编译时常量)。我没有检查标准,但我认为

有一个LONGLONG_MAX宏。



我不相信很长时间都在标准中(ISO / IEC

14882:2003)。我认为它已包含在C ++的C ++ 0x草案中。

兼容性。


On Sun,2006年7月2日17:54 :31 GMT,red floyd< no ***** @ here.dudewrote

in comp.lang.c ++:


Frederick Gotham写道:


Robbie Hatley发布:


const long long MAX = 9223372036854775807LL;



删除LL在最后标记,它没有任何意义。事实上,如果你决定更改你正在使用的整数类型,它只会阻碍你。

有关整数文字的信息,请转到第20页以下文件:

http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf

MAX和MIN广泛用作宏其他东西的名字 - 期待

冲突。

numeric_limits< long long> :: max()会更好(除非你需要一个

编译时常量)。我没有检查标准,但我认为

有一个LONGLONG_MAX宏。



我不喜欢我们相信很长一段时间都在标准中(ISO / IEC

14882:2003)。我认为它已经包含在C ++的草案中,因为C99

的兼容性。



....并且绝对必要。例如,对于一种语言来说,它是非常有用的

没有一个整数类型可以包含文件系统中的任何文件的大小。或者,在忙碌的一天,在纽约和纳斯达克证券交易所交易的股票数量将在b $ b溢出一个32位的有效值。我想,但是不能打扰到b
,因为几天纳斯达克交易量已超过0x7fffffff

股票。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


For your enjoyment, a function that expresses any integer with
absolute value less-than-or-equal-to nine quintillion in any
base from 2 to 36. (For larger bases, you could expand the
"digit" character string with, perhaps, foreign letters with
diacritical marks, if you don''t mind iso-8895-1 instead of ASCII.
And if you don''t like the non-std type "long long" you can always
change it to "long"; but then it could only handle numbers up to
about 2 billion, instead of 9 quintillion.
///////////////////////////////////////////////////////////////////////////
// //
// Base //
// Represent an integer in any base from 2 to 36. //
// //
///////////////////////////////////////////////////////////////////////////

std::string
Base
(
int base, // must be >= 2 and <= 36
int precision, // must be >= 1 and <= 63
long long number, // must be >= -9E18 and <= +9E18
bool leading_zeros // does user want leading zeros?
)
{
const long long MAX = 9223372036854775807LL;
const long long MIN = -9223372036854775808LL;
double largest = pow(base, precision) - 1;
if
(
base < 2 || base 36 // If base is out-of-range
|| precision < 1 || precision 63 // or precision is out-of-range
|| number < MIN || number MAX // or number is out-of-range
|| largest MAX // or base/precision combo is out-of-range
|| largest < number // or base/precision combo can''t express number
)
{
return std::string("ERROR"); // then return "ERROR".
}

std::ostringstream repre;
if (number < 0)
{
number *= (-1);
repre << ''-'';
}

long long place = 1LL;
for (int i = 1; i <= precision - 1; ++i)
{
place *= base;
}

long long value = 0LL;
std::string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool first_non_zero = false;
for ( ; place 0; place /= base)
{
value = number / place;
if (value 0) first_non_zero = true;
if (leading_zeros || first_non_zero) repre << digits[value];
number -= value * place;
}
return repre.str();
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

解决方案

Robbie Hatley posted:

const long long MAX = 9223372036854775807LL;


Remove the "LL" tagged on the end, it serves no purpose. In fact, it will
only hinder you if you decide to change the integer type you''re using.
For info on integer literals, go to page 20 of the following document:

http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf
MAX and MIN are widely used as macro names for other things -- expect
conflict.
numeric_limits<long long>::max() would be preferable (unless you need a
compile-time constant). I haven''t checked the Standard, but I''d presume
that there''s a LONGLONG_MAX macro.

const long long MIN = -9223372036854775808LL;



Rather than:

-9223372036854775808LL
I think you need:

-9223372036854775807 -1
(A program is ill-formed if it contains an integer literal which is out
of range. If you write:

-9223372036854775808

It''s intepreted as:

-(9223372036854775808)

And, as you can see, that positive figure is out of range. Therefore you
need to write:

-9223372036854775807 -1
(But no worries, it''s a compile-time constant.)

double largest = pow(base, precision) - 1;
if
(
base < 2 || base 36 // If base is out-of-range
|| precision < 1 || precision 63 // or precision is
|| out-of-range number < MIN || number MAX // or number
|| is out-of-range largest MAX // or base/precision
|| combo is out-of-range largest < number // or
|| base/precision combo can''t express number
)
{
return std::string("ERROR"); // then return "ERROR".
}



I''d consider that inefficient, and would use asserts instead.

std::ostringstream repre;
if (number < 0)
{
number *= (-1);


You have a bug there.

The number system used by the machine may have asymmetrical ranges for
positive and negative integers. There''s likely to be a negative value
which hasn''t got a corresponding positive value.

repre << ''-'';
}

long long place = 1LL;
for (int i = 1; i <= precision - 1; ++i)
{
place *= base;
}

long long value = 0LL;
std::string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool first_non_zero = false;
for ( ; place 0; place /= base)
{
value = number / place;
if (value 0) first_non_zero = true;
if (leading_zeros || first_non_zero) repre << digits[value];
number -= value * place;
}
return repre.str();
}


That''s inefficient; there''s no need for a "ostringstream" object.
--

Frederick Gotham


Frederick Gotham wrote:

Robbie Hatley posted:

> const long long MAX = 9223372036854775807LL;



Remove the "LL" tagged on the end, it serves no purpose. In fact, it will
only hinder you if you decide to change the integer type you''re using.
For info on integer literals, go to page 20 of the following document:

http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf
MAX and MIN are widely used as macro names for other things -- expect
conflict.
numeric_limits<long long>::max() would be preferable (unless you need a
compile-time constant). I haven''t checked the Standard, but I''d presume
that there''s a LONGLONG_MAX macro.

I don''t believe that long long is in the Standard yet (ISO/IEC
14882:2003). I think it''s included in the draft C++0x for C99
compatibility.


On Sun, 02 Jul 2006 17:54:31 GMT, red floyd <no*****@here.dudewrote
in comp.lang.c++:

Frederick Gotham wrote:

Robbie Hatley posted:

const long long MAX = 9223372036854775807LL;


Remove the "LL" tagged on the end, it serves no purpose. In fact, it will
only hinder you if you decide to change the integer type you''re using.
For info on integer literals, go to page 20 of the following document:

http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf
MAX and MIN are widely used as macro names for other things -- expect
conflict.
numeric_limits<long long>::max() would be preferable (unless you need a
compile-time constant). I haven''t checked the Standard, but I''d presume
that there''s a LONGLONG_MAX macro.


I don''t believe that long long is in the Standard yet (ISO/IEC
14882:2003). I think it''s included in the draft C++0x for C99
compatibility.

....and for absolute necessity. It is very handicapping for a language
not to have an integer type that can contain the size of any file in
the file system, for example. Or, on a busy day, the number of shares
traded on the New York and NASDAQ stock exchanges together will
overflow a signed 32-bit value. I think, but can''t be bothered to
look up, the fact that the NASDAQ volume alone has exceeded 0x7fffffff
shares on a few days.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于整数基函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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