- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是c初学者,我的目标是添加一个多项式数组,几周来我真的很努力让这段代码正常运行,不幸的是,由于我收到了一个错误,所以出现了错误结果的加法函数中出现错误,并显示消息“Unhanded exception”。我已经关注这个论坛几个月了,我要感谢你们对问题给出的所有答案,他们对我帮助很大。
typedef struct
{
int* coef;
int exp;
}polinom;
typedef polinom* sir;
void read(polinom*,char); //read a polynom
void print(polinom,char*); //print a polynom
polinom addn(sir, int );
void read(polinom* p)
{
cout<<"Give the polynom "<<endl;
cout<<"\tgrad if polynom: ";
cin>>p->exp;
cout<<"\tGive coef:\n";
p->coef=(int*)malloc(sizeof(int)*((p->exp)+1));
for(int i=0;i<=p->exp;i++)
{
cout<<"\t\tcoef ["<<i+1<<"]= ";
cin>>p->coef[i];
}
}
void readn(sir r,int n)
{
for(int i=0;i<n;i++)
{
cout<<"Polynom ["<<i+1<<"]:\n";
read(&r[i]);
}
}
void print(polinom p)
{
for(int i=p.exp; i>=0; i--)
{
cout<<p.coef[i]<<'^'<<i<<' ';
}
cout<<endl;
}
void printn(sir r, int n)
{
cout<<"print a polynom\n";
for(int i=0; i<n; i++)
{
print(r[i]);
}
}
void main()
{
int n;
cout<<"Give a number of polynomials: \n";
cin>>n;
sir s;
s=(polinom*)malloc(sizeof(polinom)*n);
readn(s,n);
printn(s,n);
printf("here is the addition \n");
print(addn(s,n));
system("pause");
}
polinom addn(sir s, int n)//s array of polynomial and n is the number of them
{
int grad,i,j,k;
polinom result, aux;
for(i=0; i<10; i++)
{
aux.coef=0;
result.coef=0;
}//try to initialize
for(i=0; i<n; i++)
{
if(s[i].exp ==s[i+1].exp)//<---------------------x==y
{
for(j=0; j < s[i].exp; j++)
result.coef[j]=s[i].coef[j] + s[i+1].coef[j];
grad=s[i].exp;
}// end of if
if(s[i].exp > s[i+1].exp)//<---------------------x>y
{
for(j=0; j < s[i].exp; j++)
{
if(i >= s[i].exp-s[i+1].exp)
{
result.coef[j]=s[i].coef[j] + s[i+1].coef[j];
j++;
}// end of if
else
{
result.coef[j] = s[i].coef[j];
}// end of else
}
grad=s[i].exp;
}// end of if
if(s[i+1].exp < s[i].exp)//<---------------------y<x
{
for(j=0; j < s[i].exp; j++)
{
if(i >= s[i+1].exp-s[i].exp)
{
result.coef[j]=s[i].coef[j] + s[i+1].coef[j];
j++;
}// end of if
else
{
result.coef[j]=s[i].coef[j];
}// end of else
}
grad=s[i].exp;
}// end of if
if(i == n-1)//<---------------------only additional
{
result.exp=grad;
for(int k = 0; k < result.exp; k++)
{
aux.coef[k]=0;
result.coef[k] = result.coef[k] + aux.coef[k];
}
}
}
return result;
}
最佳答案
您的代码有很多问题。我建议停止编写代码并开始重新阅读一下,然后重新开始调试。
<小时/>你说你想要 C,但这段代码只能用 C++ 编译,因为 cout
和 cin
是 C++ 特性。另一方面,您使用类似 C 的东西,例如 malloc()
(它应该与 free()
一起提供,而在 C++ 中应该使用 new
(应与 delete
一起提供)。
这里有一些注释,作为开始。
你的main()
应该看起来像这样:
int main() { // main should return an int, not void
int n;
cout << "Give a number of polynomials: \n";
cin >> n;
sir s;
s = (polinom*) malloc(sizeof(polinom) * n);
readn(s, n);
printn(s, n);
printf("here is the addition \n");
print(addn(s, n));
free(s); // don't forget to free your memory
// system("pause"); Don't use this
return 0; // return success code
}
system(“pause”); - Why is it wrong?
<小时/>你的add()
返回一个局部变量!!!!!这将超出函数终止的范围,因此 print()
的参数是垃圾。
polinom addn(sir s, int n)
{
int grad, i, j, k;
polinom result, aux;
...
return result;
}
在main()
中你有print(addn(s,n));
。
关于c++ - 如何使用指针和结构体添加多项式数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001427/
所以我想创建一个程序,当用户输入值 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
我是一名优秀的程序员,十分优秀!