Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Numerical Methods Tutorial
Practical Paper
- C Programming
- C
- Data Structure Using C, C ++
- Programming in R
- Programming with Python
- Machine Learning
- Swift
- Firebase
- Android
- iOS Development
- Django
- PHP
- Arduino
- Internet of Technology
- IOT Projects
- Dart Programming
- Flutter
- Flutter Tutorials
- Kotlin Tutorial
- Laravel Tutorial
- VueJS Tutorial
- Go Lang
- Rust
- Apex
Industrial Training
Code
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
class polyn;
class llelmt{
int expon;
float coeff;
llelmt *next;
public:
llelmt(int ex=0,float cff=0){//constructor.
expon=ex;
coeff=cff;
next=NULL;
}
friend class polyn;
};
class polyn{
llelmt *head;
void addtrm(float cff,int xp);//adds a nw term to the polyn.
void sort_and_adj(void);//sorts the polyn by ascsending seq of exponents,and removes duplicate exp terms.
public:
polyn(void){//constructor.
head=NULL;
}
polyn(polyn&);//copy constructor.
~polyn();//destructor.
void getpoly(void);//gets a polynomial.
void show(void);//displays a polyn.
polyn operator+(polyn);//adds two polyns.
polyn operator-(polyn);//finds difference between 2 polyns.
};
//-------------------------------------------------------------------------
polyn::polyn(polyn &pp){
// textcolor(GREEN);
// cprintf("IN copy constructor.");
// cout<<endl;
llelmt *p;
head=NULL;
for(p=pp.head;p!=NULL;p=p->next)
addtrm(p->coeff,p->expon);
}
polyn::~polyn(){
llelmt *p,*q;
q=head;
head=NULL;
while(q!=NULL){
p=head;
q=q->next;
delete p;
}
}
void polyn::getpoly(void){
int exp;
char res='y';
float coff;
system("cls");
while (res!='n'){
textcolor(RED);
cprintf("Enter coeff,exp for the term : ");
cout<<endl;
cin>>coff>>exp;
addtrm(coff,exp);
cout<<endl;
cprintf(" Any more terms? ");
cin>>res;
}
sort_and_adj();
}
void polyn::addtrm(float cff,int xp){
llelmt *p,*q;
q=new llelmt;
q->coeff=cff;
q->expon=xp;
if(head!=NULL){
for(p=head;p->next!=NULL;p=p->next);
p->next=q;
}
else{
head=q;
}
}
void polyn::show(void){
llelmt *p;
textcolor(CYAN);
cout<<" = ";
for(p=head;p!=NULL;p=p->next){
cprintf("%f X^%d",p->coeff,p->expon);
if(p->next!=NULL)
cprintf(" + ");
}
cout<<endl;
getch();
return;
}
void polyn::sort_and_adj(void){
llelmt *p,*q,*r,*hh,*hhq,*plus;
// Sorting by Insertion sort....
plus=new llelmt;
plus->expon=-100;
plus->next=head;
hhq=plus;
for(hh=head;hh!=NULL;hh=hh->next){
q=plus;
for(p=plus->next;p!=hh;p=p->next){
if(hh->expon < p->expon){
r=hh->next;
q->next=hh;
hh->next=p;
hhq->next=r;
hh=hhq;
break;
}
q=p;
}
hhq=hh;
}
// Adjusting the polyn.....
hhq=plus;
for(hh=plus->next;hh!=NULL;hh=hh->next){
p=hh->next;
if(p!=NULL)
if(p->expon == hh->expon){
hh->coeff = hh->coeff + p->coeff ;
hh->next=p->next;
delete p;
hh=hhq;
}
hhq=hh;
}
head=plus->next;
delete plus;
}
polyn polyn::operator +(polyn a){// Target : p3=p2+p1
polyn sm;
polyn bb=*this;
llelmt *p,*q;
p=sm.head;
while(p!=NULL){//delete sm if it exists.
if(p->next!=NULL){
q=p;
p=p->next;
delete q;
}
else{
delete p;
break;
}
}
sm.head=NULL;
for(p=a.head;p!=NULL;p=p->next)//copy(sm <- a).
sm.addtrm(p->coeff,p->expon);
for(p=sm.head;p!=NULL && p->next!=NULL;p=p->next);//concat(sm <- bb).
p->next=bb.head;
bb.head=NULL;
sm.sort_and_adj();// sort and adjust sm.
return sm;
}
polyn polyn::operator-(polyn a){//current - a;
polyn x;
llelmt *p,*q;
p=x.head;//delete polyn x if it already exists.
x.head=NULL;
while(p!=NULL){
q=p->next;
delete p;
p=q;
}
for(p=a.head;p!=NULL;p=p->next)
x.addtrm(-1*(p->coeff),p->expon);
x=x+(*this);
x.sort_and_adj();
return x;
}
int main(void){
polyn r,s;
cout<<"Press any key to enter a polynomial..";
getch();
r.getpoly();
cout<<"Polynomial r";
r.show();
cout<<"\n\nCopying the polynomial into polynomial 'b'.\nb ";
polyn b(r);
b.show();
cout<<"adding: s=b+r.\ns";
s=b+r;
s.show();
cout<<"adding: s=s+r.\ns";
s=s+r;
s.show();
cout<<"subtracting: s=s-r.\ns";
s=s-r;
s.show();
cout<<"\n\n end of main";
return 0;
}
/*
polyn operator +(polyn bb,polyn a){// Target : p3=p2+p1
polyn sm;
// polyn bb=*this;
llelmt *p,*q;
p=sm.head;
while(p!=NULL){//delete sm if it exists.
if(p->next!=NULL){
q=p;
p=p->next;
delete q;
}
else{
delete p;
break;
}
}
sm.head=NULL;
for(p=a.head;p!=NULL;p=p->next)//copy(sm <- a).
sm.addtrm(p->coeff,p->expon);
for(p=sm.head;p!=NULL && p->next!=NULL;p=p->next);//concat(sm <- bb).
p->next=bb.head;
bb.head=NULL;
sm.sort_and_adj();// sort and adjust sm.
return sm;
}
polyn operator-(polyn b,polyn a){//current - a;
polyn x;
llelmt *p,*q;
p=x.head;//delete polyn x if it already exists.
x.head=NULL;
while(p!=NULL){
q=p->next;
delete p;
p=q;
}
for(p=a.head;p!=NULL;p=p->next)
x.addtrm(-1*(p->coeff),p->expon);
x=x+b;
x.sort_and_adj();
return x;
}
*/