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

功能很简单,就是实现两个很长的大数相加的功能
 1 public   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     }

 

  回复  引用    
#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 }
这段代码可以省略
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值