关于如何输入很长的数字的问题。 [英] A question about how to enter a very long number.

查看:85
本文介绍了关于如何输入很长的数字的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输入一个字符串,例如111111111111 ..... 11111111。 (50

),然后将其转换为

数字(111111111111 ..... 11111111),因为我想用那个数字来

做了一些补充。我怎么能这样做?


以下是我的试用版,但我发现a永远是0.


#include< stdio.h>

#include< stdlib.h>


int main()

{


char string1 [50];

int a;


scanf("%s",string1);


a = atoi(" string1");


printf( "%d",a);


返回0;

}

I would like to enter a string such as "111111111111.....11111111" (50
ones),and then convert it to a
number(111111111111.....11111111),because I want to use that number to
do some additions.How can I do that?

The following is my trial,but I find that the "a" is always 0.

#include<stdio.h>
#include<stdlib.h>

int main()
{

char string1[50];
int a;

scanf("%s",string1);

a= atoi("string1");

printf("%d",a);

return 0;
}

推荐答案

66******@qq.com 写道:
66******@qq.com writes:

我想输入一个字符串,例如111111111111 ..... 11111111 (50

),然后将其转换为

数字(111111111111 ..... 11111111),因为我想用那个数字来

做了一些补充。我怎么能这样做?
I would like to enter a string such as "111111111111.....11111111" (50
ones),and then convert it to a
number(111111111111.....11111111),because I want to use that number to
do some additions.How can I do that?



我不会尝试。您的系统不太可能有任何整数类型

可以保存该值,因此您必须执行其他操作。由于这个
看起来像家庭作业,我怀疑答案是使用bignum库。

合适。你必须想出一些代表这些

数字的方法,直到你这样做,没有人能说你如何将你的字符串

转换成这样的数字。一种解决方案是将其保留为字符串。


< snip>

-

Ben。

I would not try. Your system is unlikely to have any integer type
that can hold the value so you have to do something else. Since this
looks like homework, I doubt the answer "use a bignum library" is
suitable. You have to come up with some way to represent these
numbers, and until you do, no one can say how to convert your string
into such a number. One solution is to leave it as a string.

<snip>
--
Ben.


66******@qq.com 写道:

我想输入一个字符串,例如111111111111 ..... 11111111。 (50

),然后将其转换为

数字(111111111111 ..... 11111111),因为我想用那个数字来

做了一些补充。我怎么能这样做?
I would like to enter a string such as "111111111111.....11111111" (50
ones),and then convert it to a
number(111111111111.....11111111),because I want to use that number to
do some additions.How can I do that?



[...]

[...]


scanf("%s",string1);


a = atoi(" string1");
scanf("%s",string1);

a= atoi("string1");



[...]


一个问题是你需要了解
$ b之间的区别$ b string1



" string1"


修复这不足以解决你的问题;

11111111111111111111111111111111111111111111111111需要

164位有符号整数类型来表示它,而你的实现

几乎肯定不会提供这样的类型。


另外,对于快速而又脏的
程序来说,atoi函数可能是可以接受的,但如果你给它一些东西它的行为是不可能的手柄是

不可预测。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

[...]

One problem is that you need to understand the difference between
string1
and
"string1"

Fixing that isn''t enough to solve your problem;
11111111111111111111111111111111111111111111111111 would require a
164-bit signed integer type to represent it, and your implementation
almost certainly doesn''t provide such a type.

Also, the atoi function is perhaps acceptable for a quick-and-dirty
program, but its behavior if you give it something it can''t handle is
unpredictable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


66 ****** @ qq.com 写道:

我会喜欢输入诸如111111111111 ..... 11111111之类的字符串。 (50

),然后将其转换为

数字(111111111111 ..... 11111111),因为我想用那个数字来

做了一些补充。我怎么能这样做?


以下是我的试用版,但我发现a总是0.
I would like to enter a string such as "111111111111.....11111111" (50
ones),and then convert it to a
number(111111111111.....11111111),because I want to use that number to
do some additions.How can I do that?

The following is my trial,but I find that the "a" is always 0.



[绝望的代码剪断]

尝试这个作为开始的地方。你显然想修改它。


#include< stdio.h>

#include< limits.h>

#include< stdlib.h>

#include< errno.h>

#include< string.h>

#include< stddef.h>


int main()

{

unsigned long long / *或unsigned long if你没有

这种类型* /

the_number = 0;

无符号限制= CHAR_BIT * sizeof the_number;

char buffer [limit + 2],* endp,* nl;

ptrdiff_t nchars;

printf("请输入最多为二进制数的%) u digits。\ n"

" 0或1以外的字符将终止该号码。\ n"

"记住结束你的输入行尾字符。\ n"

"在此输入:",limit);

fflush(stdout);

errno = 0;

if(!fgets(buffer,sizeof buffer,stdin)){

fp rintf(stderr,

你的输入发生了一些不愉快的事情。\ nn;)

if(errno)

perror(& ; errno设置因为:");

fprintf(stderr,我放弃了。\ n);

退出(EXIT_FAILURE);

}

if((nl = strchr(buffer,''\ n'')))

* nl = 0; < br $>
其他

fprintf(stderr,这是一个很长的路线。行尾\ n

字符​​从未进入缓冲区,\ n"

"我们将转换的字符串是: \ n \'"%s \" \ n",

缓冲区);

the_number =

strtoull / *或strtoul,如果需要* /(缓冲区,& endp,2);

if(errno)

perror(errno set set:);

if(* endp){

nchars = endp - buffer;

fprintf(stderr,"字符串以%c结尾(%d) )\ n"

而不是EOL字符。\ nn,* endp,* endp);

* endp = 0;

printf("转换的字符串是%u chars,并且是\ n"

" \"%s \"。\ n",(无符号) )nchars,缓冲区);

}

printf

(你的号码是%llu(基数10),%#llo(基数) 8),%#llx"

"(base 16)\ n",

the_number,the_number,the_number);

返回n 0;

}

[hopeless code snipped]
Try this as a place to start. You will obviously want to modify it.

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>

int main()
{
unsigned long long /* or unsigned long if you don''t have
this type */
the_number = 0;
unsigned limit = CHAR_BIT * sizeof the_number;
char buffer[limit + 2], *endp, *nl;
ptrdiff_t nchars;
printf("Please type a binary number of at most %u digits.\n"
"A character other than 0 or 1 will terminate the number.\n"
"Remember to end your imput with an end-of-line character.\n"
"Type it here: ", limit);
fflush(stdout);
errno = 0;
if (!fgets(buffer, sizeof buffer, stdin)) {
fprintf(stderr,
"Something untoward happened with your input.\n");
if (errno)
perror("errno was set because:");
fprintf(stderr, "I''m giving up.\n");
exit(EXIT_FAILURE);
}
if ((nl = strchr(buffer, ''\n'')))
*nl = 0;
else
fprintf(stderr, "That was a very long line. The end-of-line\n"
"character never made it into the buffer,\n"
"The string we will be converting is:\n\"%s\"\n",
buffer);
the_number =
strtoull /* or strtoul, if needed */ (buffer, &endp, 2);
if (errno)
perror("errno was set:");
if (*endp) {
nchars = endp - buffer;
fprintf(stderr, "The string ended with %c (%d)\n"
"rather than an EOL character.\n", *endp, *endp);
*endp = 0;
printf("The string converted was %u chars, and was\n"
"\"%s\".\n", (unsigned) nchars, buffer);
}
printf
("Your number is %llu (base 10), %#llo (base 8), %#llx"
" (base 16)\n",
the_number, the_number, the_number);
return 0;
}


这篇关于关于如何输入很长的数字的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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