Program to add two polynomials in C++.

#include<stdio.h>                //INCLUDING DIFFERENT LIBRARIES
#include<stdlib.h>
#include<iostream.h>

//DECLARING FUNCTION PROTOTYPES
void convertpoly();
int check_int(char ch);            //CHECKS IF THE VALUES TAKEN IN IS AN INTEGER
int check_char(char ch);        //CHECKS IF THE VALUE TAKEN IN IS A CHARACTER
void display();                    //DISPLAYS THE RESULTS ON THE SCREEN
void a1();                        //POINTS P AND Q TO THE  FIRST NODE
void a2();                        //CHECKS FOR EQUALITY OF POWERS OF DIFFERENT TERMS OF P AND Q
void a3();                        //DOES TERM ADDITION AND SUBTRACTION AND RESULT INSERTION
void a4();                        //DELETES A RESULT IF ITS COEFFICIENT IS ZERO
void a5();                        //INSERTS UNOPERATED TERM OF P IN Q
int priority(struct node *);    //CHECKS THE ORDER OF THE TERMS OF P AND Q

struct node                        //DECLARING THE STRUCTURE FOR STORING THE TERMS OF THE POLYNOMIAL
{
float coeff;
char sign;
int A,B,C;
struct node *next;
}*pn,*p,*qn,*q,*q1,*q2;


void main()                            //MAIN FUNCTION
{
cout<<"\t\t******** WELCOME TO THE POLYNOMIAL ADDER ********\n"<<endl;
convertpoly();                    //CALLING FUNCTION TO CONVERT THE POLYNOMIALS TO THE NODAL FORM
p=p->next;                        //POINTING TO THE LAST TERM OF P
q=q->next;                      //POINTING TO THE LAST TERM OF Q
a1();
a2();
display();
}//END OF MAIN


int check_int(char ch)                //FUNCTION TO CHECK IF OBTD CHAR IS AN INTEGER/DECIMAL POINT
{
if((ch>47&&ch<58)||(ch==46))
return 1;
else
return 0;
}

int check_char(char ch)                //FUNCTION TO CHECK IF OBTD CHAR IS A VARIABLE
{
if((ch=='x'||ch=='X')||(ch=='y'||ch=='Y')||(ch=='z'||ch=='Z'))
return 1;
else
return 0;
}

