Ordinal value

I tried to make a program for counting "big number". For example numbers which contain like 50 or 100 digits. I decided to put those numbers in strings and use classic algorithm, which I learned at primary school in first grade.
e.g.
1
2
3
4
  12364639 // number 1
+    27452 // number 2
__________
  12392091 // outcome 

So in this case string seems ideal to me.

And my problem is, that I have to count the digits together one by one. And each digit is type char as I know. So how can I count together for example '1' + '2' and get '3'?
There are libraries for that: http://gmplib.org/

If you really just want to tackle the problem, one (possibly naïve, I've never done this) solution would be to convert them like this:

1
2
3
4
5
int intFromChar(char ch)
{
    // make sure ch is a digit
    return ch - '0';
}

This relies on the fact that character sets assign numbers continuous values from 0 to 9, so, whatever these values are, the difference from whatever number to 0 is always its own mathematical value.
Last edited on
Create a function to convert them to ints
1
2
3
4
5
int char2int(char c){
    switch(c){
        case '0': return 0;
        case '1': return 1;
      --etc--

and the opposite to turn the sum back into a char. Don't forget to minus 10 if the sum is >= 10, then add a 1 to the next digit.
I just tried filipe's function; it works and is much simpler than what I suggested.
Yup, thanks.
Topic archived. No new replies allowed.