1

Cのリンクリストを使用して、多項式で加算、減算、乗算、微分演算を実行するプログラムに取り組んでいます。他の操作は、乗算以外は正常に機能しています。ここにコードがあります

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
   int coeff;
   int pow;
   struct link *next;
   };
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
 char ch;
 do
 {
  printf("\n\nenter coeff:");
  scanf("%d",&node->coeff);
  printf("\nenter power:");
  scanf("%d",&node->pow);
  node->next=(struct link*)malloc(sizeof(struct link));
  node=node->next;
  node->next=NULL;
printf("\ncontinue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{

printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf(" + ");


}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
 while(poly1->next &&  poly2->next)
 {
  if(poly1->pow>poly2->pow)
  {
   poly->pow=poly1->pow;
   poly->coeff=poly1->coeff;
   poly1=poly1->next;
   }
  else if(poly1->pow<poly2->pow)
  {
   poly->pow=poly2->pow;
   poly->coeff=poly2->coeff;
   poly2=poly2->next;
   }
  else
  {
   poly->pow=poly1->pow;
   poly->coeff=poly1->coeff+poly2->coeff;
   poly1=poly1->next;
   poly2=poly2->next;
   }
  poly->next=(struct link *)malloc(sizeof(struct link));
  poly=poly->next;
  poly->next=NULL;
 }
 while(poly1->next || poly2->next)
 {
  if(poly1->next)
  {
   poly->pow=poly1->pow;
   poly->coeff=poly1->coeff;
   poly1=poly1->next;
   }
  if(poly2->next)
  {
   poly->pow=poly2->pow;
   poly->coeff=poly2->coeff;
   poly2=poly2->next;
   }
   poly->next=(struct link *)malloc(sizeof(struct link));
   poly=poly->next;
   poly->next=NULL;
   }

}

void polysub(struct link *poly1,struct link *poly2,struct link *poly)
{
 while(poly1->next &&  poly2->next)
 {
  if(poly1->pow>poly2->pow)
  {
   poly->pow=poly1->pow;
   poly->coeff=poly1->coeff;
   poly1=poly1->next;
   }
  else if(poly1->pow<poly2->pow)
  {
   poly->pow=poly2->pow;
   poly->coeff=poly2->coeff;
   poly2=poly2->next;
   }
  else
  {
   poly->pow=poly1->pow;
   poly->coeff=poly1->coeff-poly2->coeff;
   poly1=poly1->next;
   poly2=poly2->next;
   }
  poly->next=(struct link *)malloc(sizeof(struct link));
  poly=poly->next;
  poly->next=NULL;
 }
 while(poly1->next || poly2->next)
 {
  if(poly1->next)
  {
   poly->pow=poly1->pow;
   poly->coeff=poly1->coeff;
   poly1=poly1->next;
   }
  if(poly2->next)
  {
   poly->pow=poly2->pow;
   poly->coeff=poly2->coeff;
   poly2=poly2->next;
   }
   poly->next=(struct link *)malloc(sizeof(struct link));
   poly=poly->next;
   poly->next=NULL;
   }

}

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));
                    temp->next=NULL;    
                    n2=n2beg;
                    while (n2) 
                    {
                            temp->coeff = n1->coeff * n2->coeff;

                            temp->pow = n1->pow + n2->pow;

                            n2 = n2->next;
                            temp->next=(struct link *)malloc(sizeof(struct link));
                            temp=temp->next;
                            temp->next=NULL;

                    }

                    polyadd(temp,n,n);
                    n1 = n1->next;
                    free(temp);
            }

}

