我的程序正在产生分段错误,我无法弄清楚原因

时间:2014-11-30 14:45:38

标签: c segmentation-fault runtime-error polynomial-math

我的代码意味着两个创建多项式,并添加,减去或相乘它们。当我运行其中任何一个选项时,它会创建两个多项式,打印出第一个多项式,然后出现分段错误。它似乎发生在show(polys1)和printf语句之间,我无法弄清楚原因。任何帮助将不胜感激。我花了一段时间阅读类似的问题,但找不到任何为我的程序解释的。

poly.h

#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;
}

poly.c

#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);
            }
}

1 个答案:

答案 0 :(得分:0)

我认为问题的出现是因为在使用

之前您尚未为coeff polys分配任何内存
poly->coeff[i]= /*something*/

我还没有检查过你的整个代码。所以我不知道你想要做什么。在使用malloc的{​​{1}}之前,尝试使用coeff分配一些内存。