What is wrong with my addition of polynomials c code? -
i wrote c program should add 2 polynomials. wrote program in kali linux 2.0 os. when execute program don't required output. instead this-
polynomial 1 how many no. terms want enter? 1 enter coefficient term 1: 2 enter exponent term 1: 3 polynomial 2 how many no. terms want enter? 1 enter coefficient term 1: 2 enter exponent term 1: 3 polynomial 1= polynomial 2= sum of 2 polynomials
the program code given below-
#include<stdio.h> #include<stdlib.h> typedef struct node { int exp,coeff; struct node *next; }poly; poly *heada,*headb,*headc; poly *lasta,*lastb,*lastc; void insert(poly*,poly*,poly *); void input(poly *,poly *); void display(poly *); void insert(poly *new,poly *head,poly *last) { poly *p,*q; if(head==null&&last==null) //setting start { head=last=new; return; } p=head; q=null; while(new->exp<p->exp) { q=p; p=p->next; } if(p->exp==new->exp) //if exponents equal p->coeff=p->coeff+new->coeff; else { if(q!=null) //insertion in middle { q->next=new; new->next=p; } else if(q==null) //insertion @ beginning { new->next=head; head=new; } else if(p==null) //insertion @ end { last->next=new; last=new; } } } void input(poly *head,poly *last) { int i,n,c,e; poly *new; new=(poly *)malloc(sizeof(poly)); printf("how many no. terms want enter? "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nenter coefficient term %d: ",i); scanf("%d",&new->coeff); printf("enter exponent term %d: ",i); scanf("%d",&new->exp); new->next=null; insert(new,head,last); insert(new,headc,lastc); } } void display(poly *start) { poly *p; p=start; while(p!=null) { printf("(%dx^%d)+",p->coeff,p->exp); p=p->next; } printf("\b"); } void main() { system("clear"); heada=(poly *)malloc(sizeof(poly)); headb=(poly *)malloc(sizeof(poly)); headc=(poly *)malloc(sizeof(poly)); lasta=(poly *)malloc(sizeof(poly)); lastb=(poly *)malloc(sizeof(poly)); lastc=(poly *)malloc(sizeof(poly)); heada=headb=headc=null; lasta=lastb=lastc=null; printf("polynomial 1\n\n"); input(heada,lasta); printf("\npolynomial 2\n\n"); input(headb,lastb); printf("\n\npolynomial 1="); display(heada); printf("\npolynomial 2="); display(headb); printf("\nsum of 2 polynomials is="); display(headc); }
edit: after reading comments brought changes in code. output is-
polynomial 1 how many no. terms want enter? 1 enter coefficient term 1: 5 enter exponent term 1: 6 polynomial 2 how many no. terms want enter? 2 enter coefficient term 1: 6 enter exponent term 1: 5 enter coefficient term 2: 7 enter exponent term 2: 8 polynomial 1=(0x^0)+ polynomial 2=(0x^0)+ sum of 2 polynomials is=(0x^0)
the present program code is-
poly *insert(poly*,poly*,poly *); poly *input(poly *,poly *); void display(poly *); poly *insert(poly *new,poly *head,poly *last) { poly *p,*q; if(head==null&&last==null) { head=last=new; return; } p=head; q=null; while(new->exp<p->exp) { q=p; p=p->next; } if(p->exp==new->exp) p->coeff=p->coeff+new->coeff; else { if(q!=null) { q->next=new; new->next=p; } else if(q==null) { new->next=head; head=new; } else if(p==null) { last->next=new; last=new; } } return head; } poly *input(poly *head,poly *last) { int i,n,c,e; poly *new; printf("how many no. terms want enter? "); scanf("%d",&n); for(i=1;i<=n;i++) { new=(poly *)malloc(sizeof(poly)); if(new==null) { printf("allocation error!!"); break; } printf("\nenter coefficient term %d: ",i); scanf("%d",&new->coeff); printf("enter exponent term %d: ",i); scanf("%d",&new->exp); new->next=null; head=insert(new,head,last); headc=insert(new,headc,lastc); free(new); } return head; } void display(poly *start) { poly *p; p=start; while(p!=null) { printf("(%dx^%d)+",p->coeff,p->exp); p=p->next; } printf("\b"); } void main() { system("clear"); heada=(poly *)malloc(sizeof(poly)); headb=(poly *)malloc(sizeof(poly)); headc=(poly *)malloc(sizeof(poly)); lasta=(poly *)malloc(sizeof(poly)); lastb=(poly *)malloc(sizeof(poly)); lastc=(poly *)malloc(sizeof(poly)); if(heada==null||headb==null||headc==null||lasta==null||lastb==null||lastc==null) { printf("allocation failure!!!"); return; } heada=headb=headc=null; lasta=lastb=lastc=null; printf("polynomial 1\n\n"); heada=input(heada,lasta); printf("\npolynomial 2\n\n"); headb=input(headb,lastb); printf("\n\npolynomial 1="); display(heada); printf("\npolynomial 2="); display(headb); printf("\nsum of 2 polynomials is="); display(headc); }
here's working version:
#include <stdio.h> #include <stdlib.h>
a term_t
describes single term in polynomial.
typedef struct term { int exp; int coeff; struct term *next; } term_t;
a poly_t
describes whole polynomial, list of terms. last
pointer wasn't necessary since traverse list start.
typedef struct poly { term_t *head; } poly_t;
we need way initialize polynomials setting list of terms empty. need way destroy polynomial freeing of terms.
void init_poly(poly_t *poly) { poly->head = null; } void destroy_poly(poly_t *poly) { term_t *term = poly->head; while (term != null) { term_t *nextterm = term->next; free (term); term = nextterm; } poly->head = null; }
we find necessary able clone term (create copy of existing term). note in c, not necessary cast return value of malloc
.
term_t *clone_term(term_t *term) { term_t *new_term; if ((new_term = malloc(sizeof *new_term)) == null) { printf("allocation failure!!!\n"); return null; } new_term->coeff = term->coeff; new_term->exp = term->exp; new_term->next = null; return new_term; }
we need way insert term polynomial. terms kept sorted exponent (highest exponent comes earlier in list), if term matching exponent in polynomial, add coefficient. can eliminate of special-case code maintaining pointer next pointer modify point new term.
void insert_term(poly_t *poly, term_t *term) { term_t **nextptr = &poly->head; term_t *nextterm; while ((nextterm = *nextptr) != null) { if (nextterm->exp == term->exp) { /* found existing term matching exponent */ nextterm->coeff += term->coeff; free (term); /* don't need term must free'd */ return; } else if (nextterm->exp < term->exp) { /* next term has lower exponent, stop here */ break; } nextptr = &nextterm->next; } term->next = nextterm; *nextptr = term; }
the input_poly
function responsible inputting single polynomial. allocates new term_t
each term inserts.
int input_poly(poly_t *poly) { int i, n; printf("how many terms want enter? "); scanf("%d", &n); (i = 0; < n; i++) { term_t *term; if ((term = malloc(sizeof *term)) == null) { printf("allocation failure!!!\n"); return -1; } printf("\nenter coefficient term %d: ", i+1); scanf("%d", &term->coeff); printf("enter exponent term %d: ", i+1); scanf("%d", &term->exp); term->next = null; insert_term(poly, term); } return 0; }
the add_to_poly
function adds polynomial existing polynomial. cloning terms of addend, , inserting them accumulator. necessary clone each term because term cannot member of 2 polynomials @ same time. note since exponents of both polynomials kept in sorted order, done more efficiently.
int add_to_poly(poly_t *accum, const poly_t *addend) { term_t *term; (term = addend->head; term != null; term = term->next) { term_t *new_term; if ((new_term = clone_term(term)) == null) { return -1; } insert_term(accum, new_term); } return 0; }
using backspace character delete trailing +
may appear work on terminal, not work when output redirected file. better print separator when needed.
void display_poly(poly_t *poly) { term_t *term; (term = poly->head; term != null; term = term->next) { printf("(%dx^%d)", term->coeff, term->exp); if (term->next != null) { printf("+"); } } }
main
supposed have following type signature. initialize our polynomials, input 2 polynomials, add them , print them.
int main(int argc, char **argv) { poly_t polya; poly_t polyb; poly_t polyc; init_poly(&polya); init_poly(&polyb); init_poly(&polyc); printf("polynomial 1\n\n"); if (input_poly(&polya) == -1) { goto error; } printf("\n"); printf("polynomial 2\n\n"); if (input_poly(&polyb) == -1) { goto error; } printf("\n\n"); if ((add_to_poly(&polyc, &polya) == -1 || add_to_poly(&polyc, &polyb) == -1)) { goto error; } printf("polynomial 1="); display_poly(&polya); printf("\n"); printf("\npolynomial 2="); display_poly(&polyb); printf("\n"); printf("\nsum of 2 polynomials is="); display_poly(&polyc); printf("\n"); error: destroy_poly(&polya); destroy_poly(&polyb); destroy_poly(&polyc); return 0; }
Comments
Post a Comment