Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* polynomial.c source file for task in exercise 10*/
- #include <stdio.h>
- #include <stdlib.h>
- #include "polynomial.h"
- Node_ptr constructorTerm(int coef, int exp, Node_ptr tmp);
- void printPolynomial(Polynomial p, int bild);
- Polynomial constructPolynomial(int ce[], int size);
- Polynomial addPolynomials( Polynomial p1, Polynomial p2);
- int main ( ){
- Polynomial p1;
- Polynomial p2;
- Polynomial p3; // variable refers to header node
- Node_ptr term; // variable refers to a term (node)
- Node_ptr tmp;
- int ce[100];
- int ce2[100];
- // constructorTerm(5,14,tmp);
- int size = 0;
- int size2 = 0;
- int i, temp;
- printf("Antal siffror i polynomial nr 1: ");
- scanf("%d",&size);
- for(i = 0; i < size; i++){
- printf("\ncoef: ");
- scanf("%d",&ce[i*2]);
- printf("\nexp: ");
- scanf("%d",&ce[i*2+1]);
- }
- printf("Antal siffror i polynomial nr 2: ");
- scanf("%d",&size2);
- for(i = 0; i < size2; i++){
- printf("\ncoef: ");
- scanf("%d",&ce2[i*2]);
- printf("\nexp: ");
- scanf("%d",&ce2[i*2+1]);
- }
- int bild = 1;
- p1 = constructPolynomial(ce, size);
- printPolynomial(p1,bild);
- bild++;
- p2 = constructPolynomial(ce2, size2);
- printPolynomial(p2,bild);
- int count = 0;
- p3 = addPolynomials(p1,p2);
- printf("The final polynomial: \n");
- p3 = p3 -> next;
- while(p3 != NULL){
- if(count >= 1){
- printf("+ %dx^%d ", p3->coef, p3->exp);
- p3 = p3 -> next;
- count++;
- }
- else{
- printf(" %dx^%d ", p3->coef, p3->exp);
- count++;
- p3 = p3 -> next;
- }
- }
- }
- Node_ptr constructorTerm(int coef,int exp, Node_ptr tmp){
- Node_ptr hello = (Node_ptr)malloc ( sizeof (struct node) );
- (*hello).coef = coef;
- (*hello).exp = exp;
- (*hello).next = tmp->next;
- return hello;
- }
- printPolynomial(Polynomial p, int bild){
- int count = 0;
- if(bild == 1)
- printf("1st polynomial:\n");
- else printf("2nd polynomial:\n");
- while(p != NULL){
- if(count >= 1){
- printf("+ %dx^%d ", p->coef, p->exp);
- p = p -> next;
- count++;
- }else{
- printf(" %dx^%d ", p->coef, p->exp);
- p = p -> next;
- count++;
- }
- }
- printf("\n");
- }
- Polynomial constructPolynomial(int ce[], int size){
- int i = 1;
- Node_ptr temporary = (Node_ptr)malloc(sizeof(struct node));
- Polynomial sup = (Node_ptr)malloc(sizeof(struct node));
- sup-> coef = ce[0]; sup ->exp = ce[1]; sup -> next = NULL;
- for (i= 1; i< size; i++){
- temporary = constructorTerm(ce[i*2],ce[(i*2+1)],sup);
- temporary -> next = sup;
- sup = temporary;
- }
- return sup;
- }
- /*Polynomial addPolynomials(Polynomial p1, Polynomial p2){
- Polynomial p3 = (Node_ptr)malloc(sizeof(struct node));
- Node_ptr tmp = p3;
- p3 -> next = NULL;
- while(p1 !=NULL && p2 != NULL){
- if(p1->exp > p2->exp){
- tmp -> next = constructorTerm(p1->coef,p1->exp,tmp);
- tmp = tmp -> next;
- p1 = p1->next;
- }
- else if(p2->exp > p1->exp){
- tmp -> next = constructorTerm(p2->coef,p2->exp,tmp);
- tmp = tmp -> next;
- p2 = p2->next;
- }
- else if(p2->exp == p1 ->exp){
- int bingo = p2->coef + p1->coef;
- tmp -> next = constructorTerm(bingo,p1->exp,tmp);
- tmp = tmp -> next;
- p2 = p2 ->next;
- p1 = p1 -> next;
- }
- }
- while(p1 != NULL || p2 != NULL){
- if(p1 == NULL && p2 == NULL)
- break;
- else{
- if(p1 == NULL && p2 != NULL){
- tmp -> next = constructorTerm(p2->coef,p2->exp,tmp);
- tmp = tmp -> next;
- p2 = p2->next;
- }
- else if(p2 == NULL && p1 != NULL){
- tmp -> next = constructorTerm(p1->coef,p1->exp,tmp);
- tmp = tmp -> next;
- p1 = p1->next;
- }
- }
- }
- return p3;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement