开发者

Need code for addition of 2 numbers

开发者 https://www.devze.com 2023-01-22 04:52 出处:网络
I am having the numbers follows taken as strings My actual number is 1234567890123456789 from this i have to separate it as s=12 s1=6789 s3=3456789012345

I am having the numbers follows taken as strings

My actual number is 1234567890123456789

from this i have to separate it as s=12 s1=6789 s3=3456789012345

remaining as i said

I would like to add as fo开发者_如何学Cllows

11+3, 2+4, 6+5, 7+6, 8+7, 9+8 such that the output should be as follows

4613579012345

Any help please


    public static string CombineNumbers(string number1, string number2)
    {
        int length = number1.Length > number2.Length ? number1.Length : number2.Length;
        string returnValue = string.Empty;

        for (int i = 0; i < length; i++)
        {
            int n1 = i >= number1.Length ? 0 : int.Parse(number1.Substring(i,1));
            int n2 = i >= number2.Length ? 0 : int.Parse(number2.Substring(i,1));
            int sum = n1 + n2;

            returnValue += sum < 10 ? sum : sum - 10;
        }
        return returnValue;
    }


This sounds an awful lot like a homework problem, so I'm not giving code. Just think about what you need to do. You are saying that you need to take the first character off the front of two strings, parse them to ints, and add them together. Finally, take the result of the addition and append them to the end of a new string. If you write code that follows that path, it should work out fine.

EDIT: As Ralph pointed out, you'll also need to check for overflows. I didn't notice that when I started typing. Although, that shouldn't be too hard, since you're starting with a two one digit numbers. If the number is greater than 9, then you can just subtract 10 to bring it down to the proper one digit number.


How about this LINQish solution:

private string SumIt(string first, string second)
{
    IEnumerable<char> left = first;
    IEnumerable<char> right = second;

    var sb = new StringBuilder();

    var query = left.Zip(right, (l, r) => new { Left = l, Right = r })
                    .Select(chars => new { Left = int.Parse(chars.Left.ToString()),
                                           Right = int.Parse(chars.Right.ToString()) })
                    .Select(numbers => (numbers.Left + numbers.Right) % 10);

    foreach (var number in query)
    {
        sb.Append(number);
    }
    return sb.ToString();
}


Tried something:

public static string NumAdd(int iOne, int iTwo)
    {
        char[] strOne = iOne.ToString().ToCharArray();
        char[] strTwo = iTwo.ToString().ToCharArray();

        string strReturn = string.Empty;

        for (int i = 0; i < strOne.Length; i++)
        {
            int iFirst = 0;
            if (int.TryParse(strOne[i].ToString(), out iFirst))
            {
                int iSecond = 0;
                if (int.TryParse(strTwo[i].ToString(), out iSecond))
                {
                    strReturn += ((int)(iFirst + iSecond)).ToString();
                }
            }

            // last one, add the remaining string
            if (i + 1 == strOne.Length)
            {
                strReturn += iTwo.ToString().Substring(i+1); 
                break;
            }
        }

        return strReturn;
    }

You should call it like this:

string strBla = NumAdd(12345, 123456789);

This function works only if the first number is smaller than the second one. But this will help you to know how it is about.


In other words, you want to add two numbers treating the lesser number like it had zeroes to its right until it had the same amount of digits as the greater number.

Sounds like the problem at this point is simply a matter of finding out how much you need to multiply the smaller number by in order to reach the number of digits of the larger number.

0

精彩评论

暂无评论...
验证码 换一张
取 消