gpt4 book ai didi

c++ - 如何使用指针和结构体添加多项式数组?

转载 作者:行者123 更新时间:2023-11-30 17:25:43 26 4
gpt4 key购买 nike

我是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++ 编译,因为 coutcin 是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com