473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about how to enter a very long number.

I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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",stri ng1);

a= atoi("string1") ;

printf("%d",a);

return 0;
}
Oct 30 '08 #1
18 1711
66******@qq.com writes:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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>
--
Ben.
Oct 30 '08 #2
66******@qq.com writes:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),because I want to use that number to
do some additions.How can I do that?
[...]
scanf("%s",stri ng1);

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;
111111111111111 111111111111111 111111111111111 11111 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_Keit h) 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"
Oct 30 '08 #3
66******@qq.com wrote:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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_FAILU RE);
}
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;
}

Oct 30 '08 #4
Ben Bacarisse wrote:
66******@qq.com writes:
>I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(1111111 11111.....11111 111),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.
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.
Oct 30 '08 #5
66******@qq.com wrote:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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.
Oct 30 '08 #6
On 30 Oct, 06:25, Martin Ambuhl <mamb...@earthl ink.netwrote:
Ben Bacarisse wrote:
66650...@qq.com writes:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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. *

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.
Oct 30 '08 #7
On 30 Oct, 06:25, Martin Ambuhl <mamb...@earthl ink.netwrote:
Ben Bacarisse wrote:
66650...@qq.com writes:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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. *

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.

--
Nick Keighley

Oct 30 '08 #8
<66******@qq.co mwrote:
>I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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",stri ng1);

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
Oct 30 '08 #9
66******@qq.com wrote:
I would like to enter a string such as "111111111111.. ...11111111" (50
ones),and then convert it to a
number(11111111 1111.....111111 11),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.

int a;
What is the largest number you can hold in an int?
scanf("%s",stri ng1);
don't use scanf - what happens if the user types
"supercalifragi listicexpialido coius" instead of a number?
a= atoi("string1") ;
don't use atoi -it doesn't do any error checking.

Also, why are you passing a string literal "string1" to atoi? You surely
want to pass a variable to it...

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Oct 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14807
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
3
1999
by: Jack | last post by:
On the 1st of January 1998, Bjarne Stroustrup gave an interview to the IEEE's 'Computer' magazine. Naturally, the editors thought he would be giving a retrospective view of seven years of object-oriented design, using the language he created. By the end of the interview, the interviewer got more than he had bargained for and, subsequently, the editor decided to suppress its contents, 'for the good of the industry' but, as with many of...
20
3343
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to speed. I like books with practical exercises, and also with test questions (like cert books) *2*
13
2199
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int median_index;
0
2063
by: Joseph S. | last post by:
hi all, debugging PHP applications interactively is possible, easy and free. I am talking about PHPEclipse and using it for debugging over several scripts or debugging through a session. Since I have wasted a lot of time writing echo statements all over the code in order to confirm program flow and watch variables, I feel I must share this with others who will be facing similar problems. PHPEclispe users can go to Step 5 directly....
5
2405
by: Donkey | last post by:
Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below: #include <stdio.h> const long double num=1123222.232121342; main() { Printf("the number occupies: %i, and it is 1123222.232121342 \n",sizeof num); printf("char: %i ,the number is %c\n",sizeof(char),(char)num);
2
2911
by: TheRomance | last post by:
i have a problem about insert integer to link list. sorry it's too long but i try many times, many ways , it's still have an error function is fix . can't change anything about function. i can only make a code that work with function. Number.cpp #include "Number.h"
75
5468
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has been a significant decline in the number of students opting to take courses in C++. I just want to let people know what I believe are the biggest obstacles to C++ language acquisition, and what aspects of the language make it less appealing than...
9
2714
by: Amy Lee | last post by:
Hello, I major maths in mu university, and I wanna build an app to do Steffensen's Acceleration Method. Here's my code: /* Equation: x^3-x-1=0 */ #include <stdio.h> #include <math.h>
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.