Sunday, 2 October 2016

Polynomial Addition C-program w/ Output

Compiled and runned on Visual Studio Express 2013 for Windows Desktop


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct polynode{
float coeff;
int exp;
struct polynode* link;
};

void create_poly(struct polynode** q) //Call by reference to provide address to original pointer
{
float c;
int e, flag;
struct polynode* temp = (struct polynode*) malloc(sizeof(struct polynode));
do{
if (*q == NULL)
{
*q = (struct polynode*) malloc(sizeof(struct polynode));
temp = *q;
}
else{
temp->link = (struct polynode*)malloc(sizeof(struct polynode));
temp = temp->link;
}
printf("\n\tEnter Coeffecient\n\t");
scanf("%f", &c);
temp->coeff = c;
printf("\n\tEnter Exponent\n\t");
scanf("%d", &e);
temp->exp = e;
 printf("\n\tWanna continue Y=1/N=0\n\t");
 scanf("%d", &flag);
} while (flag);
temp->link = NULL;
}

void display(struct polynode *q)
{
if (q==NULL)
{
printf("\n\tNot Found ! Try Again!!!\n\t");
return;
}
while (q != NULL)
{
printf("%fx^%d : ", q->coeff, q->exp);
q = q->link;
}
printf("\b\b\b");// Delete : from last
printf("\n\t");
}

void addpoly(struct polynode* p, struct polynode* q, struct polynode **r)
{
struct polynode *t = (struct polynode*) malloc(sizeof(struct polynode));// Temperary node
if (p==NULL && q==NULL)
{
return;
}
while (p != NULL && q != NULL)
{
if (*r==NULL) //Checking Total node where addition takes place
{
*r = (struct polynode*) malloc(sizeof(struct polynode));
t = *r; //Assigning temp to total node
}
else{
t->link = (struct polynode*) malloc(sizeof(struct polynode));// Creation of nodes at Intermediate Stage
t = t->link;
}
if (p->exp < q->exp)// If expo. of Second is greater then First assigining Second to Total
{
t->coeff = q->coeff;
t->exp = q->exp;
q = q->link;
}
else{
if (p->exp > q->exp)
{
t->coeff = p->coeff;
t->exp = p->exp;
p = p->link;
}
else{
if (p->exp == q->exp)
{
t->coeff = p->coeff+q->coeff;
t->exp = p->exp;
p = p->link;
q = q->link;
}
}//else
}//else
}//while-1

while (p != NULL)//If second list becomes empty or empty
{
if (*r == NULL) //Checking Total node where addition takes place
{
*r = malloc(sizeof(struct polynode));
t = *r; //Assigning temp to total node
}
else{
t->link = malloc(sizeof(struct polynode));// Creation of nodes at Intermediate Stage
t = t->link;
}
t->coeff = p->coeff;
t->exp = p->exp;
p = p->link;
}//while-2


while (q != NULL)//If first list becomes empty or empty
{
if (*r == NULL) //Checking Total node where addition takes place
{
*r = malloc(sizeof(struct polynode));
t = *r; //Assigning temp to total node
}
else{
t->link = malloc(sizeof(struct polynode));// Creation of nodes at Intermediate Stage
t = t->link;
}

t->coeff = q->coeff;
t->exp = q->exp;
q = q->link;
}//while-3

t->link = NULL;  //At last Make total NULL
}//polyadd()

void main()
{
struct polynode *first, *second, *total;
first = second = total = NULL;
int ch;
while(1)
{   
printf("\n\t Enter choice \t 1. First Expression\t2. Second Expression\t3.Addition\t4.exit\n\t");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("\n\tCreate 1st expression\n\t");
create_poly(&first);
printf("\n\tStored the 1st expression\n\t");
display(first);
break;
case 2: printf("\n\tCreate 2nd expression\n\t");
create_poly(&second);
printf("\n\tStored the 2nd expression\n\t");
display(second);
break;
case 3: addpoly(first, second, &total);
display(total);
break;
case 4:exit(0);
}


}

O/p-



No comments:

Post a Comment

LibreOffice Google Summer of Code Final Report

Adding native support for histogram chart and its variations  My final report for the work done since May till August in LibreOffice codebas...