A question about how to enter a very long number.

6

66650755

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;


}
 
B

Ben Bacarisse

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?

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>
 
K

Keith Thompson

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");
[...]

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.
 
M

Martin Ambuhl

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.

[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;
}
 
M

Martin Ambuhl

Ben said:
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.

This is an ubwarranted claim. The number of implementations without a
64 bit integral type is becoming vanishingly small.
And 66650755 needs only 50 of them.
 
M

Martin Ambuhl

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?

I should point out that your question does not tell us the base of the
representation. All other answers that I have seen assume base 10, and
their statements about probable impossibility are correct with that
assumption. The code I posted assumes base 2. Higher bases may quickly
run out of room.
 
N

Nick Keighley

This is an ubwarranted claim.  The number of implementations without a
64 bit integral type is becoming vanishingly small.
And 66650755 needs only 50 of them.
 
N

Nick Keighley

This is an ubwarranted claim.  The number of implementations without a
64 bit integral type is becoming vanishingly small.
And 66650755 needs only 50 of them.

I must be in need of coffee. I don't follow this. What is
the conection between a number that is approx 1e50 and your
666... number? Even 1...1 base 2 is larger than that.
 
O

osmium

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;


}

Start by getting your program to run with a more modestly sized number, say
three or four decimal digits. Once that works you can address the problem
of how to handle Really Big Numbers. The link below might be helpful in
attacking phase 2.

http://en.wikipedia.org/wiki/Binary_coded_decimal
 
B

Ben Bacarisse

Martin Ambuhl said:
This is an ubwarranted claim. The number of implementations without a
64 bit integral type is becoming vanishingly small.
And 66650755 needs only 50 of them.

True, but I thought the 1s were an example and the number was
decimal. This seemed reasonable since the code tried to use atoi.
You've given the OP the base-2 solution so all the bases are now
covered!
 
W

WANG Cong

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?

Well, if that is a binary number, you can use strtoll(); if that is a
decimal, _probably_ you have to need the gmp library to make that.

See: http://gmplib.org/

Good luck!


Cong
 
K

Keith Thompson

CBFalconer said:
INT_MAX, as read from <limits.h>.

By snipping most of Mark's message, you give the impression that he's
not already perfectly well aware of that. His intent, I think was to
encourage the OP to think about these questions, not to solicit
answers to be posted here.
 
C

CBFalconer

Keith said:
By snipping most of Mark's message, you give the impression that
he's not already perfectly well aware of that. His intent, I
think was to encourage the OP to think about these questions, not
to solicit answers to be posted here.

Not so. By snipping I reduced the question/answer to a minimum
that could impress a relatively newbie reader. Usenet is not a
home for private messages. Everything is public.
 
K

Keith Thompson

CBFalconer said:
Not so. By snipping I reduced the question/answer to a minimum
that could impress a relatively newbie reader.

I don't understand. Whom are you trying to impress? And what does
that have to do with the original poster's question (which isn't
quoted here, but was quoted in the article to which you replied)?
Usenet is not a
home for private messages. Everything is public.

Certainly, but answers to questions are typically addressed to the
questioner, with others able to read the answer so that they can
either learn from it, correct it, or add to it. In this case, the
questioner was the original poster; Mark, quite reasonably, answered
by posing questions intended to lead the OP to figure things out for
himself.

If you happen to be walking by a classroom when the teacher is asking
the students "What is two plus three?", do you stick your head in the
door and shout "five"?
 
K

Kenny McCormack

Keith Thompson said:
If you happen to be walking by a classroom when the teacher is asking
the students "What is two plus three?", do you stick your head in the
door and shout "five"?

Actually, CBF probably would.
 
C

CBFalconer

Keith said:
.... snip ...


I don't understand. Whom are you trying to impress? And what does
that have to do with the original poster's question (which isn't
quoted here, but was quoted in the article to which you replied)?

"Marks identity" and "Marks message" have little to do with it. I
usually don't check the writers identity. As I recall I simply
cleared up a point that appeared obscure, to say the least, in the
original.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top