Professional Documents
Culture Documents
MANUALCPP
MANUALCPP
Given that an employee class contains the following members Data members:
Employee_no, Employee_name,Basic,DA,IT,Net_sal Member functions:to read the data, to calculate
Net_sal and to print the data members.Write c++ program to read the data of N employees and
computeNet_sal of each employee(DA=52% of basic and IT=30% of gross salarty*/
#include<iostream.h>
#include<conio.h>
class employee
{
public:
int emp_num;
float basic,net_sal,it,da,gross;
char emp_name[10];
void read()
{
cout<<"enter the name of the employee\n";
cin>>emp_name;
cout<<"enter the employee number"<<endl;
cin>>emp_num;
out<<"enter the basic salary of the emplyee"<<endl;
cin>>basic;
}
void cal_net()
{
da=0.52*basic;
gross=basic+da;
it=0.3*gross;
net_sal=basic+da-it;
}
void print()
{
cout<<"------------------------\nName of the employee is "
<<emp_name<<endl;
cout<<"employee number is "<<emp_num<<endl;
cout<<"the basic is " <<basic<<endl;
cout<<"the DA is " <<da<<endl;
cout<<"the GROSS is " <<gross<<endl;
cout<<"the IT is " <<it<<endl;
cout<<"the net sal is “ <<net_sal<<endl;
}
};
void main()
{
employee emp[100];
int n;
clrscr();
cout<<"enter number of employees"<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<endl<<"enter the details of employee"<<i<<endl;
emp[i].read();
emp[i].cal_net();
}
for(i=1;i<=n;i++)
{
cout<<"\nDetails of employee "<<emp[i].emp_name<<endl;
emp[i].print();
}
getch();
}
OUTPUT
#include<iostream.h>
#include<conio.h>
class STUDENT
{
public:
int usn;
char name[20];
float mark1,mark2,mark3,avg;
public:
void read();
void average();
void print();
};
void STUDENT::read()
{
cout<<"enter the name of the student\n";
cin>>name;
cout<<"enter the USN number of the student\n";
cin>>usn;
cout<<"enter mark1\n";
cin>>mark1;
cout<<"enter mark2\n";
cin>>mark2;
cout<<"enter mark3\n";
cin>>mark3;
}
void STUDENT::average()
{
int least;
least=mark1;
if(mark2<least)
least=mark2;
if(mark3<least)
least=mark3;
avg=(mark1+mark2+mark3-least)/2;
cout<<avg;
}
void STUDENT::print()
{
cout<<"the name is "<<name<<endl;
cout<<"USN numberis "<<usn<<endl;
cout<<"mark1= "<<mark1;
cout<<"\nmark2="<<mark2;
cout<<"\nmark3="<<mark3;
cout<<"\naverage of two better marks is ";
average();
}
void main()
{
STUDENT stud[10];
int n;
clrscr();
Output
details of abc
the name is abc
USN numberis 123
mark1= 23
mark2=24
mark3=23
average of two better marks is 23.5
details of xyz
the name is xyz
USN numberis 345
mark1= 24
mark2=25
mark3=25
average of two better marks is 25
/*3 Write a C++ program to create a class called COMPLEX and implement the following overloading
functions ADD that return a COMPLEX number.
(1)ADD(a,s2)-where a is an integer(real part) and s2 is a complex number.
(2)ADD(s1,s2)-where s1 and s2 are complex numbers.*/
# include<conio.h>
# include<iostream.h>
# include<stdlib.h>
class complex
{
private: float real;
float img;
public:
void read()
{
cout<<"\nEnter the real part: ";
cin>>real;
cout<<"\nEnter the imaginary part: ";
cin>>img;
}
void display()
{
if(img>=0)
cout<<real<<" +"<<img<<"i"<<endl;
else
cout<<real<<img<<"i"<<endl;
}
complex add(float r, complex c1);
complex add(complex c1, complex c2);
};
OUTPUT
enter first complex number
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct NODE
{
int info;
NODE *link;
};
class LIST
{
public:
NODE *first;
LIST()
{
first=NULL;
}
void insert();
void del();
void display();
};
void LIST::insert()
{
void LIST::del()
{
if(first==NULL)
{
cout<<"no elements to delete";
return;
}
NODE *temp=first;
first=first->link;
cout<<endl<<"element deleted is "<<temp->info;
delete temp;
void LIST::display()
{
NODE *temp;
if(first==NULL)
{
cout<<"no elements to display";
return;
}
cout<<"elements are\n";
for(temp=first;temp!=NULL;temp=temp->link)
cout<<temp->info<<endl;
void main()
{
LIST L;
int ch;
clrscr();
for(;;)
{
cout<<"\n1 - insert\n2 - delete\n3 - display\n4- EXIT\nenter your choice \n";
cin>>ch;
switch(ch)
{
case 1:L.insert(); break;
case 2:L.del(); break;
case 3:L.display(); break;
case 4: cout<<"program terminated"; exit(0);
}
}
getch();
}
OUTPUT
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
1
enter the element to be inserted
23
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
1
enter the element to be inserted
34
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
3
elements are
34
23
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
2
element deleted is 34
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
2
element deleted is 23
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
2
no elements to delete
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
3
no elements to display
1 - insert
2 - delete
3 - display
4- EXIT
enter your choice
4
program terminated
/*5.Write a C++ program to create a template function for Quicksort and demonstrate sorting of
integers and doubles*/
#include<iostream.h>
#include<stdlib.h>
while(i<=j)
{
while(key>=a[i])
{
i++;
}
while(key<a[j])
{
j--;
}
if(i<j) temp=a[i],a[i]=a[j],a[j]=temp;
}
temp=a[j],a[j]=a[low],a[low]=temp;
return j;
}
template <class T>
void sort(T a[])
{
int n;
cout<<"enter number of elements"<<endl;
cin>>n;
cin>>a[i];
}
quicksort(a,0,n-1);
cout<<"the sorted array is "<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
int a[10];
cout<<"Integer array sorting"<<endl;
sort(a);
double b[10];
cout<<"double array sorting"<<endl;
sort(b);
}
OUTPUT
# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
# define SIZE 5
class stack
{
private:
int stk[SIZE];
int top;
public:
stack()
{
top=-1;
}
friend stack operator +(stack s,int x);
friend stack operator -(stack s);
friend ostream & operator<<(ostream &dout, stack &s);
};
}
else
{
s.stk[++s.top] = x;
}
return s;
}
if(s.top==-1)
{
cout<<"\nStack is empty\n";
}
else
{
cout<<"\ncontents of the stack \n";
for(int i=s.top;i>=0;i--)
cout<<s.stk[i]<<endl;
}
return (dout);
}
void main()
{
int opt;
stack s;
for(;;)
{
cout<<"\n1:-Insertion";
cout<<"\n2:-Deletion";
cout<<"\n3:-Display";
cout<<"\n4:-Exit";
cout<<"\nEnter your choice ";
cin>>opt;
switch(opt)
{
case 1: cout<<"\nEnter an element ";
int x;
cin>>x;
s = s + x;
break;
case 2: s = -s;
break;
case 3: cout<<s;
break;
case 4: cout<<"\nProgram Terminated";
exit(0);
}
}
getch();
}
OUTPUT
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 1
Enter an element 23
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 1
Enter an element 34
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 3
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 2
Deleted element is 34
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 3
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 2
Deleted element is 23
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 2
1:-Insertion
2:-Deletion
3:-Display
4:-Exit
Enter your choice 4
Program Terminated
/*7. Write a C++ program to create a class called DATE.Accept two valid dates in the form
dd/mm/yy.Implement the following operations by overloading the operators + and - .After every
operation display the results by overloadingthe operator<<
i) no_of_days=d1-d2;where d1 and d2 are DATE objects,d1>=d2 and no_of_days
is an interger.
ii)d2=d1+no_of_days;where d1 is a DATE object and no_of_days is an integer.*/
#include<iostream.h>
#include<conio.h>
class date
{
private:
int no_days,mon,month[13],day,year;
public:
date();
void read();
int operator-(date);
date operator+(int);
friend ostream& operator<<(ostream &,date);
int operator>=(date);
};
date::date()
{
month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12]=31;
month[4]=month[6]=month[9]=month[11]=30;
month[2]=28;
no_days=0;
}
if((year>d2.year)||(year==d2.year&&mon>d2.mon)||(year==d2.year&&mon==d2.mon&&day>d2.day))
return 1;
return 0;
void date::read()
{
cout<<"enter the date in (dd/mm/yyyy)format\n";
cin>>day;
cin>>mon;
cin>>year;
}
int date::operator-(date d2)
{
while(year!=d2.year||mon!=d2.mon||day!=d2.day)
{
no_days++;
d2.day++;
if((d2.year%4==0&&d2.mon==2)?d2.day>29:d2.day>month[d2.mon])
{
d2.day=1;
d2.mon++;
if(d2.mon>12)
{
d2.mon=1;
d2.year++;
}
}
}
return no_days;
}
date date::operator+(int d)
{
for(int i=0;i<d;i++)
{
day++;
if((year%4==0&&mon==2)?day>29:day>month[mon])
{
day=1;
mon++;
if(mon>12)
{
mon=1;
year++;
}
}
}
return *this;
}
ostream& operator<<(ostream &out,date p)
{
out<<p.day<<"/"<<p.mon<<"/"<<p.year;
return out;
}
void main()
date d1,d2;
int days;
clrscr();
cout<<"\n 1st date"<<endl;
d1.read();
cout<<"\n 2nd date"<<endl;
d2.read();
if(d1>=d2)
{
days=d1-d2;
cout<<"\ndays is "<<days;
}
else
{
cout<<"subtraction is not possible";
}
cout<<"\nenter the number of days to be added\n";
cin>>days;
d2=d1+days;
cout<<"d2=d1+"<<days<<endl;
cout<<d2;
getch();
}
OUTPUT
1st date
enter the date in (dd/mm/yyyy)format
23
02
2009
2nd date
enter the date in (dd/mm/yyyy)format
23
02
2008
days is 366
enter the number of days to be added
23
d2=d1+23
18/3/2009
/*8.Write a C++ program to create a class called MATRIX using a two dimensional
array of integers.Implement the following operations by overloading the
operator == which checks the compatibility of the two matrices to be added and
subtracted.Perform the addition and subtraction by overloading the operators +
and - respectively.Display the results by overloading the operator <<*/
# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
class matrix
{
private:
int a[10][10];
int row,col;
public:
matrix(){}
matrix(int i,int j)
{
row=i;
col=j;
}
void getdata();
friend int operator ==(matrix m1,matrix m2);
matrix operator +(matrix m);
matrix operator -(matrix m);
friend ostream &operator<<(ostream &dout, matrix &m);
};
void main()
{
int m,n,p,q;
clrscr();
#include<iostream.h>
#include<conio.h>
class octal
{
int oct,dec;
public:
octal()
{
oct=0;
dec=0;
}
int operator+(int);
octal(int);
friend ostream & operator<<(ostream&,octal);
};
int octal::operator+(int x)
{
return(dec+x);
octal::octal(int n)
{
dec=n;
int sum=0,p=1,r,temp;
temp=dec;
while(temp>0)
{
r=temp%8;
sum=sum+(p*r);
p=p*10;
temp=temp/8;
}
oct=sum;
void main()
{
int k,x;
clrscr();
cout<<"Enter the decimal number \n";
cin>>x;
octal ol=x;
cout<<"the ocatl equivalent is\n"<<ol;
cout<<endl;
int y=ol+k;
ol=y;
cout<<"\nThe octal equivalent is \n"<<ol;
cout<<endl<<"The decimal equivalent is \n"<<y;
getch();
}
OUTPUT
Enter the decimal number
23
the ocatl equivalent is
27
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
void main()
{
Queue <int> q1;
Queue <double> q2;
int ch;
for(;;)
{
case 4: double y;
cout<<"\nEnter the double to be inserted:\n ";
cin>>y;
q2.Insert(y); break;
}
}
OUTPUT
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
1
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
4
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
1
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
4
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
3
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
6
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
5
2.3 deleted
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
2
23 deleted
1 . insert integer
2 . delete integer
3 . display integer
4 . insert double
5 . delete double
6 . display double
7. exit
enter the choice
7
program terminated
/*11. Write a C++ program to create a class called DLIST (Doubly Linked List) with
member functions to insert a node at a specified position an delete a node
from a specified position of the list .Demonstrate the operations by
displaying the content of the list after every operation.*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct NODE
{
int info;
NODE *llink;
NODE *rlink;
};
class Dlist
{
NODE *first;
public:
Dlist()
{
first = NULL;
}
void ins_pos();
void del_pos();
void show();
};
void Dlist::ins_pos()
{
NODE *cur,*prev;
NODE *temp=new NODE;
int count=1,pos;
cout<<"enter the item to be inserted\n";
cin>>temp->info;
if(first==NULL)
{
cout<<"the element inserted at the position 1\n";
pos=1;
temp->rlink=NULL;
first=temp;
return;
}
else
{
cout<<"enter the position\n";
cin>>pos;
}
if(pos==1)
{
temp->rlink=first;
temp->llink=NULL;
first->llink=temp;
first=temp;
}
else
{
cur=first;
prev=NULL;
while(count!=pos && cur!=NULL)
{
prev=cur;
cur=cur->rlink;
count++;
}
if(count!=pos)
{
cout<<"position not found\n";
return;
}
else
{
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
}
}
void Dlist::del_pos()
{
NODE *cur,*prev,*next;
int count=1,pos;
if(first==NULL)
{
cout<<"list is empty\n";
return;
}
cout<<"enter the position whose element to be deletd";
cin>>pos;
if(pos==1)
{
cur=first;
cout<<"the deleted item is "<<first->info;
first=first->rlink;
delete(cur);
return;
}
else
{
prev=NULL;
cur=first;
while(count!=pos && cur!=NULL)
{
prev=cur;
cur=cur->rlink;
count++;
}
if(count!=pos)
{
cout<<"position not found\n";
return;
}
else
{
prev->rlink=next;
cout<<"the deleted item is "<<cur->info;
delete(cur);
}
}
Dlist d;
int ch;
for(;;)
{
cout<<"\n1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
cout<<"enter the choice\n";
cin>>ch;
switch(ch)
{
case 1: d.ins_pos();
break;
case 2: d.del_pos();
break;
case 3: d.show();
break;
case 4: cout<<"program terminated";
exit(0);
}
}
}
OUTPUT
1. Insert
2. Delete
3. Display
4. Exit
enter the choice
1
enter the item to be inserted
23
the element inserted at the position 1
1. Insert
2. Delete
3. Display
4. Exit
enter the choice
1
enter the item to be inserted
34
enter the position
2
1. Insert
2. Delete
3. Display
4. Exit
enter the choice
2
enter the position whose element to be dele
the deleted item is 34
1. Insert
2. Delete
3. Display
4. Exit
enter the choice
4
program terminated
/*12. Write a C++ program to create a class called STUDENT with data members USN,
nameand age.Using inheritance,create the classes UGSTUDENT and PGSTUDENT
having fields as Semester,Fees andStipend.Enter the data for atleast
5 students.Find the semesterwise average age for all theUG and PG students
separately.*/
#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[20],usn[20];
int age;
void getdata();
void showdata();
};
void student::getdata()
{
cout<<"enter the name\n";
cin>>name;
cout<<"enter the usn\n";
cin>>usn;
cout<<"enter the age\n";
cin>>age;
}
void student::showdata()
{
cout<<"Name of the student "<<name<<endl;
cout<<"USN of the student "<<usn<<endl;
cout<<"age of the student "<<age<<endl;
}
void ugstudent::getdata()
{
student::getdata();
cout<<"enter the sem\n";
cin>>sem;
cout<<"enter the fees\n";
cin>>fees;
cout<<"enter the stipend\n";
cin>>stipend;
}
void ugstudent::showdata()
{
student::showdata();
cout<<"SEM "<<sem<<endl;
cout<<"Fees "<<fees<<endl;
cout<<"Stipend is "<<stipend<<endl;
}
void pgstudent::getdata()
{
student::getdata();
cout<<"enter the sem\n";
cin>>sem;
cout<<"enter the fees\n";
cin>>fees;
cout<<"enter the stipend\n";
cin>>stipend;
}
void pgstudent::showdata()
{
student::showdata();
cout<<"SEM "<<sem<<endl;
cout<<"Fees "<<fees<<endl;
cout<<"Stipend is "<<stipend<<endl;
}
void main()
{
ugstudent ug[10];
pgstudent pg[10];
int n;
clrscr();
cout<<"enter the number of students\n";
cin>>n;
cout<<"\nEnter the details of UG students\n";
for(int i=1;i<=n;i++)
{
cout<<"\nenter the details of student "<<i<<endl<<endl;
ug[i].getdata();
}
for(i=1;i<=n;i++)
{
cout<<"\n Details of student "<<ug[i].name<<endl;
ug[i].showdata();
}
for(int s=1;s<=8;s++)
{
float sum=0;
int count=0;
int flag=0;
for(i=1;i<=n;i++)
if(ug[i].sem==s)
{
sum+=ug[i].age;
count++;
flag=1;
}
if(flag==1)
{
for(i=1;i<=n;i++)
{
cout<<"\nenter the details of student "<<i<<endl;
pg[i].getdata();
}
for(s=1;s<=4;s++)
{
float sum=0;
int count=0;
int flag=0;
for(i=1;i<=n;i++)
if(pg[i].sem==s)
{
sum+=pg[i].age;
count++;
flag=1;
}
if(flag==1)
{
OUTPUT
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
class String
{
char str[20];
public:
String() //zero argument constructor
{
strcpy(str, " ");
}
fout<<s.str;
return fout;
}
void main()
{
String s1("VTU ");
String s2("Belgaum");
clrscr();
cout<<"\nBefore concatenation\n";
cout<<"\nThe string1 is: \n";
cout<<s1;
cout<<"\n";
cout<<"\nThe string2 is: \n";
cout<<s2;
cout<<"\n\nAfter concatetion of string1 and string2 the string is\n";
String s3 = s1 + s2;
cout<<s3;
getch();
}
OUTPUT
Before concatenation
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
class bin_tree
{
node *temp;
public:
node *root;
bin_tree()
{
root=NULL;
}
void create();
void pre(node *root);
void in(node *root);
void post(node *root);
};
void bin_tree::create()
{
node *prev,*cur;
temp=new node;
temp->llink=temp->rlink=NULL;
cout<<"enter the item\n";
cin>>temp->info;
if(root==NULL)
{
root =temp;
return;
}
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(temp->info<cur->info)?cur->llink:cur->rlink;
}
if(temp->info<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
}
void bin_tree::pre(node *root)
{
if(root!=NULL)
{
cout<<root->info<<"\t";
pre(root->llink);
pre(root->rlink);
}
in(root->llink);
cout<<root->info<<"\t";
in(root->rlink);
}
post(root->llink);
post(root->rlink);
cout<<root->info<<"\t";
}
}
void main()
{
bin_tree bt;
int ch;
bt.root=NULL;
clrscr();
for(;;)
{
cout<<"\n1. creation of tree\n";
cout<<"2. preorder traversal\n";
cout<<"3. inorder traversal\n";
cout<<"4. postorder traversal\n";
cout<<"5. exit \n";
cout<<"enter choice";
cin>>ch;
switch(ch)
{
case 1: bt.create();
cout<<"item added to tree\n\n";
break;
case 2:
if(bt.root==NULL)
{
cout<<"tree is empty";
break;
}
cout<<"\n pre order traversal\n";
bt.pre(bt.root);
cout<<endl;
break;
case 3: if(bt.root==NULL)
{
cout<<"tree is empty";
break;
}
cout<<"\n in order traversal\n";
bt.in(bt.root);
cout<<endl;
break;
case 4: if(bt.root==NULL)
{
cout<<"tree is empty";
break;
}
cout<<"\n post order traversal\n";
bt.post(bt.root);
cout<<endl;
break;
OUTPUT
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
23
item added to tree
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
34
item added to tree
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
21
item added to tree
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice1
enter the item
12
item added to tree
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice2
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice3
in order traversal
12 21 23 34
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice4
1. creation of tree
2. preorder traversal
3. inorder traversal
4. postorder traversal
5. exit
enter choice5
program terminated
/*15 Write a C++ program to create a class called EXPRESSION.Using appropriate member functions
convert a given valid infix expression to a postfix form.Display the infix and postfix exp.*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class express
{
char infix[20],post[20],s[20];
int top;
public:
express(char in[20])
{
top=-1;
strcpy(infix,in);
}
void push(char c);
char pop();
int s_p(char c);
int i_p(char c);
void inpo();
void show();
};
void express::push(char c)
{
s[++(top)]=c;
}
char express::pop()
{
return(s[top--]);
}
int express::s_p(char c)
{
switch(c)
{
case '+':
case '-': return 2;
case '*':
case '/': return 4;
case '$':
case '^': return 5;
case '(' : return 0;
case '#' :return -1;
Default: return 8;
}
}
int express::i_p(char c)
{
switch(c)
{
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '$':
case '^': return 6;
case '(' : return 9;
case ')' :return 0;
Default: return 7;
}
}
void express::inpo()
{
char symbol;
int i,j;
j=0;
push('#');
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while((s_p(s[top]))>i_p(symbol))
post[j++]=pop();
if((s_p(s[top]))!=i_p(symbol))
push(symbol);
else
pop();
}
while(s[top]!='#')
post[j++]=pop();
post[j]='\0';
}
void express::show()
{
cout<<"the postfix expression is \n"<<post;
}
void main()
{
char infix[20];
clrscr();
cout<<"enter the infix expression\n";
cin>>infix;
express exp=infix;
exp.inpo();
exp.show();
getch();
}
OUTPUT