Breaking

Search

Tuesday, June 30, 2020

postfix evaluation and polynomial expression in c++ programs

INFIX TO POST FIX EXPRESSION

postfix evaluation and polynomial expression in c++ programs
The write the Program for Post fix Evaluation.

Algorithm:-

  • Step 1: start the program
  • Step 2: Declar an integer array Exp[]
  • Step 3: Declare lenth as integer.
  • Step 4: Get the Postfix Expression call the Postfix Evalvation Function.
  • Step 5: For the Postfix Expression in the array Exp[] using get() Function.
  • Step 6: using switch case sratement do the above mapulation.
  • Step 7: Stop the program.

For Example:Infix to Post fix Expression program

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#define max 100
class postfix
{
private:
char exp[100];
int instack(char);
int income(char);
public:
void infixpost(void);
void getexp(void);
{
count<<"\n\Enter the infix expression";
gets(exp);
}
};
void postfix: :infixpost(void)
{
char op,stack[100];
int len,i,top=-1;
len=strlen(exp);
count<<"\n\tpostfix expression";
for(i=0;i<len;i++)
{
op=exp[i];
if(isalpha(op))
count<<op;
else
if(op==')')
{
while(stack[top]!='(')
count<<stack[top--];
top--;
}
else
{
while(instack(stack[top]>=incom(op))
count<<stack[top--];
stack[++top]=op;
}
}
while(top>=0)
count<<stack[top--];
getch();
}
int postfix: :instack(char op)
{
switch(op)
{
Case'^':return 2;
Case'%':
Case'*':
Case'/' : return 2;
Case'+':
Case'-':
Case'(':
Case')' : return 0;
}
return-1;
}
int postfix: :income(char op)
{

switch(op)
{
Case'(':
Case'^' : return 4;
Case'%':
Case'*' :return 3;
Case'/' : return 2;
Case'+':
Case'-' : return 1;
Case') : return 0;
}
return 4;
}
void main()
{
Clrscr();
postfix 0;
0.getexp();
0.infixpost();
Getch();
}


OUTPUT:

Enter the infix Expression a+b*c+(d*e+f)*g
Postfix expressin abc*+de*f+g*++


Polynomial Addition

The write the program to add two polynomial using Array and printer.

Algorithm:-

  • step 1: Start the program
  • step 2: Enter Polynomial (Addition)Expression.
  • step 3: Create two object XY and store the Expiseacts of them.
  • step 4: Creat an object to perform the addition of the two polynomial Expression.
  • step 5: The addition Can be done by overlonding the '+' operator.
  • step 6: After addition display the expression by calling the display () Function.
  • step 7: Stop the Program.

For Example:Polynomial Addition Program

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#define maxsize 9
class polynomial
{
private:
int a[maxsize*2+1],noitem;
public:
polynomial()
{
noitem=0;
}
polynomial (int no)
{
noitem=no;
}
Void getpoly(void);
Void putpoly(void);
polynomial operator+(polynomial &);
};
polynomial polynomial: :operator+(polynomial &b)
{
polynomial temp (tis->noitem+b.noitem);
Int i,j,k;
while((i<2*this->noitem)&&(j<2*b.noitem)
{
if(a[i]>b.a[j])
{
temp.a[k++]=a[i++];
temp.a[k++]=a[i++];
}
else if(a[i]<b.a[j])
{
temp.a[k++]=b.a[i++];
temp.a[k++]=b.a[i++];
}
else
{
temp.a[k++]=a[i++];
j++;
temp.a[k++]=a[i++];=b.a[j++];
}
}
while(i<2*this->noitem)
{
temp.a[k++]=a[i++];
temp.a[k++]=a[i++];
}
while(i<2*b.noitem)
{
temp.a[k++]=b.a[i++];
temp.a[k++]=b.a[i++];
}
return temp;
}
void polynomial: :getpoly(void)
{
int i;
couny<<"\nenterno.of terms in the polynomial";
cin>>noitem;
for(i=0;i<noitem*2;i+=2)
{
count<<"\n Enter power";
cin>>a[i];
count<<"\nenter co efficient";
cinn>>a[i+1];
}
}
void polynomial: :put poly(void)
{
int i;
count<<end 1;
for(i=0;i<noitem*2; i+=2)
count<<a[i+1]<<"X"<,a[i]<<"+";
}
void main()
{
polynomial X,Y,Z;
Clrscr();
X.getpoly();
Y.getpoly();
z=X+Y;
Clrscr();
X.putpoly();
Y.putpoly();
z.putpoly();
getch();
}


output:

Enter no.of term in the polynomial 3
Enter Power 2
Enter co-efficient 3
Enter Power 1
Enter co-efficient 2
Enter Power 0
Enter co-efficient 5



Enter no.of term in the polynomial 3
Enter Power 2
Enter co-efficient 4
Enter Power 1
Enter co-efficient 7
Enter Power 0
Enter co-efficient 8

Enter no.of term in the polynomial 3
Enter Power 2
Enter co-efficient 3
Enter Power 1
Enter co-efficient 2
Enter Power 0
Enter co-efficient 5

Enter no.of term in the polynomial 3
Enter Power 2
Enter co-efficient 3
Enter Power 1
Enter co-efficient 2
Enter Power 0
Enter co-efficient 5

Enter no.of term in the polynomial 3
Enter Power 2
Enter co-efficient 7
Enter Power 1
Enter co-efficient 9
Enter Power 0
Enter co-efficient 13

3*2+2*1+5
4*2+7*1+8
7*2+9*1+13



Click Now: C++ Program




No comments:

Post a Comment