void convertpoly()                    //READING POLYNOMIAL TERMS FROM THE TEXT FILE AND CONVERTING THEM INTO NODAL
//FORM TO FORM CIRCULAR LINKED LISTS
{
FILE *fp;
char ch;
p=new struct node;                    //CREATING A DUMMY NODE AT THE BEGINNING OF A POLYNOMIAL
p->sign='-';
p->A=-1;
p->B=-1;
p->C=-1;
p->next=p;
fp=fopen("poly.txt","r");            //OPENING FILE IN READ ONLY MODE
ch=fgetc(fp);                        //READING CHAR FROM FILE
char ch1='+';                        //INITIALISING CHAR TO +
while(ch!='$')                        //READING TILL END OF POLY
{
if(ch=='-'||ch=='+')
{
ch1=ch;
ch=getc(fp);
}

float num;

if(check_char(ch))            //IF COEFFICIENT IS 1
num=1;
else
{
num=0.0;
while(check_int(ch))        //IF COEFFICIENT IS OTHER THAN 1
{
num=num*10+(ch-'0');
ch=fgetc(fp);
}

}

if(ch1=='-')
num=num*(-1);
int a=0,b=0,c=0;
if(ch=='x'||ch=='X')
a=fgetc(fp)-'0';
if(ch=='y'||ch=='Y')
b=fgetc(fp)-'0';
if(ch=='z'||ch=='Z')
c=fgetc(fp)-'0';
ch=fgetc(fp);
if(ch=='-'||ch=='+'||ch=='$')            //IF ONLY ONE VARIABLE PRESENT(OUT OF X,Y AND Z)
{
ch1=ch;
pn=new struct node;                    //CREATING A NEW NODE FOR STORING THE CURRENT TERM
pn->coeff=num;
pn->sign='+';                            //WRITING TO THE NODE
pn->A=a;
pn->B=b;
pn->C=c;
pn->next=p->next;                        //CREATING CIRCULAR LINKED LIST
p->next=pn;
p=pn;
continue;
}
else
{
if(ch=='x'||ch=='X')
a=fgetc(fp)-'0';
if(ch=='y'||ch=='Y')
b=fgetc(fp)-'0';
if(ch=='z'||ch=='Z')
c=fgetc(fp)-'0';
ch=fgetc(fp);
if(ch=='-'||ch=='+'||ch=='$')            //IF END OF TERM OR END OF POLYNOMIAL
{
ch1=ch;
pn=new struct node;                //PUTTING THE PRESENT TERM IN A NEW NODE
pn->coeff=num;
pn->sign='+';
pn->A=a;
pn->B=b;
pn->C=c;
pn->next=p->next;                //INSERTING THE NEW NODE IN THE LINKED LIST
p->next=pn;
p=pn;
continue;
}
else
{
if(ch=='x'||ch=='X')
a=fgetc(fp)-'0';
if(ch=='y'||ch=='Y')
b=fgetc(fp)-'0';
if(ch=='z'||ch=='Z')
c=fgetc(fp)-'0';
ch1=ch=fgetc(fp);
}
}

pn=new struct node;                    //END OF POLYNOMIAL
pn->coeff=num;                        //WRITING THE TERM TO A NEW NODE
pn->sign='+';
pn->A=a;
pn->B=b;
pn->C=c;
pn->next=p->next;
p->next=pn;
p=pn;
}

q=new struct node;                    //START OF THE SECOND POLYNOMIAL Q
q->sign='-';                        //PUTTING A DUMMY NODE AT THE BEGINNING OF THE POLYNOMIAL
q->A=-1;
q->C=-1;
q->B=-1;
q->next=q;
ch=fgetc(fp);
ch1='+';
while(ch!='$')                        //READING TILL THE END OF THE POLYNOMIAL
{
if(ch=='-'||ch=='+')
{
ch1=ch;
ch=getc(fp);
}
float num;
if(check_char(ch))            //IF COEFFICIENT IS 1
num=1;
else
{
num=0.0;
while(check_int(ch))        //CONVERTING THE COEFFICIENT INTO NUM DIGIT BY DIGIT
{
num=num*10+(ch-'0');
ch=fgetc(fp);
}

}
if(ch1=='-')                //IF COEFF IS NEGATIVE
num*=-1;
int a=0,b=0,c=0;
if(ch=='x'||ch=='X')        //READING THE POWERS OF VARIABLES
a=fgetc(fp)-'0';        //STORING THE POWERS IN TEMP VARIABLES
if(ch=='y'||ch=='Y')
b=fgetc(fp)-'0';
if(ch=='z'||ch=='Z')
c=fgetc(fp)-'0';
ch=fgetc(fp);
if(ch=='-'||ch=='+'||ch=='$')        //IF END OF TERM OR END OF POLYNOMIAL
{
ch1=ch;
qn=new struct node;
qn->coeff=num;
qn->sign='+';
qn->A=a;
qn->B=b;
qn->C=c;
qn->next=q->next;
q->next=qn;
q=qn;
continue;                    //GOING TO THE BEGINNING OF THE LOOP
}
else
{
if(ch=='x'||ch=='X')        //READING POWER OF NEXT VARIABLE
a=fgetc(fp)-'0';
if(ch=='y'||ch=='Y')
b=fgetc(fp)-'0';
if(ch=='z'||ch=='Z')
c=fgetc(fp)-'0';
ch=fgetc(fp);
if(ch=='-'||ch=='+'||ch=='$')        //WRTING IN THE NODE IF END OF TERM OR POLYNOMIAL
{
ch1=ch;
qn=new struct node;
qn->coeff=num;
qn->sign='+';
qn->A=a;
qn->B=b;
qn->C=c;
qn->next=q->next;
q->next=qn;
q=qn;
continue;
}
else
{if(ch=='x'||ch=='X')
a=fgetc(fp)-'0';
if(ch=='y'||ch=='Y')
b=fgetc(fp)-'0';
if(ch=='z'||ch=='Z')
c=fgetc(fp)-'0';
ch1=ch=fgetc(fp);
}
}

qn=new struct node;
qn->coeff=num;
qn->sign='+';
qn->A=a;
qn->B=b;
qn->C=c;
qn->next=q->next;
q->next=qn;
q=qn;
}
fclose(fp);
}


int priority(struct node *p)
{
int a,b,c;
a=p->A;
b=p->B;
c=p->C;
return (100*a+10*b+c);
}

void a1()
{
p=p->next;                        //POINTING TO THE FIRST NODE
q1=q;                            //Q1 POINTING TO THE LAST NODE
q=q->next;
}

void a2()
{
while(priority(p)<priority(q))
{
q1=q;
q=q->next;
}
if(priority(p)==priority(q))        //IN CASE OF IDENTICAL POWERS
a3();
else
a5();
}

void a3()
{
if(priority(p)<0)
return;
else
q->coeff+=p->coeff;                //ADDING THE COEFFICIENTS FOR SAME POWERS
if(q->coeff==0)                        //IF COEFF OF RESULT IS ZERO
a4();
else                                //RESULT INSERTION IN Q
{
q1=q;
p=p->next;
q=q->next;
a2();
}
}

void a4()                                //DELETING THE NODE WHICH HAS COEFF ZERO
{
q2=q;
q=q->next;
q1->next=q;
delete(q2);
p=p->next;
a2();
}

void a5()
{
q2=new struct node;
q2->coeff=p->coeff;                    //INSERTING UNOPERATED TERM OF P IN Q
q2->A=p->A;
q2->B=p->B;
q2->C=p->C;
q2->sign=p->sign;
q2->next=q;
q1->next=q2;
q1=q2;ADDING TWO POLYNOMIALS
p=p->next;
a2();
}

void display()                                //FUNCTION TO DISPLAY THE RESULT.
{
q=q->next;

cout<<"REQUIRED SUM \n\nP + Q =";
while(!(q->sign=='-'))
{
if (q->coeff>0)
cout<<" + ";
cout<<q->coeff;
if(!(q->A==0))
cout<<"X"<<q->A;
if(!(q->B==0))
cout<<"Y"<<q->B;
if(!(q->C==0))
cout<<"Z"<<q->C;
q=q->next;
}
cout<<endl<<endl;
}//END OF PROGRAM
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s