1

I am currently working on a C project that requires the creation, storage and mathematical usage of numbers that are too large to be put into normal variable types. To do this, we were instructed to represent numbers as a sequence of digits stored in an array of integers. I use a struct defined as so:

struct BigInt {
int val[300000];
int size;
};

(I know I can dynamically allocate memory, and that that is preferable, however this is how I am most comfortable doing it, it has worked perfectly fine so far and this is how the professor instructed us to do it.)

I then define member A:

struct BigInt A={NULL};

I can generate and store, then add, subtract and multiply random numbers with this, and they can have any number digits up to 300000(far more than I will ever need to account for). For example, if the number 1432 was generated and stored into BigInt A, A.size would be 4 and A.val[2] would be 3.

Now I need to create a way to store user input into this type. For example, the user needs to be able go straight from inputting 50! and then it be stored into this struct array type I have created. How would I go about doing this?

The only ways that I could think of would be to store the user input as a string then have the math in that string be executed multiple times, each time storing a different digit, or reading numbers straight off of stdout, but I don't know if either of those are even possible or would solve my problem.

2
  • If you intend to convert any expression input as a string, you will have to write a simple parser. That will break the expressions into tokens and create a tree. You can then evaluate all the nodes of the tree using your basic operations and store the result in the final struct. Commented Jul 24, 2017 at 4:00
  • Also, allocating 300000 integers inside the struct might be a very bad idea. Most implementations wont be able to support such large auto variables. I would suggest starting with small size like 4 digits and growing exponentially as required. Also you are using base 10 to store numbers in int which can be very inefficient. You can use char instead. Also see if you can implement base 256. Commented Jul 24, 2017 at 4:02

2 Answers 2

1

You can try using string as follows:

char s[300001];
scanf("%s", s);
A.size = strlen(s);
for(int i = 0; i < A.size; i++){
    A.val[i] = s[i] - '0';
}

I think it will solve your problem, but this way of implementation for big integers is not efficient though.

Sign up to request clarification or add additional context in comments.

Comments

1

Sorry for previous answer, to solve in c you need to use array of chars to store each digits.

2 Comments

How is this helping him? This question is tagged C and not Java and he is specifically asking about help with a C project.
Thanks for the suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.