用逗号输入整数 [英] Inputting integers with commas

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

问题描述

怎么办?这么简单#@%^& @的东西,我花了好几个小时。


例如,


long mynum ;


istringstream mystr(" 380,900",istringstream :: in);

mystr>> mynum;

cout<< mynum;


收益率为380。


我试过设置mystr的语言环境,

mystr.imbue(locale(" en_US"));


各种各样的东西,但没有快乐。


是,我知道我可以迭代并删除逗号,但是我希望
希望有一个更优雅和/或内置的解决方案。

-

%Randy Yates%" Bird,在翼上,

%% Fuquay-Varina,NC%漂浮在

%%% 919- 577-9882%,但他眼中有泪滴......

%%%%< ya *** @ ieee.org> %''一个夏日梦'',*面对音乐*,ELO
http ://home.earthlink.net/~yatescr

How to do? Such a simple #@%^&@ thing and I''m spending hours on it.

For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".

I''ve tried setting the locale of mystr,

mystr.imbue(locale("en_US"));

to various things but no joy.

Yes, I know I could iterate through and .erase the commas, but I
was hoping there was a more elegant and/or built-in solution.
--
% Randy Yates % "Bird, on the wing,
%% Fuquay-Varina, NC % goes floating by
%%% 919-577-9882 % but there''s a teardrop in his eye..."
%%%% <ya***@ieee.org> % ''One Summer Dream'', *Face The Music*, ELO
http://home.earthlink.net/~yatescr

推荐答案

Randy Yates schrieb:
Randy Yates schrieb:
怎么办?这么简单#@%^& @的事情,我花了好几个小时。

例如,

长的mynum;
istringstream mystr(" 380,900",istringstream :: in);
mystr>> mynum;
cout<< mynum;

收益率" 380"。
How to do? Such a simple #@%^&@ thing and I''m spending hours on it.

For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".




正确地说,你只读第一个数字; - )

除此之外你可以把逗号读成char:

char c;

long mynum1,mynum2;

mystr>> mynum1>> c>> mynum2;


HTH,
Malte



And correctly so, you''re only reading the first number ;-)
Apart from that you could just read the comma into a char:
char c;
long mynum1, mynum2;
mystr >> mynum1 >> c >> mynum2;

HTH,
Malte


Malte Starostik schrieb:
Malte Starostik schrieb:
Randy Yates schrieb:
Randy Yates schrieb:
怎么办?这么简单#@%^& @的东西,我花了好几个小时。
例如,

long mynum;

istringstream mystr( 380,900,istringstream :: in);
mystr>> mynum;
cout<< mynum;

收益率" 380"。
How to do? Such a simple #@%^&@ thing and I''m spending hours on it.
For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".



正确地说,你只读第一个数字; - )


And correctly so, you''re only reading the first number ;-)




抱歉,我认为逗号不应该分开单个

数字而是千分隔符? (不习惯,在我的de_DE语言环境中

千分隔符是a。而小数点是a,)

在这种情况下可能是这样的:


long mynum;

istringstream mystr(" 380,900",istringstream :: in);

for(mynum = 0; mystr;)

{

long tmp;

if(mystr>> tmp)mynum + = tmp;

char c;

if(mystr>> c&& c =='','')mynum * = 1000;

}

cout<< mynum<< endl;


错误检查虽然非常不理想,但它读取的内容如

380,-900为379100,380,1,0为380001000等。


问候,

Malte



Sorry, I gather that comma is not supposed to separate individual
numbers but a thousand separator? (not used to that, in my de_DE locale
the thousand separator is a . while a decimal point is a ,)
In that case maybe something like this:

long mynum;
istringstream mystr("380,900", istringstream::in);
for ( mynum = 0; mystr; )
{
long tmp;
if (mystr >> tmp) mynum += tmp;
char c;
if (mystr >> c && c == '','' ) mynum *= 1000;
}
cout << mynum << endl;

The error checking is quite suboptimal though and it reads things like
380,-900 as 379100, 380,1,0 as 380001000 etc.

Regards,
Malte


Malte Starostik< ma ******* ******@t-online.de>写道:
Malte Starostik <ma*************@t-online.de> writes:
Randy Yates schrieb:
Randy Yates schrieb:
怎么办?这么简单的#@%^& @的东西,我花了好几个小时才开始。例如,
long mynum;
istringstream mystr(" 380,900",istringstream :: in);
mystr>> mynum;
cout<< mynum;
收益率380。
正确地说,你只读了第一个数字
How to do? Such a simple #@%^&@ thing and I''m spending hours on
it. For example,
long mynum;
istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;
yields "380".
And correctly so, you''re only reading the first number




我明白了。因此,当你的老板给你发电子邮件并询问


Hey Malte,请为公司野餐订购1,173件T恤,


you比如说,


嘿老板,这是1或173?


;(

;-)
除此之外你可以把逗号读成char:
char c;
long mynum1,mynum2;
mystr>> mynum1>> c>> mynum2;


正如我所说,我知道有各种各样的方法 - 我正在寻找

优雅/正确的方式。

HTH,


不是真的。

Malte



I see. So when your boss emails you and asks

"Hey Malte, please order 1,173 T-Shirts for the company picnic",

you say,

"Hey boss, which is it, 1 or 173?"

;(
;-) Apart from that you could just read the comma into a char:
char c;
long mynum1, mynum2;
mystr >> mynum1 >> c >> mynum2;
As I said, I know there are ways AROUND it - I was looking for the
elegant/proper way.
HTH,
Not really.
Malte




-

%Randy Yates%"也许有一天我会感受到她冷酷的拥抱,

%% Fuquay-Varina,NC%并亲吻她的界面,

% %% 919-577-9882%直到那时,我会离开她。

%%%%< ya *** @ ieee.org> %''你的真实,2095',*时间*,ELO
http: //home.earthlink.net/~yatescr


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

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