Data structure to store and add arbitrarily large number
Hi folks, I decided to play around with some of the silliness known as technical questions. At one time obtaining a University Degree was sufficient proof – until grade standards started diving to 20,000 leagues under the sea. In general, there are many right answers.
a) Design a data structure to store an arbitrarily large number
b) How will you use it to store two such number and add them.
c) Write down the class (code) for the data structure
We assume integers and not negative.
If it is arbitrary real then use:
This is an all in one function that could be decomposed..
- Take the string, break into characters, then convert to integer.
- Conversion was done with an old fashion C approach
- Add the integers (doing a carryover)
- push the result into a new stack
- convert the stack to a character string
- return the string
To test the code:
- You can check (and discard) comma(,) and other symbols ($) that have no numeric consequences.
- With the +/- real case, you can do multiple tests for negative (i.e. (1234), or 1234- or –1234) giving robustness.
- You can even work with complex (real and imaginaries)