how do i create a function that adds the contents of 2 strings of numbers, only n8umbers that does not use + or - or atoi function or of the like
add_strings("123", "66")
would return "189"
how do i create a function that adds the contents of 2 strings of numbers, only n8umbers that does not use + or - or atoi function or of the like
add_strings("123", "66")
would return "189"
your saying you cant use a standard library function that would convert a string to an int? if so then your question is basically 'how do i write atoi?'. if this is the case, then try it out on paper how you would do it (ie without 'programming' or using a computer), and post your attempts or thoughts.
make a db of pre determined additions of x and y add[10][10] = ('0', '1', '2' etc)
and than another db of carries
Last edited by qubit67; 04-22-2007 at 12:23 AM.
what? if you want help please be more elaborate
first, answer the question from my first post: you have to do this without functions such as 'atoi' right? and your edit didnt make it much more clear or elaborate! please dont use shortforms and explain what you are saying.
It seems like you already know the answer, and just looking for someone else to do the work.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
it sound exctly like my hw..
but here is the answer.
Code:/*************************************************************************** * How to add two postive numbers? * * Sun Apr 22 09:25:07 2007 * Jabka Atu * ****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include <iostream> using namespace std; char* add_two_postive_numbers(char *first_value,char *second_value){ char *result=NULL; char i=0; char ans='0'; char carry=0; char size_of_first = strlen(first_value); char size_of_second = strlen(second_value); char biggest_size = (size_of_first > size_of_second) ? size_of_first : size_of_second; char smallest_size = (size_of_first < size_of_second) ? size_of_first : size_of_second; char delta = size_of_first - size_of_second; if (size_of_first < size_of_second) { result=add_two_postive_numbers(second_value,first_value); return result; } result=new char[biggest_size + 2];//one more for '\0' and for extra 1 result[biggest_size + 1] = '\0'; result[0] = '0'; for (i=0;i<size_of_second;i++) { ans=first_value[size_of_first - i-1] + second_value[size_of_second-i-1] + carry -'0'; carry=0; if (ans > '9') { carry = 1; result[biggest_size - i]= ans - 10; } else { result[biggest_size - i] = ans; } } if(!carry) //since there is an option that there //will be no borrow i will cp it. { for(i = 0;i < delta; i++) { result[biggest_size - size_of_second - i] = first_value[size_of_first - size_of_second - i- 1]; } } if (carry) { for (i = 0;i < (delta) ;i++) { ans=first_value[size_of_first - size_of_second - i - 1] + carry; carry = 0; if (ans > '9') { carry = 1; result[biggest_size-size_of_second - i] = ans - 10 ; } else { result[biggest_size - size_of_second - i] = ans ; } }//end of for if (carry) { result[0]='1'; } }//end of if (exsitence of an carry return result; }
Last edited by jabka; 04-22-2007 at 12:32 AM. Reason: spelling errors ...
the first thing you need is to create a function that returns the integer value of a char:once you have this we can work out the next step, which would be to figure out how to convert between your two 'languages' (ie string and integers).Code:int char2int(char c);
some examples (note that you would already have a way to convert a char to an int, so everything is represented using ints):
1 = 10^0 * 1
2 = 10 ^0 * 2
10 = 10^1 * 1
15 = 10^1 * 1 + 10^0 *5
Last edited by nadroj; 04-22-2007 at 12:54 AM.
@jabka Please don't post stuff like that, He has to do the work, it's his homework.
Also you've posted C++ code in the C section...
Hope is the first step on the road to disappointment.
i know they have a character value, and im not trying to overcomplicate things. what if the character is 'z' this would screw up though, right?
That's what error checking is for. You could always just use isdigit.Code:signed int x = 'z' - '0'; if( x < 0 || x > 9 ) puts( "You didn't enter a digit." );
Quzah.
Last edited by quzah; 04-22-2007 at 01:16 AM.
Hope is the first step on the road to disappointment.
im assuming he cant use functions like that, but i was still waiting for the answer from my previous post.
You don't need a function for it. Just basic subtraction, as I mentioned. Actually, you don't even need subtraction for digit testing. All you have to do is compare it to the range of zero to nine as a character.
Quzah.
Hope is the first step on the road to disappointment.
ok sounds fair. either way would work. i just never thought about the ascii characters.
edit:something like this?:All you have to do is compare it to the range of zero to nine as a character.if thats the case, then thats exactly what i was thinking of to make a function. or you could just do it in the loop that hell need in the final product, but id just like to seperate this.Code:if (myChar == '1') myDigit = 1; //etc
Last edited by nadroj; 04-22-2007 at 01:25 AM.