void diff(struct link* p1,struct link* p2)
{

while(p1->next!=NULL)
{
    p2->coeff=p1->coeff*p1->pow;
    p2->pow=p1->pow-1;
    p2->next=NULL;
    p2->next=(struct link *)malloc(sizeof(struct link));
   p2=p2->next;
   p2->next=NULL;
   p1=p1->next;
}

}
  main()
  {
  int op;
  char ch;
  do{
  poly1=(struct link *)malloc(sizeof(struct link));
  poly2=(struct link *)malloc(sizeof(struct link));
  poly=(struct link *)malloc(sizeof(struct link));
  printf("\n\nWhat do you want to do?\n1.Addition\n2.Subtraction
          \n3.Multiplication\n4.Differentiation\n0.Exit
          \nEnter your choice:");
  scanf("%d",&op);
  switch(op)
  {
        case 1:
            printf("\n\nenter 1st polynomial:");
            create(poly1);
            printf("\n\nenter 2nd polynomial:");
            create(poly2);
            printf("\n1st Polynomial:\t");
            show(poly1);
            printf("\n2nd Polynomial:\t");
            show(poly2);
            polyadd(poly1,poly2,poly);
            printf("\nAdded polynomial:\t");
            show(poly);
            break;
        case 2:
            printf("\n\nenter 1st polynomial:\t");
            create(poly1);
            printf("\n\nenter 2nd polynomial:\t");
            create(poly2);
            printf("\n\n1st Polynomial:\t");
            show(poly1);
            printf("\n\n2nd Polynomial:\t");
            show(poly2);
            polysub(poly1,poly2,poly);
            printf("\n\nSubtracted polynomial:\t");
            show(poly);
            break;  
        case 3:
            printf("\n\nenter 1st polynomial:");
            create(poly1);
            printf("\n\nenter 2nd polynomial:");
            create(poly2);
            printf("\n\n1st Polynomial:\t");
            show(poly1);
            printf("\n\n2nd Polynomial:\t");
            show(poly2);
            polymul(poly1,poly2,poly);
            printf("\n\nMultiplied polynomial:\t");
            show(poly);
            break;
        case 4:
            printf("\n\nenter polynomial:");
            create(poly1);
            printf("\n\nPolynomial:\t");
            show(poly1);
            diff(poly1,poly2);
            printf("\n\nDifferentiated Polynomial:\t");
            show(poly2);
            break;
        }

 /* printf("\n Want to continue? Y/N:");
  ch=getch();*/
  }
  while(op);

}

4

5 に答える 5

0
#include<stdio.h>
#include <conio.h>
struct node
{
    int c,e;
    struct node *link;
}*start1=NULL,*start2=NULL,*start3=NULL,*temp1,*temp2,*temp3,*new_node;
int delete_dup(int h)
{
    struct node *cr,*prev,*run,*tmp;

    cr = start3->link;
    prev = start3;

    while(cr != NULL){

        run = start3;

        while(run != cr)
        {
             if(run->e == cr->e)
            {
                run->c+=cr->c;
                tmp = cr;
                cr = cr->link;
                prev->link = cr;
                remove(tmp);h--;
                break;
            }

            run = run->link;
        }

        if(run == cr){
            cr = cr->link;
            prev = prev->link;
        }
    }
    return h;

}

void main()
{
    int n,m,i,j,k;
    puts("Enter the number of terms in first polynomial");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        new_node=(struct node*)malloc(sizeof(struct node));
        new_node->link=NULL;
        puts("C=");
        scanf("%d",&new_node->c);
        puts("E=");
        scanf("%d",&new_node->e);
        new_node->link=start1;
        start1=new_node;

        }
        puts("Enter the number of terms in first polynomial");
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        new_node=(struct node*)malloc(sizeof(struct node));
        new_node->link=NULL;
        puts("C=");
        scanf("%d",&new_node->c);
        puts("E=");
        scanf("%d",&new_node->e);
        new_node->link=start2;
        start2=new_node;

        }
        temp1=start1;
        temp2=start2;
        i=0; j=0;
        while(i<m)
        {   
            j=0; temp1=start1;
            while(j<n)
            {
                new_node=(struct node*)malloc(sizeof(struct node));
                new_node->link=NULL;
                new_node->c=temp1->c*temp2->c;
                new_node->e=temp1->e+temp2->e;
                new_node->link=start3;
                start3=new_node;
                j++;
                temp1=temp1->link;
            }
            temp2=temp2->link;
            i++;
        }
        i=0;
        k=delete_dup(m*n);
        temp3=start3;
        while(i<k-1)

            {
                printf("(%dx^%d)+",temp3->c,temp3->e);
                temp3=temp3->link;
                i++;
            }
                printf("(%dx^%d)",temp3->c,temp3->e);

                    }
于 2014-03-20T16:18:18.093 に答える
0

polyadd(temp,n,n)as used inは、ソース多項式および加算先多項式としての 使用にpolymul()問題があります。n

polyadd()同じ多項式を指すパラメーターに対処するためにやり直すか、別の多項式を使用するためにpolyadd()inの呼び出しをやり直してください。polymul()最初に提案します。

于 2013-08-13T19:56:03.770 に答える
0

create()関数にエラーが見つかったため、完全なコードは確認しませんでした。poly1関数に引数として渡したことに注意してくださいcreate()

C は値による呼び出しに従うため、これは正しくありません。何が起こるかというと、poly1(まだ初期化されていない) の値だけが渡され、*nodeその値が格納されます。のアドレスをpoly1引数として渡し、ポインターからポインターを使用して関数でその値をキャッチすることをお勧めします。

于 2014-02-12T01:39:25.967 に答える