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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

发表评论
值得学习!
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 }
老大这里算法有些失误啊
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 }
老大这里算法有些失误啊
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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 }
这段代码可以省略
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 }
这段代码可以省略