- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序,使用指针和链表在 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
语句放在前缀 -Sum
中。正如您在输出中看到的,以 -Sum
为前缀的最后一行是 -Sum: 9e1
,而以 -Sum
为前缀的倒数第二行> 是 -Sum: 6e0 + 9e1
。所以这似乎是第一个术语 6e0
丢失的地方。我已经对代码进行了注释,指出这些 -Sum
前缀行的打印位置(它在 addPoly()
方法中的第一个内部 while 循环,第 65 行和 73 行),但我是无法理解为什么会发生这种情况。
最佳答案
首先,由于使用了未初始化的数据,您的程序无法开箱即用。它偶然在 ideone 上工作,但在一般情况下它一定会像在我的系统上那样失败。示例:在 createPolyTerm
中,您创建了 tempPolyTerm
,其中字段 next
根本没有初始化,因此它包含垃圾。但是,您将继续,就像将 next
设置为 NULL
一样。又如:sumPolyFirstTerm
是通过malloc
分配缓冲区创建的,没有初始化任何成员。但是,在 addPoly
中,您将其传递到 printPoly
中,期望在 next
字段中出现 NULL
。
如果您依赖于分配时缓冲区数据始终设置为零,请使用 calloc而不是 malloc
,至少这是让你的程序在我的系统上运行的方法。
好吧,回到你的麻烦。看起来以下字符串包含错误
*sumPolyIteratorParent = *sumPolyIterator;
您可能希望将父迭代器指向基迭代器的当前位置,但您使用了取消引用并更新了 PolyTerm 本身,而不是更改迭代器。使用这个替换程序看起来工作正常:
sumPolyIteratorParent = sumPolyIterator;
关于c - 使用基于指针的链表计算多项式和时指针分配中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42694076/
所以我想创建一个程序,当用户输入值 c 且 a = 1 时,打印出可因式分解的二次方程。程序应确定 b 的所有可能的整数值,以便三项式以 x^2 + bx + c 的形式打印出来 一个例子是,如果用户
我有自己定义的多项式类,它是系数列表的形式。 有点像 axˆ2 + bx + c is equals to [c, b, a] (for ax + b == [b, a] similarly, for
我必须制作一个对多项式执行运算的 GUI,但我不断收到无法摆脱的 NullPointerExceptions。在输出上它没有显示任何内容。我尝试调试我的程序,据我所知,我从键盘插入的多项式在某种程度上
numpy.lib.polynomial.polyval 允许您使用另一个多项式评估多项式: numpy.polyval(poly1d([1, 2, 3]), 2) Out[832]: 11 nump
如果我想计算多项式,如何在 C 中定义具有可变数量参数的函数?我的函数必须有这个参数:第一个参数:float x,第二个:int n,其余的 float (系数)。非常感谢! 最佳答案 用 varia
我正在尝试求多项式的不定积分,但是我的数学和编码都不是很好。我的代码可以编译,但我相信我的公式有误: Polynomial Polynomial :: indefiniteIntegral() co
我有 3 个数据集。 2 表示多项式本身(我们称它们为 x 和 y),1 表示函数值(它将是 z)。 多项式看起来像这样(假设两个维度的幂都是 3): z = a00 + a01*x + a02*x^
如何在 python 中计算最佳拟合线,然后将其绘制在 matplotlib 中的散点图上? 我使用普通最小二乘回归计算线性最佳拟合线如下: from sklearn import linear_mo
我正在尝试分解 bool 多项式以获得逻辑网络的最小形式。我的变量是 a1、a2、a3 ... 以及负对应项 na1、na2、na3 ... 如果需要一个函数 f = a1*a2*b2*nb1 + a
长话短说 如何使用系数数组构建表达式并将其转换为 Func ?有没有比表达式树更好的方法? 我有一个使用 Func formula 构造的不可变序列类型用于为序列 A 生成术语 An。我开始构建一个辅
我在我的 Mac OS Sierra 上运行 Spark 2.1.1(这应该有帮助)。我尝试在网上找到的测试数据集上拟合多项式逻辑回归,我在此处报告前几行(我不知道如何在此处附加文件): 1,0,24
我必须构建一个从类 lista(列表)继承的类多项式(polinom)。我必须从多项式类中加、减、乘、除 2 个对象。我有这段代码。我不明白为什么我的析构函数不工作。我还必须重载运算符:+、-、> 但
我有一个 Polynomial类,我正在尝试定义 operator++ ,递增前和递增后,以及尝试定义递减前和递减后,即 operator-- .这是我的代码片段: class Polynomial
我是编程新手(Python 是我的第一语言),但我喜欢设计算法。我目前正在研究方程组(整数),但找不到任何解决我的特定问题的引用。 让我解释一下。 我有一个等式(一个测试,如果你愿意的话): raw_
我正在尝试使用 scipy.stats (python) 中的 multinominal.pmf 函数。 当我在输入中所有概率都大于零的情况下使用此函数时,它工作正常。问题是当我想在其中一个概率为零时
我想用 0xA001 多项式计算字节数组的 CRC-16 校验和。但我真的不知道如何在 Java 中做到这一点,以及如何使用给定的多项式。它是某种特殊值(0xA001)吗?你能告诉我一个可以为我计算校
由于我的分类器在测试数据上产生了大约 99% 的准确率,我有点怀疑并想深入了解我的 NB 分类器最有用的特征,看看它正在学习什么样的特征。以下主题非常有用:How to get most inform
如 McFadden (1978)表明,如果多项 logit 模型中的备选方案数量大到无法计算,则通过对备选方案进行随机子集来获得一致估计仍然是可行的,因此每个个体的估计概率基于所选备选方案和 C其他
我现在有一些离散点,我使用 scipy.interpolate.splprep () 函数(B 样条插值)对其进行插值,以获得令人满意的平滑曲线。这是代码(借鉴另一个问题的答案)和我得到的结果。 im
我在 IPython notebook 中有一些多项式 x: import numpy as np x = np.polynomial.polynomial.Polynomial([1,2,3]) x
我是一名优秀的程序员,十分优秀!