两个大数相加比较符合自然运算的算法

功能很简单,就是实现两个很长的大数相加的功能
 1public static string MaxNumAdd(string num1, string num2)
 2        {
 3            //初始设置进位为0
 4            int carry = 0;
 5            int length1 = num1.Length;
 6            int length2 = num2.Length;
 7            Stack<int> stack1 = new Stack<int>(length1);
 8            Stack<int> stack2 = new Stack<int>(length2);
 9            int max = length1;
10            if (max < length2)
11            {
12                max = length2;
13            }

14            //用于存放结果
15            Stack<int> stack3 = new Stack<int>(max + 1);
16            int size = max - length1;
17            while (size > 0)
18            {
19                stack1.Push(0);
20                size--;
21            }

22            size = max - length2;
23            while (size > 0)
24            {
25                stack2.Push(0);
26                size--;
27            }

28            for (int i = 0; i < length1; i++)
29            {
30               
31                int num = Convert.ToInt32(num1[i].ToString());
32                stack1.Push(num);
33            }

34            for (int i = 0; i < length2; i++)
35            {
36               
37                int num = Convert.ToInt32(num2[i].ToString());      
38                stack2.Push(num);
39            }

40            //临时施计算结果
41            int tempResult = 0;
42            for (int i = 0; i < max; i++)
43            {
44                int n1 = stack1.Pop();
45                int n2 = stack2.Pop();
46                tempResult = n1 + n2 + carry;
47                stack3.Push(tempResult);
48                if (tempResult > 9)
49                {
50                    carry = 1;
51                }

52                else
53                {
54                    carry = 0;
55                }

56            }

57            stack3.Push(carry);
58            StringBuilder sb = new StringBuilder();
59            while (stack3.Count > 0)
60            {
61                sb.Append(stack3.Pop().ToString());
62            }

63            return sb.ToString().TrimStart('0');
64        }

65    }
作者:jillzhang
出处:http://jillzhang.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2006-10-22 02:06 Robin Zhang 阅读(2621) 评论(6) 编辑 收藏

 回复 引用   
#1楼 2006-10-22 08:52 DDV[未注册用户]
值得学习!
 回复 引用   
#2楼 2006-10-22 13:42 yuchen[未注册用户]
int n1 = stack1.Pop();
45 int n2 = stack2.Pop();
46 tempResult = n1 + n2 + carry;
47 stack3.Push(tempResult);
48 if (tempResult > 9)
49 {
50 carry = 1;
51 }
52 else
53 {
54 carry = 0;
55 }
老大这里算法有些失误啊

 回复 引用   
#3楼 2006-10-22 13:56 yuchen[未注册用户]
int tempResult = 0;
for (int i = 0; i < max; i++)
{
int n1 = stack1.Pop();
int n2 = stack2.Pop();
tempResult = n1 + n2 + carry;
if (tempResult > 9)
{
carry = 1;
}
else
{
carry = 0;
}

if (tempResult > 9)
{
tempResult = tempResult - 10;
stack3.Push(tempResult);
}
else
{
stack3.Push(tempResult);
}


}

 回复 引用   
#4楼 2006-10-22 13:57 yuchen[未注册用户]
int tempResult = 0;
for (int i = 0; i < max; i++)
{
int n1 = stack1.Pop();
int n2 = stack2.Pop();
tempResult = n1 + n2 + carry;
if (tempResult > 9)
{
carry = 1;
}
else
{
carry = 0;
}

if (tempResult > 9)
{
tempResult = tempResult - 10;
stack3.Push(tempResult);
}
else
{
stack3.Push(tempResult);
}


}

 回复 引用   
#5楼 2008-02-02 11:55 thf[未注册用户]
int size = max - length1;
17 while (size > 0)
18 {
19 stack1.Push(0);
20 size--;
21 }
22 size = max - length2;
23 while (size > 0)
24 {
25 stack2.Push(0);
26 size--;
27 }
这段代码可以省略