Add two numbers as strings

Hi guys.I need to make a small program with a function with this prototype: void f(char *a,char *b) that adds two numbers represented as strings without using conversion operators or other tricks.Thanks
Last edited on
You will still use at least char → int conversion.
Is it an assigment where you should forrlow instructions no matter what ridiculous it is or you can choose easier way?
Like i said it doesnt matter what i use ..i just need to have a function like this in my program and to avoid conversion operators or something like that.I will upload my code as soon as i get home.
How should you retun the result?
And you cannot get away from conversions. You still have to use them in some variant.
Yes i must return the result in that function.Are you sure conversion is a must?It cant be done without it?
Last edited on
From your description it is not clear where the result of the addition will be stored. It can be stored either in the string pointed by the first parameter or in the string pointed by the second parameter. Also it is not clear what to do if there will be an overflow. And at last the third question is how the numbers are stored. Whether the least significant digit is the first character in a string or is the last character in a string.
It cant be done without it?
Nope. Even to add two digits represented by characters ('5' + '3') you should convert them to numbers first and then conceert result to char.
How should you retun the result?
Yes i must return the result in that function
"Yes" is not a valid answer to question startinf with "How".

vlad from moscow summarized other questions you should answer before you start to create your dunction.
Well firstly it doesnt matter about overflow cuz it wont be large strings.Secondly the numbers are stored like this:the least significant digit is the last character in the string.And thirdly it doesnt matter where the addition will be stored i just need to see it on console.
what is your problem with std::cout << (atoi(a) + atoi(b));?
Well it would be easy to solve this with atoi but like i said someone asked me to solve this without that.
- Or you could try to cast the variables if you want.
ok, hint:
you should get two characters representing buts of same signifience achar and bchar
then:
1
2
3
4
5
6
7
8
9
10
11
12
//int result
//int factor;
//int overflow;
int x = char_to_int(achar) + char_to_int(bchar) + overflow;
overflow = (x>9)?1:0;
x %= 10;
    //add digit to result. If result is integer:
    //result = result + x*factor;
    //factor *= 10;
    //If it is string: char n = int_to_char(x);
    //Push character into string somehow
//move to pair of more significant characters. 

You should define function char_to_int (and possibly int_to_char)
Even if you will not define them as functions and will do manipulations with it in body of your function: you will still use conversions. You cannot go away from char → int conversion.
without using conversion operators or other tricks

Do mean without using a standard conversion function? ie you can convert the strings to ints yourself.

Or do you have to do without even converting the string to an int in your own code? Then MiiNiPaa's kind of solution would be the way to go.

Andy

PS @MiiNiPaa

You could avoid conversion if you were "keen" (stupid?) enough:

1
2
3
4
5
6
7
8
9
10
11
12
13
rchar = '0'; // result char
ochar = '0'; // overflow char
switch(achar) {
    case '0':
        switch(bchar) {
            case '0': rchar = '0'; break;
            case '1': rchar = '1'; break;
            // etc, etc, etc
    case '9':
        switch(bchar) {
            case '0': rchar = '9'; break;
            case '1': rchar = '0'; ochar = '1'; break;
            // etc, etc, etc 


With 100 case statements in all, plus another switch to deal with overflow, etc...
Last edited on
You could avoid conversion if you were "keen" (stupid?) enough
You can even create a lookup table for this but I think it is too esoteric...

Look at this!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>

void f(char* a, char* b)
{
    std::stringstream x;
    x << a << ' ' << b;
    std::cout << std::accumulate(std::istream_iterator<int>(x),
                                 std::istream_iterator<int>(), 0);
}

int main()
{
    char a[20];
    char b[20];

    std::cin.getline(a, 19);
    std::cin.getline(b, 19);

    f(a, b);
}
Last edited on
@MiiNiPaa

Looks a bit "tricky" (i.e. trick like)...

Andy

PS OK, it's just a toy sample, but...

Why new just 20 chars? (without deleting them...) Such small amounts of memory are better allocated on the stack, for routine (or illustrative) purposes?

And why 19 rather than 20 for getline? getline will deal with the null terminator correctly if you use this:

1
2
    char a[20] = "";         // be neurotic and zero buffer
    std::cin.getline(a, 20); // or even sizeof(a)/sizeof(char) 


As just ask as I'm seeing this unnecessary subtraction of one all over the place, even though the cplusplus.com tutorial shows otherwise.
http://www.cplusplus.com/reference/istream/istream/getline/

Is this a hangover from some other call?

Need to set a good example!


Last edited on
andywestken wrote:
And why 19 rather than 20 for getline?
Because that is how strncpy/strncat works (and possibly other c-string manipulation functions).
1
2
3
   char x[20];
   std::strncpy(x, "123456789012345678901234567890", 20);
   std::cout << x;
12345678901234567890
//20 symbols. That means \0 is 21st character
//and there is buffer overflow
I did not know that istream::getline() function works differently.

Ponters, dynamic memory allocation and absense of delete is because I played with it and delete got trimmed. Will change it to arrays now.
Last edited on
So it cant be done without using atoi ,parseint ,accumulate etc..and just using fundamental types and primary instructions?
It can be, but in process you will create your own version of atoi which will probably be less optimal and more error-prone than standard one. So why reinvent the wheel?

https://www.student.cs.uwaterloo.ca/~cs350/common/os161-src-html/atoi_8c-source.html
http://research.microsoft.com/en-us/um/redmond/projects/invisible/src/crt/atoi.c.htm
Two different versions of atoi()
Topic archived. No new replies allowed.