我的代码旨在两个创建多项式,然后对其进行加,减或乘。当我运行任何一个选项时,它都会创建两个多项式,打印出第一个多项式,然后出现分段错误。它似乎发生在show(polys1)和printf语句之间,我不知道为什么。任何帮助将不胜感激。我花了一段时间阅读类似的问题,但找不到任何可以解释我的程序的问题。
多聚体
#if ! defined(POLY_H)
#define POLY_H
struct link{
int *coeff;
int pow;
int degree;
}
llink;
void create(struct link *node);
void polyadd(struct link *poly1,struct link *poly2,struct link *poly);
void polysub(struct link *poly1,struct link *poly2,struct link *poly);
void polymul(struct link *n1, struct link *n2, struct link *n);
void show(struct link *node);
#endif
polyMain.c
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#include "poly.h"
struct link *poly1s=NULL,*poly2s=NULL,*polys=NULL;
int main()
{
int op;
char ch;
do{
poly1s=(struct link *)malloc(sizeof(struct link));
poly2s=(struct link *)malloc(sizeof(struct link));
polys=(struct link *)malloc(sizeof(struct link));
printf("\n\nWhat do you want to do?\n1.Addition\n2.Subtraction\n3.Multiplication\n0.Exit\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n\nenter 1st polynomial:");
create(poly1s);
printf("\n\nenter 2nd polynomial:");
create(poly2s);
printf("\n1st Polynomial:\t");
show(poly1s);
printf("\n2nd Polynomial:\t");
show(poly2s);
polyadd(poly1s,poly2s,polys);
printf("\nAdded polynomial:\t");
show(polys);
break;
case 2:
printf("\n\nenter 1st polynomial:\t");
create(poly1s);
printf("\n\nenter 2nd polynomial:\t");
create(poly2s);
printf("\n\n1st Polynomial:\t");
show(poly1s);
printf("\n\n2nd Polynomial:\t");
show(poly2s);
polysub(poly1s,poly2s,polys);
printf("\n\nSubtracted polynomial:\t");
show(polys);
break;
case 3:
printf("\n\nenter 1st polynomial:");
create(poly1s);
printf("\n\nenter 2nd polynomial:");
create(poly2s);
printf("\n\n1st Polynomial:\t");
show(poly1s);
printf("\n\n2nd Polynomial:\t");
show(poly2s);
polymul(poly1s,poly2s,polys);
printf("\n\nMultiplied polynomial:\t");
show(polys);
break;
}
printf("\n Want to continue? Y/N:");
ch=getchar();
}
while(op);
return 0;
}
多聚体
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#include "poly.h"
void create(struct link *node)
{
printf("\n\nenter the degree of the polynomial:");
scanf("%d",&node->degree);
node->coeff=malloc((node->degree)*sizeof(int));
int i;
for(i = 0;i<node->degree;i++)
{
printf("\n\nenter coeff:");
scanf("%d",&node->coeff[i]);
}
printf("\nenter power:");
scanf("%d",&node->pow);
}
void show(struct link *node)
{
int i;
printf("%dx^%d",node->coeff[0],node->pow);
for(i = 1;i<node->degree;i++)
printf(" + %dx^%d",node->coeff[i],node->pow);
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
int i;
for(i = 0;i<poly1->degree;i++)
poly->coeff[i]=poly1->coeff[i];
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
int i;
for(i = 0;i<poly2->degree;i++)
poly->coeff[i]=poly2->coeff[i];
}
else
{
poly->pow=poly1->pow;
int j = poly1->degree-poly2->degree;
int k;
if(j > 0)
k = poly1->degree;
else k = poly2->degree;
int i;
for(i = 0;i<k;i++)
{
if(i<poly1->degree && i<poly2->degree)
poly->coeff[i]=poly1->coeff[i]+poly2->coeff[i];
else if(i<poly1->degree)
poly->coeff[i]=poly1->coeff[i];
else if(i<poly2->degree)
poly->coeff[i]=poly2->coeff[i];
}
}
}
void polysub(struct link *poly1,struct link *poly2,struct link *poly)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
int i;
for(i = 0;i<poly1->degree;i++)
poly->coeff[i]=poly1->coeff[i];
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
int i;
for(i = 0;i<poly2->degree;i++)
poly->coeff[i]=poly2->coeff[i];
}
else
{
poly->pow=poly1->pow;
int j = poly1->degree-poly2->degree;
int k;
if(j > 0)
k = poly1->degree;
else k = poly2->degree;
int i;
for(i = 0;i<k;i++)
{
if(i<poly1->degree && i<poly2->degree)
poly->coeff[i]=poly1->coeff[i]-poly2->coeff[i];
else if(i<poly1->degree)
poly->coeff[i]=poly1->coeff[i];
else if(i<poly2->degree)
poly->coeff[i]=poly2->coeff[i];
}
}
}
void polymul(struct link *n1, struct link *n2, struct link *n)
{
struct link * n2beg=n2;
while (n1)
{
struct link * temp=(struct link *)malloc(sizeof(struct link));
n2=n2beg;
while (n2)
{
int j = n1->degree-n2->degree;
int k;
if(j > 0)
k = n1->degree;
else k = n2->degree;
int i;
for(i = 0;i<k;i++)
{
if(i<n1->degree && i<n2->degree)
temp->coeff[i] = n1->coeff[i] * n2->coeff[i];
else if(i<n1->degree)
temp->coeff[i]=n1->coeff[i];
else if(i<n2->degree)
temp->coeff[i]=n2->coeff[i];
}
temp->pow = n1->pow + n2->pow;
}
polyadd(temp,n,n);
free(temp);
}
}
最佳答案
我认为出现问题是因为在使用前您尚未为coeff
的polys
分配任何内存
poly->coeff[i]= /*something*/
我尚未检查您的整个代码。所以我不知道你想做什么。在使用
malloc
的coeff
之前,尝试使用polys
分配一些内存。