[英]How do I add an array of polynomials using pointers and a structure?
我是c語言的初學者,我的目標是添加一個多項式數組,並且在接下來的幾周里,我確實在努力使此代碼正常運行,但是由於我收到了有關該錯誤的事實,因此出現了錯誤。結果中出現“未處理的異常”消息。 我已經在這個論壇上關注了幾個月,我要感謝您就問題提出的所有答案,它們對我有很大幫助。
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()
一起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
}
您的add()
返回一個局部變量! 這將超出函數終止的范圍,因此print()
的參數是垃圾。
polinom addn(sir s, int n)
{
int grad, i, j, k;
polinom result, aux;
...
return result;
}
在main()
有print(addn(s,n));
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.