473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Adding Base-n's through strings (between base-2 and base-36)

1
I just wanted to add strings in any base form (example 1101+100 = 10001 in base-2 but it could be added using any base-form like in base-3 to base-36) and I'm having a big trouble with my code because it gave me incorrect results.

addition(char st[], char st2[], int base){
int i, j, carry = 0, ans, len, o=0, z=1, l=0;
char final[50];

if(strlen(st)>=strlen(st2))
len = strlen(st);
else
len = strlen(st2);

for(i=len; i>=0; i--){
if(isdigit(st[i])&&isdigit(st2[j])){ //if both addends are digits
ans = (st[i] - '0') + (st2[i] - '0') + carry;
}
else if(isdigit(st[i])&&isalpha(st2[j])){ //if first addend is a digit and second addend is an alphabet
ans = (st[i] - '0') + (st2[j] - 'A'+10) + carry;
}
else if(isalpha(st[i])&&isdigit(st2[j])){ //if first addend is an alphabet and second addend is a digit
ans = (st[i] - 'A'+10) + (st2[j] - '0') + carry;
}
else if(isalpha(st[i])&&isalpha(st2[j])){ //if both addends are alphabet
ans = (st[i] - 'A'+10) + (st2[j] - 'A'+10) + carry;
}

if(ans > base){
ans = ans - base;
carry = ans - base;
}
else if(ans == base){
ans = ans - base;
carry = 1;
}

if(ans >= 0 && ans <= 9)
final[i] = ans + 49;
else if(ans >= 65 && ans <= 90)
final[i] = ans + 65;

printf("%c", final[i]);
}
}

I really need help about this code. Thanks :-)
Feb 27 '15 #1
1 1272
weaknessforcats
9,208 Expert Mod 8TB
I would write functions that convert the string to a base 10 number. Add the numbers for each string. Then call a function to convert the base 10 number to a string in the correct base:
Expand|Select|Wrap|Line Numbers
  1. char str1[6] = "11101";
  2. char str2[7] = "000111";
  3. char str3[10];
  4.  
  5. int X = ConvertToInteger(str1, 2);
  6. int Y = ConvertToInteger(str2, 2);
  7.  
  8. int Sum = X + Y;
  9.  
  10. ConvertToString(Sum,str3,2);
  11.  
This approach will break your logic into chunks as opposed to a "strem of consciousness" if-else-if code.
Feb 27 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lydia Shawn | last post by:
hi there, i want to extract the numbers from this example input: bla trigger3 trigger4 trigger1 blabla trigger1 5000.00 trigger3 trigger1 trigger2 trigger2 600.00 trigger4 trigger1 50.00...
1
by: Philip | last post by:
I am trying to return a string from a mc++ method back to an invoking C# module. In the invoking C# module I have the string defined as.... String strString;.... in mc++ the method definition is...
1
by: Newbie | last post by:
Hi! How do I pass strings between webforms using Request in the target webform but without using Response.Redirect("SearchPOs.aspx?vendorName=" + vendorName); in the source webform. This is...
1
by: Rajesh | last post by:
Hi All, I'm having problems marshalling an array of strings between C# and C++ C# static extern void Func(out IntPtr num, ref IntPtr str); IntPtr strPtr = IntPtr.Zero;
4
by: Me | last post by:
how can you add a string aaaaaa at the beginning of a given string "string" and bbbbb at the end of it especially recursively ifthere are multiple "string"'s in a given text file ?
4
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
How can I send a string between C# to managed C++ and vice versa?
6
by: Alex | last post by:
Hi Everyone, I'm not completely new to CSS, but I've never written an entire site in CSS, which is what I'm doing now. I wrote our website many years ago using tables to format the page, but...
10
by: ptq2238 | last post by:
Hi, Tried this code to assist my understanding of strings and functions but I'm not sure why the errors are occurring and hope someone can shed some light to my learning. #include <stdio.h>...
1
by: jimmypringles | last post by:
Anyone knows how how to add "number" strings without converting them to integers? I came up with this function but it didn't work perfectly. e.g. I added "1998" and "8991" and the result was...
4
by: Warrick Wilson | last post by:
I'm new to C# and I'm trying to get some "toy" programs running to get familliar with it. So I want to make a slideshow program that will display one image after another, and have some sort of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.