我正在尝试编写一个程序,使用指针和链表在C中添加两个多项式。这看起来像这样:
ideone.com上的实时代码。
1 #include<stdio.h>
2 #include<malloc.h>
3
4 typedef struct PolyTerm PolyTerm;
5
6 struct PolyTerm
7 {
8 int coeff;
9 int exp;
10 PolyTerm *next;
11 };
12
13 void printPoly(PolyTerm *pPoly)
14 {
15 if(pPoly == NULL)
16 {
17 printf("\n");
18 return;
19 }
20 printf("%de%d",pPoly->coeff,pPoly->exp);
21 if(pPoly->next != NULL)
22 printf(" + ");
23 printPoly(pPoly->next);
24 }
25
26 void printPolyTerm(PolyTerm *pPoly)
27 {
28 if(pPoly != NULL)
29 printf("%de%d",pPoly->coeff,pPoly->exp);
30 }
31
32 void createPolyTerm(int pCoeff, int pExp, PolyTerm **pPoly)
33 {
34 PolyTerm *tempPolyTerm;
35 if(*pPoly == NULL)
36 {
37 tempPolyTerm = (PolyTerm *)malloc(sizeof(PolyTerm));
38 tempPolyTerm -> coeff = pCoeff;
39 tempPolyTerm -> exp = pExp;
40
41 *pPoly = tempPolyTerm;
42 }
43 else
44 {
45 (*pPoly)->coeff = pCoeff;
46 (*pPoly)->exp = pExp;
47 (*pPoly)->next = (PolyTerm *)malloc(sizeof(PolyTerm));
48 (*pPoly)->next->next = NULL;
49 }
50 }
51
52 void addPoly(PolyTerm **pFirstPoly,PolyTerm **pSecondPoly, PolyTerm **sumPoly)
53 {
54 PolyTerm *sumPolyIterator = *sumPoly;
55 PolyTerm *sumPolyIteratorParent = *sumPoly;
56 PolyTerm *firstPolyCurrentTerm = *pFirstPoly;
57 PolyTerm *secondPolyIterator = *pSecondPoly;
58
59 while(firstPolyCurrentTerm != NULL)
60 {
61 //find if current exp exist in sumPoly
62 while(sumPolyIterator != NULL)
63 {
64 printf("-Sum: ");
65 printPoly(*sumPoly); //in last iteration, prints 6e0 + 9e1
66 printf("inner while 1\n");
67 printf("sumPolyIterator:%de%d\n",sumPolyIterator->coeff,sumPolyIterator->exp);
68 *sumPolyIteratorParent = *sumPolyIterator;
69 if(sumPolyIterator->exp == firstPolyCurrentTerm->exp)
70 break;
71 sumPolyIterator = sumPolyIterator->next;
72 printf("-Sum: ");
73 printPoly(*sumPoly); //in last iteration, prints 9e1, where did 6e0 went?
74 }
75
76 if(sumPolyIterator == NULL)
77 {
78 //if the exp is not present in sum yet, create it
79 sumPolyIterator = (PolyTerm *)malloc(sizeof(PolyTerm));
80 sumPolyIteratorParent->next = sumPolyIterator;
81 }
82
83 sumPolyIterator->exp += firstPolyCurrentTerm->exp;
84 sumPolyIterator->coeff = firstPolyCurrentTerm->coeff;
85
86 //iterate through second polynomial to find all terms with same exp
87 while(secondPolyIterator != NULL)
88 {
89 if(secondPolyIterator->exp == firstPolyCurrentTerm->exp)
90 {
91 sumPolyIterator->coeff += secondPolyIterator->coeff;
92 }
93 secondPolyIterator = secondPolyIterator->next;
94 }
95
96 //reset to first term
97 sumPolyIterator = *sumPoly;
98 secondPolyIterator = *pSecondPoly;
99
100 firstPolyCurrentTerm = firstPolyCurrentTerm->next;
101 }
102 }
103
104 void main()
105 {
106 PolyTerm *firstPolyTerm = NULL;
107 createPolyTerm(1,0,&firstPolyTerm);
108 createPolyTerm(2,1,&(firstPolyTerm->next));
109 createPolyTerm(3,2,&(firstPolyTerm->next->next));
110
111 PolyTerm *secondPolyTerm = NULL;
112 createPolyTerm(5,0,&secondPolyTerm);
113 createPolyTerm(7,1,&(secondPolyTerm->next));
114 createPolyTerm(9,2,&(secondPolyTerm->next->next));
115
116 PolyTerm *sumPolyFirstTerm = (PolyTerm *)malloc(sizeof(PolyTerm));
117 PolyTerm **sumPoly = &sumPolyFirstTerm;
118 addPoly(&firstPolyTerm,&secondPolyTerm,&sumPolyFirstTerm);
119
120 printPoly(firstPolyTerm);
121 printPoly(secondPolyTerm);
122 printPoly(sumPolyFirstTerm);
123 }
输出结果为:
-Sum: 0e0
inner while 1
sumPolyIterator:0e0
-Sum: 6e0
inner while 1
sumPolyIterator:6e0
-Sum: 6e0
-Sum: 6e0 + 9e1
inner while 1
sumPolyIterator:6e0
-Sum: 6e0 + 9e1
-Sum: 6e0 + 9e1
inner while 1
sumPolyIterator:9e1
-Sum: 9e1
1e0 + 2e1 + 3e2
5e0 + 7e1 + 9e2
9e1 + 12e2
两个多项式的总和1e0 + 2e1 + 3e2
和5e0 + 7e1 + 9e2
是6e0+9e1+12e2
。但是,在最后一行打印9e1 + 12e2
。似乎在某些指针赋值中丢失了第一项6e0
。所以我尝试使用前缀为printf
的{{1}}语句。正如您在输出中看到的那样,前缀为-Sum
的最后一行是-Sum
,而前缀为-Sum: 9e1
的第二行是-Sum
。所以这似乎是第一个词-Sum: 6e0 + 9e1
丢失的地方。我已经注释了代码,指出了这些6e0
前缀行的打印位置(-Sum
方法中的第一个内部while循环,第65行和第73行),但我无法理解为什么会发生这种情况。< / p>
答案 0 :(得分:0)
首先,由于使用了未初始化的数据,您的程序无法开箱即用。它偶然会在ideone上运行,但一般情况下它必须像在我的系统上那样失败。示例:在createPolyTerm
中创建tempPolyTerm
,其中字段next
根本没有初始化,因此它包含垃圾。但是,您继续,好像next
设置为NULL
。另一个例子:sumPolyFirstTerm
是通过使用malloc
分配缓冲区创建的,没有成员被初始化。但是,在addPoly
中,您将其传递到printPoly
期待NULL
字段中的next
。
如果您依赖缓冲区数据在分配时始终设置为零,请使用calloc而不是malloc
,至少这是让您的程序在我的系统上运行的方法。
好的,回到你的麻烦。看起来以下字符串包含错误
*sumPolyIteratorParent = *sumPolyIterator;
您可能希望将父迭代器指向基本迭代器的当前位置,而是使用解除引用和更新的PolyTerm本身而不是更改迭代器。有了这个替换程序看起来工作正常:
sumPolyIteratorParent = sumPolyIterator;