You are on page 1of 86

PRACTICAL ASSIGNMENT

NAME : Nishank Yadav


REG. NO. :
YEAR : 2009/10
Certificate

This is to certify that Nishank Yadav of


class twelve, Modern School, Vasant Vihar has
successfully completed his practical assignment file in
computer science for the AISSCE as prescribed by CBSE
in the year 2009-2010.

Date :

Registration No. :

Signature of Internal Signature of External


Examiner Examiner

__________________ __________________
C++ Revision Tour
Q- Write a program to find the LCM and GCD of two numbers.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();

int x,y,gcd=1;

cout<< "ENTER 1st NO : ";


cin>>x;

cout<<"\n\nEnter 2nd NO. :";


cin>>y;

for(int i=1;i<1000;++i)
{
if((x%i==0)&&(y%i==0))
gcd=i;
}

cout<<"\n\n\nGCD :"<<gcd;
cout<<"\n\n\nLCM :"<<(x*y)/gcd;

getch();
}

OUTPUT:

ENTER 1st NO : 12

ENTER 2nd NO. :13

GCD :1

LCM :156
Q- Write a program to find the sum of sequence
1 + 1/1! + 1/2!+ .........

#include<iostream.h>
#include<conio.h>

void main()
{
int n,i;
float sum=1.00;
float fact(int a);
clrscr();
cout<<"Enter n:";
cin>>n;
for(i=1;i<n;i++)
{
sum+=(1/fact(i));
}
cout<<"Sum of series ="<<sum;
getch();
}

float fact (int a)


{
int f=1,i;
for (i=1;i<=a;i++)
f*=i;
return f;
}

OUTPUT:

Enter n : 5
Sum of series =2.708333
Q- Write a function that takes time as three integer
arguments(hours,minutes,seconds),
and returns the number of seconds since the clock last struck "12". Use this
function to write a program to calculate the amount of time in seconds between
two times, both of which are within one 12 hour cycle of clock.

#include<iostream.h>
#include<conio.h>
#include<math.h>
int seconds(int hh,int mm,int ss)
{
int z,s;
z=abs(hh-12);
s=(z*60*60)+(mm*60)+ss;
return s;
}
int main()
{
clrscr();
int h1,h2,m1,m2,s1,s2,t1,t2;
cout<<"Enter the first time:"<<endl;
cout<<"\tHours:";
cin>>h1;
cout<<"\tMinutes:";
cin>>m1;
cout<<"\tSeconds:";
cin>>s1;
t1=seconds(h1,m1,s1);
cout<<"Enter the second time:"<<endl;
cout<<"\tHours:";
cin>>h2;
cout<<"\tMinutes:";
cin>>m2;
cout<<"\tSeconds:";
cin>>s2;
t2=seconds(h2,m2,s2);
cout<<"The difference in time is:"<<abs(t1-t2);
return 0;
}

OUTPUT:
Enter the first time:
Hours:5
Minutes:45
Seconds:59

Enter the second time:


Hours:7
Minutes:12
Seconds:5
The difference in time is:9234
Write a program to find the sum of the sequence

#include<iostream.h>
#include<conio.h>
#include<math.h>
void sum(int x,int n)
{
double sum=0,s=0;
int k=1;
long fac=1;
for(int i=1;i<=n-1;i++)
{
for(int j=(2*k);j>=1;j--)
{
fac*=j;
}
sum+=(pow(x,i)/fac);
}
s=1+sum;
cout<<"\nThe sum is:\n";
cout<<s;
}
void main()
{
clrscr();
int x,n;
cout<<"\nEnter the limiting value\n";
cin>>n;
cout<<"\nEnter the value \n";
cin>>x;
sum(x,n);
getch();
}

OUTPUT:

Enter the limiting value


5

Enter the value


3
The sum is:
13.1875

Q- Computers are playing an increasing role in education. Write aprogram that will
help elementary school students leaern multiplication. Use rand function to
produce
two positive one digit integers.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void main()
{
clrscr();

int x[2],ans;
time_t t;
char c;
do
{
srand(time(&t));
x[0]=rand()%10;

srand(x[0]);
x[1]=rand()%10;

cout<<"\n\nWhat is "<<x[0]<<"times "<<x[1]<<" ?\nANS: ";


do
{
cin>>ans;
if(ans!=(x[0]*x[1]))
cout<<"\nWRONG! TRY AGAIN\n";
}while(ans!=(x[0]*x[1]));

if(ans==(x[0]*x[1]));
{
cout<<"correct!\n\n\nDO YOU WANT TO CONTINUE?";
cin>>c;
}
if(c=='N'||c=='n')
{
break;}
}while(1);
}

OUTPUT:

What is 5times 1 ?
ANS: 5
correct!

DO YOU WANT TO CONTINUE?y

What is 6times 7 ?
ANS: 48

WRONG! TRY AGAIN


42
correct!

DO YOU WANT TO CONTINUE?n


Q- Write a program to accept three digits and print all possible combinations
from these digits.

#include<iostream.h>
#include<conio.h>
void main()
{
int a[3];
clrscr();
cout<<"Enter three digits :"<<endl;
cin>>a[0]>>a[1]>>a[2];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
if (i!=j&&i!=k&&j!=k)
cout<<endl<<endl<<a[i]<<a[j]<<a[k];
}
}
}
getch();
}

OUTPUT:
Enter three digits :
123

123

132

213

231

312

321
Q - Use one dimensional array to solve the following proble. Read 20 integers from
data file, each of which is between 10 and 100, inclusive. As each number is read,
print it only if it is not a duplicate of a number already erad. Provide for the
worst case in which all 20 integers are different. Use the smallest possible array
to solve this problem.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[20];
for (int i=0;i<20;i++)
{
cout<<"enter the element no "<<i+1<<endl;
cin>>arr[i];
}

for (i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
if (i!=j)
{
if (arr[i]==arr[j])
arr[j]=0;
else
continue;
}
else
continue;
}
}

for (i=0;i<20;i++)
{
if (arr[i]<=100&&arr[i]>=10)
cout<<arr[i]<<endl;
else
continue;
}
getch();
}

OUTPUT:

enter the element no 1


1
enter the element no 2
11
enter the element no 3
12
enter the element no 4
113
enter the element no 5
14
enter the element no 6
15
enter the element no 7
16
enter the element no 8
17
enter the element no 9
18
enter the element no 10
19
enter the element no 11
10
enter the element no 12
11
enter the element no 13
12
enter the element no 14
13
enter the element no 15
14
enter the element no 16
15
enter the element no 17
16
enter the element no 18
17
enter the element no 19
23
enter the element no 20

34
11
12
14
15
16
17
18
19
10
13
23
34
Q Write a c++ program to sum the sequence
x -(x^2/2!) + (X^4/4!) -(x^6/6!) + .......

#include<iostream.h>
#include<conio.h>
#include<math.h>

int main()
{
int x,p,i,j;
double fact=1.0,ans=0;
cout<<"Enter the value of x:";
cin>>x;
cout<<"Enter till what power you want:";
cin>>p;
ans=x;
for(i=2,j=1;i<=p;i++,j++){
fact=fact*i;
if(i%2==0)
ans+=(pow(-1,j))*((pow(x,i))/(fact));
}
cout<<"The sum of the series is:"<<ans;
return 0;
}

OUTPUT:
Enter the value of x:3
Enter till what power you want:4
The sum of the series is:-4.875
Q Write a c++ function having two value parameters U and n with result type float
to find the sum of the series given below
1 - (U^2) + 1/2!(U^2) -1/3!(U^3) + .............

#include<iostream.h>
#include<conio.h>
#include<math.h>

int main()
{
int x,p,i,j;
double fact=1.0,ans=1;
cout<<"Enter the value of x:";
cin>>x;
cout<<"Enter till what power you want:";
cin>>p;
for(i=1;i<=p;i++){
fact=fact*i;
ans+=(pow(-1,i))*((pow(x,i))/(fact));
}
cout<<"The sum of the series is:"<<ans;
return 0;
}

OUTPUT:
Enter the value of x:3
Enter till what power you want:4
The sum of the series is:1.375
Structures
Q- Write a program to accept information about a person and then display it .

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{
char name[30];
long phone;
public:
person()
{
cout<<"\nConstructor initialised\n";
}
void getname()
{
cout<<"\nEnter the name\n";
gets(name);
cout<<"\nEnter the phone number\n";
cin>>phone;
}
void display()
{
cout<<"\nName\t";puts(name);
cout<<"\nPhone number\t"<<phone;
}
~person()
{
cout<<"\n******************\n";
}
};
class spouse:public person
{
char spousename[40];
public:
void getsp();
void display();
};
void spouse::getsp()
{
person::getname();
cout<<"\nEnter the spouse name\n";
gets(spousename);
}
void spouse::display()
{
person::display();
cout<<"Spouse name\t";puts(spousename);
}
void main()
{
clrscr();
spouse obj;
obj.getsp();
obj.display();
getch();
}
OUTPUT:

Constructor initialised

Enter the name


ARUN

Enter the phone number


6667070

Enter the spouse name


ARUNA

Name ARUN

Phone number 6667070

Spouse name ARUNA

******************
Q- Write a program to record a score of a cricket match. One array stores
information of
batting team such as batman's name,runs scored,etc. The other array stores
information
about bowling team. The progdram reads in above informatin and depending on
user's choice,
it displays either batting team's information or bowling team's information.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct bat
{
char name[20],modeout[70], indica;
int runs, score, totruns, totove, xtras;
};

struct bowl
{
char name[20];
int ttvrs,rnsgvn,wktstkn;
};
void main()
{
clrscr();
int plno;
int plytyp;
bat pl1[3];
bowl pl2[3];

cout<<"Enter the batsmen details:"<<endl;


for (int i=0;i<3;i++)
{
cout<<"Enter name of player "<<i+1<<endl;
gets (pl1[i].name);
cout<<"enter the runs scored by player "<<i+1<<endl;
cin>>pl1[i].runs;
cout<<"Enter the overs played by the player"<<i+1<<endl;
cin>>pl1[i].totove;
cout<<"Enter the status of the player if out (N)or not(Y)"<<endl;
cin>>pl1[i].indica;
}

cout<<"Enter the bowlers details "<<endl;


for (i=0;i<3;i++)
{
cout<<"Enter the name of the bowler "<<i+1<<endl;
gets(pl2[i].name);
cout<<"Enter the runs given by the bowler "<<i+1<<endl;
cin>>pl2[i].rnsgvn;
cout<<"Enter the wickets taken by the bowler "<<i+1<<endl;
cin>>pl2[i].wktstkn;
cout<<"Enter the total overs played by the bowler "<<i+1<<endl;
cin>>pl2[i].ttvrs;
}

cout<<"Thank you all details recd"<<endl;


xyz:
cout<<"Select between batsmen(1) or bowlers(2) to see their details"<<endl;
abc:
cin>>plytyp;

switch (plytyp)
{

case 1:
cout<<"Enter the batsman number to see his details "<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Batsman number :"<<plno+1<<endl;
cout<<"Batsman name :";
puts(pl1[plno].name);
cout<<"Runs scored by the batsman :"<<pl1[plno].runs<<endl;
cout<<"Total overs played by the batsman :"<<pl1[plno].totove<<endl;
cout<<"Player status out "<<pl1[plno].indica<<endl;
break;
case 2:
cout<<"Enter the bowlers number to see his details "<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Bowlers name :";
puts(pl2[plno].name);
cout<<"Runs given by the player is :"<<pl2[plno].rnsgvn<<endl;
cout<<"Total overs played by the player :"<<pl2[plno].ttvrs<<endl;
cout<<"Total wickets taken by the user :"<<pl2[plno].wktstkn<<endl;
break;

default:
cout<<"Idiot enter a decent value"<<endl;
goto abc;
}

cout<<endl<<endl<<endl<<"Do you wish to continue? Y-1 N-2"<<endl;


cin>>plno;
if (plno==1)
goto xyz;
else
cout<<"Thank you Press any key to exit";
getch();
}

output:

Enter the batsmen details:


Enter name of player 1
SACHIN
enter the runs scored by player 1
34
Enter the overs played by the player1
6
Enter the status of the player if out (N)or not(Y)
N
Enter name of player 2
GAMBHIR
enter the runs scored by player 2
12
Enter the overs played by the player2
5
Enter the status of the player if out (N)or not(Y)
Y
Enter name of player 3
SEHWAG
enter the runs scored by player 3
56
Enter the overs played by the player3
11
Enter the status of the player if out (N)or not(Y)
N
Enter the bowlers details
Enter the name of the bowler 1
31
Enter the wickets taken by the bowler 1
1
Enter the total overs played by the bowler 1
5
Enter the name of the bowler 2
JOHNSON
Enter the runs given by the bowler 2
25
Enter the wickets taken by the bowler 2
0
Enter the total overs played by the bowler 2
4
Enter the name of the bowler 3
MCGRATH
Enter the runs given by the bowler 3
40
Enter the wickets taken by the bowler 3
0
Enter the total overs played by the bowler 3
7
Thank you all details recd
Select between batsmen(1) or bowlers(2) to see their details
2
Enter the bowlers number to see his details

2
Bowlers name :JOHNSON
Runs given by the player is :25
Total overs played by the player :4
Total wickets taken by the user :0
Do you wish to continue? Y-1 N-2
Y
Select between batsmen(1) or bowlers(2) to see their details
Enter the bowlers number to see his details

Bowlers name :LEE


Runs given by the player is :31
Total overs played by the player :5
Total wickets taken by the user :1

Do you wish to continue? Y-1 N-2


Thank you Press any key to exit
Object Oriented Programming
Q- Raising a number n to a power p is the same as multiplying n by itself p times.
Write an overloaded functions having two versions for it. The first version
takes double n and int p and returns a double value. Another version takes int
n and int p returning int value. Use default value of 2 for p in case p is
ommitted in the function call.

#include<iostream.h>
#include<conio.h>

double power (double n,int p)


{
double res=1;
for (int i=0;i<p;i++)
res*=n;
return res;
}
int power (int n, int p)
{
int res=1;
for (int i=0;i<p;i++)
res*=n;
return res;
}

void main()
{
double x,ans;
int n;
clrscr();
cout<<"enter the number"<<endl;
cin>>x;
cout<<"enter the raising value"<<endl;
cin>>n;
ans=power(x,n);
cout<<"The answer is :"<<ans<<endl;
cout<<"The square of the numberis :"<<power(x,2);
getch();
}

OUTPUT:
enter the number
3
enter the raising value
4
The answer is :81
The square of the numberis :9
Q- Write overloaded prototypes of inquote(), a function that displays its arguments
enclosed in double quotation marks. Write three versions: one for an int
argument, one for double argument and one for char argument.

#include<iostream.h>
#include<conio.h>
void inquote(int x);
void inquote(double y);
void inquote(char s);

void main()
{
clrscr();
int a,c;
double b;
char s;

cout<<"Enter the
choice"<<endl<<"1.number"<<endl<<"2.double"<<endl<<"3.char"<<endl;
cin>>c;
switch (c)
{
case 1:
cout<<"Enter the number"<<endl;
cin>>a;
inquote(a);
break;
case 2:
cout<<"enter the double value"<<endl;
cin>>b;
inquote(b);
break;
case 3:
cout<<"enter the character"<<endl;
cin>>s;
inquote(s);
break;
}
getch();
}

void inquote(int x)
{
cout<<"\""<<x<<"\""<<endl;
}

void inquote(double y)
{
cout<<"\""<<y<<"\""<<endl;
}

void inquote(char s)
{
cout<<"\""<<s<<"\""<<endl;
}

OUTPUT:

Enter the choice


1.number
2.double
3.char

3
enter the character
T
"T"
Q- Write overloaded prototypes of themax(), a function that returns volumes of
different structures. Write three versions : one for cube's volume, one for
cylinder's volume and one for rectangular box's volume.

#include<iostream.h>
#include<conio.h>

double vol;
void volume(int r,int hc)
{ vol=(3.14*r*r*hc);
cout<<"\nVolume of cylinder is "<<vol;
}
void volume(int l,int b,int h)
{ vol=(l*b*h);
cout<<"\nVolume of box is "<<vol;
}
void volume(int s)
{ vol=(s*s*s);
cout<<"\nVolume of cube is "<<vol;
}

void main()
{
clrscr();
int s,r,l,b,h,hc;
cout<<"\nEnter radius and height of cylinder ";
cin>>r>>hc;
cout<<"\nEnter l,b,h of box ";
cin>>l>>b>>h;
cout<<"\nEnter side of cube ";
cin>>s;
volume(r,hc);
volume(l,b,h);
volume(s);
getch();
}
OUTPUT:

Enter radius and height of cylinder 4 7

Enter l,b,h of box 6 7 8

Enter side of cube 5

Volume of cylinder is 351.68


Volume of box is 336
Volume of cube is 125
Classes And Objects
Q- Write a program to calculate the no. of types of guests i.e gentlemen
or ladies or children using a class.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class counter
{
int count;
public:
counter()
{
count=0;
}
void incount()
{
count++;
}
int givecount()
{
return (count);
}
}c1,c2,c3;

void main()
{
clrscr();
char guest[10];
int i,a,b,c;
for(i=0;i<10;i++)
{
cout<<"enter gntlmn(g),ladies(l),chldrn(c) "<<endl;
cin>>guest[i];
if(guest[i]=='g'||guest[i]=='G')
{
c1.incount();
a=c1.givecount();
}
else if(guest[i]=='l'||guest[i]=='L')
{
c2.incount();
b=c2.givecount();
}
else
{
c3.incount();
c=c3.givecount();
}
}
cout<<"GENTLEMEN :"<<a<<endl;
cout<<"LADIES :"<<b<<endl;
cout<<"CHILDREN :"<<c<<endl;
getch();
}

OUTPUT:
enter gntlmn(g),ladies(l),chldrn(c)
c
enter gntlmn(g),ladies(l),chldrn(c)
g
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
g
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
c
enter gntlmn(g),ladies(l),chldrn(c)
c

GENTLEMEN :2
LADIES :5
CHILDREN :3

Q- Write a program to compute tax of a person using a class taxpayer. Class


taxpayer should the person's details of account including pan no.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class taxpayer{
int pan;
char name[20];
long float tableinc;
double tax;
public:
void inputdata();
void display();
double computetax();
};
void taxpayer::inputdata()
{
cout<<"enterpersonal acct num:";
cin>>pan;
cout<<"\nenter the name of the person:";
gets(name);
cout<<"\nenter total annual taxable income:";
cin>>tableinc;
}
double taxpayer::computetax()
{
if(tableinc<=60000)
tax=0;
else if((tableinc>60000)||(tableinc<=150000))
tax= tableinc *0.05;
else if(tableinc>150000||tableinc<=500000)
tax=tableinc*0.1;
else
tax=tableinc*0.15;
return (tax);
}

void taxpayer::display()
{
cout<<"\npan num:"<<pan<<"\tname:";
puts(name);cout<<"\ttotal annual income:"<<tableinc<<"\tTax payable:"<<tax;
}
void main()
{
clrscr();
taxpayer a;
a.inputdata();
clrscr();
a.computetax();
a.display();
getch();
}

OUTPUT:

enterpersonal acct num:121

enter the name of the ethiopian:ARUN

enter total annual taxable income:120000

pan num:121 name:ARUN


total annual income:120000 Tax payable:6000
Q- Wite a c++ program to calculate the no. of objects created of a particular
class type.

#include<iostream.h>
#include<conio.h>

class just
{
int x;
public:
void enter()
{ cout<<"Enter a value of x:";
cin>>x;
}
void out()
{
cout<<"The value is:"<<x<<endl;
}
};

int main()
{
just j;
char ch='y';
int i=0;
while(ch=='y')
{
j.enter();
i++;
cout<<"Do you eant to continue:";
cin>>ch;
}
cout<<"The number of objects created are:"<<i;
return 0;
}

OUTPUT:

Enter a value of x:4


Do you want to continue:y
Enter a value of x:7
Do you want to continue:n
The number of objects created are:2

Q- Write a c++ program using a class student to calculate the percentage of


marks obtained by him.

#include<iostream.h>
#include<conio.h>

class student
{
int roll_no;
char name[20];
char class_st[8];
int marks[5];
float percentage;
float calculate();
public:
void readmarks();
void displaymarks();
};

float student::calculate()
{
percentage=0;
for(int i=0;i<5;i++)
percentage+=marks[i];
percentage=(percentage/5);
return percentage;
}

void student::readmarks()
{
cout<<"Enter the roll no.:";
cin>>roll_no;
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the class studing in:";
cin>>class_st;
cout<<"Enter the marks:"<<endl;
for(int j=0;j<5;j++){
cout<<"\tEnter mark "<<j+1<<":";
cin>>marks[j];
}
}

void student::displaymarks()
{
cout<<"Roll no:"<<roll_no<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Class:"<<class_st<<endl;
cout<<"Percentage:"<<calculate()<<endl;
}

int main()
{
student s1;
s1.readmarks();
s1.displaymarks();
return 0;
}

OUTPUT:
Enter the roll no.:12
Enter the name:KARTHIK
Enter the class studing in:12
Enter the marks:
Enter mark 1:99
Enter mark 2:95
Enter mark 3:90
Enter mark 4:80
Enter mark 5:99

Roll no:12
Name:KARTHIK
Class:12
Percentage:92.599998
Q- Write a c++ program using class serial to store and display serial's title,
duration, number of episodes,serial code.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class serial
{
int code;
char title[20];
float duration;
int noe;
public:
serial()
{
duration=30;
noe=103;
}
void newserial();
void otherentries(int dur,int no);
void displaydata();
};

void serial::newserial()
{
cout<<"\n\nEnter the serial code:";
cin>>code;
cout<<"Enter the title:";
gets(title);
}

void serial::otherentries(int dur,int no)


{
duration=dur;
noe=no;
}

void serial::displaydata()
{
cout<<"\tSerial code is:"<<code<<endl;
cout<<"\tTitle is:"<<title<<endl;
cout<<"\tDurations is:"<<duration<<endl;
cout<<"\tNo. of episodes are:"<<noe<<endl<<endl;
}

int main()
{
clrscr();
char ch='y';
int i=0,dur,no;
serial s1[10];
while(ch=='y')
{
s1[i].newserial();
cout<<"Enter the duration and the no. of episodes:";
cin>>dur>>no;
s1[i].otherentries(dur,no);
i++;
cout<<"\n\nDo you want to continue:";
cin>>ch;
}
cout<<"\n\nThe details you have entered are:"<<endl<<endl;
for(int j=0;j<i;j++){
cout<<"Data of serial "<<j+1<<" is:"<<endl;
s1[j].displaydata();
}
return 0;
}

OUTPUT:

Enter the serial code:121


Enter the title:DaVinciCode
Enter the duration and the no. of episodes:30 50
Do you want to continue:y

Enter the serial code:122


Enter the title:solomon
Enter the duration and the no. of episodes:60 20

Do you want to continue:n

The details you have entered are:

Data of serial 1 is:


Serial code is:121
Title is:DaVinciCode
Durations is:30
No. of episodes are:50

Data of serial 2 is:


Serial code is:122
Title is:solomon
Durations is:60
No. of episodes are:20
Q- Write a c++ program to illustrate a calculator. Perform calculations on two
operands using a class calculator. Calculator should add,subtract, multiply and
divide operands.

#include<iostream.h>
#include<conio.h>
#include<process.h>

class calculator
{
float result;
int o1,o2;
public:
void enter();
void showresult();
void add();
void sub();
void mul();
void div();
void clear();
};

void calculator::enter()
{
cout<<"Enter a operant:";
cin>>o1;
cout<<"Enter the other operant:";
cin>>o2;
}

void calculator::showresult()
{
cout<<"The result of the operation is:"<<result<<endl;
}

void calculator::add()
{
result=o2+o1;
}

void calculator::sub()
{
result=o1-o1;
}

void calculator::mul()
{
result=o1*o2;
}

void calculator::div()
{
result=o1/o2;
}

void calculator::clear()
{
result=0;
}

int main()
{
char ch='y';
calculator c1;
while(ch=='y')
{
c1.enter();
cout<<"Which operation do you want to perform:";
cin>>ch;
switch(ch)
{
case '+':c1.add();
break;
case '-':c1.sub();
break;
case '*':c1.mul();
break;
case '/':c1.div();
break;
default :cout<<"Wrong choice:";
exit(0);
}
c1.showresult();
c1.clear();
cout<<"Do you want to continue:";
cin>>ch;
}
return 0;
}

OUTPUT:

Enter a operand:4
Enter the other operand:8
Which operation do you want to perform:*
The result of the operation is:32
Do you want to continue:y
Enter an operand:4
Enter the other operand:8
Which operation do you want to perform:+
The result of the operation is:12
Do you want to continue:n
Q- Imagine a ticket selling booth at a fair. People passing by are requested to
purchase a ticket. A ticket is priced at Rs. 2.50. The booth keeps track of the
number of people that have visited the fair and of the total amount of money
collected. Model this ticketselling booth with a class tick. Include a program
to rest this class.

#include<iostream.h>
#include<conio.h>
#include<process.h>

class tick
{
int nop;
float total;
public:
tick()
{
nop=0;
total=0;
}
void inc();
void display();
void displaynop();
};

void tick::inc()
{
nop++;
total+=2.50;
cout<<"\nThe no. of people and the total have beem incremented";
}
void tick::display()
{
cout<<"\nThe number of people who have entered the fair are:"<<nop<<endl;
cout<<"The total amount collected till now is:"<<total<<endl;
}

void tick::displaynop()
{
cout<<"The no. of people who have visited so far are:"<<nop<<endl;
}

int main()
{
char ch='y';
int choice;
tick t1;

l1:cout<<"\n\n\n\n1.Increment person and total"<<endl;


cout<<"2.Display no. of people and the amount collected till now"<<endl;
cout<<"3.Display no. of people who entered"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:t1.inc();
goto l1;
break;
case 2:t1.display();
goto l1;
break;
case 3:t1.displaynop();
goto l1;
break;
case 4:exit(0);
break;

}
return 0;
}

OUTPUT:
1.Increment person and total
2.Display no. of people and the amount collected till now
3.Display no. of people who entered
4.Exit
Enter your choice:1

The no. of people and the total have beem incremented

1.Increment person and total


2.Display no. of people and the amount collected till now
3.Display no. of people who entered
4.Exit
Enter your choice:1

The no. of people and the total have beem incremented

1.Increment person and total


2.Display no. of people and the amount collected till now
3.Display no. of people who entered
4.Exit
Enter your choice:2

The number of people who have entered the fair are:2


The total amount collected till now is:5

1.Increment person and total


2.Display no. of people and the amount collected till now
3.Display no. of people who entered
4.Exit
Enter your choice:3
The no. of people who have visited so far are:2

1.Increment person and total


2.Display no. of people and the amount collected till now
3.Display no. of people who entered
4.Exit
Enter your choice:4

Constructors And Destructors


Q- A book shop maintains the inventory of books that are being sold at the shop.
The list includes details such as author, title, price, publisher and stock
position. Whenever a customer wants a book, the sales person inputs the title
and author and the system searches the list and displays whether it is available
or not.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class stock
{
char author[50];
char title[50];
char pub[50];
double price;
int numcopies;
public:
stock();
int access_title(char a[]);
void input();
void getdata(int);

};
stock::stock()
{
char author[50]={"abc"};
char title[50]={"efg"};
char pub[50]={"hij"};
price=500;
numcopies=50;
}
int stock::access_title(char a[])
{
if(strcmp(title,a))
return 0;
else return 1;
}
void stock::getdata(int num)
{
if(numcopies>=num)
cout<<"\nCost of "<<num<<" books is Rs. "<<(price*num);
else
cout<<"\nSorry! These many copies are unavailable!";
}
void stock::input()
{
cout<<"\nTitle: ";
gets(title);
cout<<"\nAuthor:";
gets(author);
cout<<"\nPublisher:";
gets(pub);
cout<<"\nPrices:";
cin>>price;
cout<<"\ncopies available:";
cin>>numcopies;
}

void main()
{
clrscr();

stock obj[2];
int n;
char ttle[50];

cout<<"Enter details of 3 books";


for(int i=0;i<2;++i)
obj[i].input();

cout<<endl;
cout<<"\n Enter title of required book\n";
gets(ttle);

for(i=0;i<2;i++)
{
if(obj[i].access_title(ttle))
{
cout<<"\nHow many copies? ";
cin>>n;
obj[i].getdata(n);
}
else
cout<<"\nBook unavailable";
}
getch();
}

OUTPUT:

Enter details of 3 books


Title: Da Vinci Code

Author:Dan Brown

Publisher:Sun

Prices:455

copies available:300

Title: Harry Potter

Author:J K Rowling

Publisher:Bloomsbury

Prices:800

copies available:100

Enter title of required book


HarryPotter

Book available
How many copies? 20

Cost of 20 books is Rs. 16000


Data File Handling
Q- Write a c++ program, which initialises a string variable to the content.
"time is great teacher but unfortunately it kills all its pupils. Berlioz"
and output the string one character atra time to the disk file.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>

int main()
{
char str[]="Time is a great teacher but unfortunately it kills all its
pupils.Berlioz",ch;
int i;
ofstream fout;
fout.open("OUT.TXT",ios::out);
int len=strlen(str);
for(i=0;i<len;i++){
ch=str[i];
fout.put(ch);
}
cout<<"The string has been successfully written into the file";
return 0;
}

OUTPUT:

The string has been successfully written into the file


Q- Write a user defined function in c++ to read the content from a text file
STORY.TXT , count and display the no. of alphabets present in it.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
#include<stdio.h>

void main()
{ clrscr();
char str[50];
fstream file;
file.open("Story.txt",ios::in|ios::out|ios::trunc);
cout<<"\nEnter string\n";
gets(str);
int i;
for(i=0;str[i]!='\0';i++)
file.put(str[i]);
file.seekg(0,ios::beg);
int ctr=0;
for(i=0;str[i]!='\0';i++)
{ if(isalpha(str[i]))
ctr++;
}
cout<<"\nNumber of alphabets is\t"<<ctr;
getch();
}

OUTPUT:

Enter string
KARTHIKRAO88.RAO

Number of alphabets is 13

Q- Assuming a binary file JOKES.TXT is containing objects belonging to a class


JOKE, write user defined function in c++ to add more objects belonging to class
JOKE at the bottom of it.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class joke{
int joke_id;
char type[5];
char joke_desc[200];
public:
void newjoke()
{
cout<<"\nEnter joke id:";
cin>>joke_id;
cout<<"\nEnter joke type:(max 4 letters)";
gets(type);
cout<<"\nEnter the joke:\n";
gets(joke_desc);
}
void showjoke()
{
cout<<"\nJoke id:"<<joke_id;
cout<<"\nJoke type:";
puts(type);
cout<<"\nJoke:\n";
puts(joke_desc);
cout<<"HAHAHA";
}
};
joke jo;
void add_joke()
{
fstream file;
file.open("joke",ios::binary|ios::app|ios::in|ios::out);
int n,i;
cout<<"\nHow many jokes?";
cin>>n;
for(i=0;i<n;i++)
{ jo.newjoke();
file.write((char*)&jo,sizeof(jo));
}
file.close();
}
void main()
{
clrscr();
cout<<"HAHAHA";
add_joke();

jo.showjoke();
getch();
}

OUTPUT:

HAHAHA
How many jokes?2

Enter joke id:1

Enter joke type:(max 4 letters)HUMR

Enter the joke:


ABCDEFGHIJKLM

Enter joke id:2

Enter joke type:(max 4 letters)HUMR

Enter the joke:


QWERTYUIO

Joke id:2
Joke type:HUMR
Joke:
QWERTYUIO
HAHAHA

POINTERS
Q- Suppose 7 names are stored in an array of pointers names[] as shown below.
Write a program to reverse the order of these names.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[]={"anand","naureen","banjot","wahid","sheena"};
int i,j;
cout<<"\nOriginal string\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
char *t;
for(i=0,j=4;i<5/2;i++,j--)
{
t=name[i];
name[i]=name[j];
name[j]=t;
}
cout<<"\nReversed string:\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
getch();
}

OUTPUT:

Original string
anand
naureen
banjot
wahid
sheena

Reversed string:
sheena
wahid
banjot
naureen
anand

Q- Write a function to encode a string that is passed to it. The string should
get converted into an unrecognizable form.

#include<iostream.h>
#include<conio.h>
#include<string.h>

#include<stdio.h>

void encode(char str1[],int l);

int main()
{
char str[30];
cout<<"Enter an string:";
gets(str);
int len=strlen(str);
encode(str,len);
return 0;
}

void encode(char str1[],int l)


{
int i;
for(i=0;i<l;i++){
str1[i]=str1[i]+10;
}
cout<<"\nThe encoded string is:"<<endl;
for(i=0;i<l;i++){
cout<<str1[i];
}
cout<<"\nThe proper string is:"<<endl;
for(i=0;i<l;i++)
str1[i]=str1[i]-10;
for(i=0;i<l;i++)
cout<<str1[i];
}

OUTPUT:

Enter an string:KARTHIK

The encoded string is:


UK\^RSU
The proper string is:
KARTHIK
Q- Write a function search() which scans a string from beginning to end in
search of character. If the character is found it should return a pointer to
the first occurence of the given character in the string. Ig\f the given
character is not found in the string the function should return NULL.
The prototype if the function is char * search(char *,char);

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

char *search(char *,char);


int len;

int main()
{
char str[20],ch,*f;
cout<<"Enter a string:";
gets(str);
cout<<"Enter the character to be searched for:";
cin>>ch;
len=strlen(str);
f=search(str,ch);
if(f!=NULL)
cout<<"Character is found.";
else
cout<<"Character is not found.";
return 0;
}
char *search(char *x,char d)
{
int i;
for(i=0;i<len;i++){
if(*x==d)
return x;
x++;
}
return NULL;
}

OUTPUT:

Enter a string:KARTHIK
Enter the character to be searched for:R
Character is found.
Arrays
Q- Write a c++ program to sort an array of numbers, display the greatest and
smallest numbers of the array, delete the array and shift the rest of the elements.
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int temp,ctr,i,j,a[20],size,bg,mn,sum=0,even=0,ch;
cout<<"Enter the size of the array:";
cin>>size;
cout<<"Enter the array:";
for(i=0;i<size;i++)
cin>>a[i];
for(i=0;i<size;i++){
for(j=0;j<size-1-i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"The sorted array is:"<<endl;
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
bg=a[0];
for(j=0;j<size;j++){
if(bg<a[j])
bg=a[j];
}
mn=a[0];
for(j=0;j<size;j++){
if(a[j]%2==0)
even++;
sum+=a[j];
if(mn>a[j])
mn=a[j];
}
cout<<"The maximum value is:"<<bg<<endl;
cout<<"The minimum value is:"<<mn<<endl;
cout<<"The sum of the array is:"<<sum<<endl;
cout<<"The no. of even values is:"<<even<<endl;
for(i=0;i<size;i++){
//ch=a[i];
for(j=i+1;j<size;j++){
if(a[i]==a[j])
a[j]=0;
}
}
cout<<"The array now is(after removing the duplicate elements):";
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
for(i=0;i<size;i++){
if(a[i]==0){
for(j=i+1;j<size;j++){
a[j-1]=a[j];
a[j]=0;
}
}
}
cout<<"The array now is(after shifting the elements):"<<endl;
for(i=0;i<size;i++){
if(a[i])
cout<<a[i]<<" ";
}
return 0;
}

OUTPUT:

Enter the size of the array:5


Enter the array:5
1
4
2
3

The sorted array is:


12345

The maximum value is:5


The minimum value is:1
The sum of the array is:15
The no. of even values is:2

The array now is(after removing the duplicate elements):1 2 3 4 5


The array now is(after shifting the elements):
12345

Q- Write a c++ program to


-find row sum
-find column sum
-find transpose of matrix
-add 2 matrices
-subtract two matrices
-multiply 2 matrices
-find sum of 2 diagonals

#include<iostream.h>
#include<conio.h>
#include<process.h>

int main()
{
int a[20][20],b[20][20],i,c,r,ch,rsum[20],r1,c1,j,csum[20],x[20]
[20],ip,majsum=0,minsum=0;
char choice='y';
cout<<"Enter the no. of rows of the matrix:";
cin>>r;
cout<<"Enter the no. of columns of the matrix:";
cin>>c;
cout<<"Enter an array:"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
cin>>a[i][j];
}
}
while(choice=='y')
{
cout<<"1.Product"<<endl;
cout<<"2.Transpose"<<endl;
cout<<"3.Row sum and columns sum"<<endl;
cout<<"4.Exit"<<endl;
cout<<"5.Sum of diagonals:"<<endl;
cout<<"6.Addition of the matrices:"<<endl;
cout<<"7.Subtraction of the matrices:"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the no. of rows of the other matrix:";
cin>>r1;
cout<<"Enter the no. of columns of the other matrix:";
cin>>c1;
cout<<"Enter the matrix:";
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
cin>>b[i][j];
}
}
if(c!=r1){
cout<<"The matrices cannot be multiplyed.";
break;
}
else
{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
x[i][j]=0;
for(ip=0;ip<r1;ip++){
x[i][j]+=(a[i][ip]*b[ip][j]);
}
}
}
}
cout<<"The product is:"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c1;j++){
cout<<x[i][j]<<" ";
}
cout<<endl;
}
break;
case 2:for(i=0;i<r;i++){
for(j=0;j<c;j++){
cout<<a[j][i]<<" ";
}
cout<<endl;
}
break;
case 3:for(i=0;i<r;i++)
rsum[i]=0;
for(i=0;i<c;i++)
csum[i]=0;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
rsum[i]+=a[i][j];
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
csum[i]+=a[j][i];
}
}
for(i=0;i<r;i++)
cout<<"The sum of row "<<i+1<<" is:"<<rsum[i]<<endl;
for(i=0;i<c;i++)
cout<<"The sum of column "<<i+1<<" is:"<<csum[i]<<endl;
break;
case 4:exit(0);
break;

case 5:for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(i==j)
majsum+=a[i][j];
else if((i+j)==(c-1))
minsum+=a[i][j];
}
}
cout<<"Major sum is:"<<majsum<<endl;
cout<<"The sum of the minor diagonal is:"<<minsum<<endl;
break;

case 6:cout<<"Enter the no of rows of the matrix:";


cin>>r1;
cout<<"Enter the no. of columns of the matrix:";
cin>>c1;
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
cin>>b[i][j];
}
}
if((r==r1)&&(c==c1)){
cout<<"Matrices can be added:"<<endl;
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
x[i][j]=a[i][j]+b[i][j];}
}
}
else
cout<<"Matrices cannot be added:"<<endl;
break;

case 7:cout<<"Enter the no of rows of the matrix:";


cin>>r1;
cout<<"Enter the no. of columns of the matrix:";
cin>>c1;
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
cin>>b[i][j];
}
}
if((r==r1)&&(c==c1)){
cout<<"Matrices can be subtracted:"<<endl;
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
x[i][j]=a[i][j]-b[i][j];}
}
}

else
cout<<"Matrices cannot be subtracted:"<<endl;
break;

}
cout<<"want to continue:";
cin>>choice;
}
return 0;
}

OUTPUT:

Enter the no. of rows of the matrix:3


Enter the no. of columns of the matrix:3

Enter an array:
1
2
3
4
5
6
7
8
9

1.Product
2.Transpose
3.Row sum and columns sum
4.Exit
5.Sum of diagonals:
6.Addition of the matrices:
7.Subtraction of the matrices:

Enter your choice:1

Enter the no. of rows of the other matrix:2


Enter the no. of columns of the other matrix:3

Enter the matrix:


1
2
3
4
5
6
The matrices cannot be multiplied.
want to continue:N
Q- Write a program to find the transpose of a matrix

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],i,j,r1,r2,c1,c2;
cout<<"enter rows and column\n";
cin>>r1>>c1;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"\n";
cout<<"this matrix a\n";

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<"\t"<< a[i][j];
}
cout<<"\n";
}
cout<<"Tronspose\n";
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
cout<<"\t"<<b[i][j];
}

cout<<"\n";
}

getch();
}

OUTPUT:
enter rows and column
3
4

1
2
3
4
5
6
7
8
9
10
12
13

this matrix a
1 2 3 4
5 6 7 8
9 10 12 13

Tronspose
1 5 9
2 6 10
3 7 12
4 8 13
Q- Write a function that will scan a string for the occurence of a given string for
substring.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
char *substr(char *string1,char *string2)
{
char *cp;
cp=NULL;
int n=0;
while(*string1!='\0')
{
if(*string1==*string2)
{
n=1;
cp=string1;
while(*string2!='\0')
{
if(*string1!=*string2)
{
n=0;
cp=NULL;
break;
}
string1++;
string2++;
}
}
string1++;
}
return cp;
}
void main()
{
clrscr();
char str[50],sub[50];
char *pos;
cout<<"\nEnter a string \n";
gets(str);
cout<<"\nEnter a string to searched\n";
gets(sub);
pos=substr(str,sub);
if(*pos)
{
cout<<"\nString found\n";
}
else
{
cout<<"\nString not found\n";
}
getch();
}

OUTPUT:

Enter a string
KARTHIK RAO 88

Enter a string to searched


RAO

String found
WRITE A PROGRAM TO COUNT AND DISPLAY THE NUMBER OH LINES NOT
STARTING WITH ALPHABET 'A' PRESENT IN THE TEXT FILE "STORY .TXT

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
int nooflines()
{
ifstream fin("STORY.TXT");
int number=-1;
char line[50];
while(!fin.eof())
{
fin.getline(line,50,'.');
for(int i=0;i<50;i++)
{
if((line[i]!='a'&&line[i]!='A')&&(line[i-1]=='\r'))
number++;
}
}
cout<<"\nNumber of lines="<<" "<<number;
}
void main()
{
clrscr();
ofstream fout;
char str[500];
fout.open("STORY.TXT");
cout<<"\nEnter a string\n";
gets(str);
fout<<str<<"\n";
fout.close();
nooflines();
getch();
}
output:

Enter a string
The rose is red.Agirl is playing there. There is a playground.An aeroplane is in
the sky.

Number of lines= 4

Linked lists, Stacks And Queues


Q- Write a c++ program that depending on user's choice, either pushes or pops an
element in a stack implemented as a linked list.

#include<iostream.h>
#include<conio.h>

struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr;

node *create_new_node(int );

void insert_beg(node *);


void del(void);
void display (void);

void main()
{
start=NULL;
int inf,ch;
char c='y';
clrscr();
while (c=='y'||c=='Y')
{

cout<<"1.insert\n2.delete\n3.display"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the info"<<endl;
cin>>inf;
newptr= create_new_node(inf);
insert_beg(newptr);
cout<<"Sucess"<<endl;
break;
case 2:
del();
break;
case 3:
display();
break;
}
cout<<"y-continue n-exit"<<endl;
cin>>c;
}
}

node* create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}

void insert_beg(node *pt)


{
if (start==NULL)
start=pt;
else
{
save=start;
pt->next=save;
start=pt;
}
}

void del(void)
{
if (start==NULL)
cout<<"underflow"<<endl;
else
{
save=start;
start=start->next;
delete save;
}
}

void display(void)
{
ptr=start;
cout<<"Start";
while(ptr!=NULL)
{
cout<<"->"<<ptr->info;
ptr=ptr->next;
}
cout<<endl;
}

OUTPUT:

1.insert
2.delete
3.display
1
Enter the info
34
Sucess
y-continue n-exit
y

1.insert
2.delete
3.display
1

Enter the info


56
Sucess

y-continue n-exit
y

1.insert
2.delete
3.display
1
Enter the info
678
Sucess
y-continue n-exit
y

1.insert
2.delete
3.display
3
Start->678->56->34
y-continue n-exit
y

1.insert
2.delete
3.display
2
y-continue n-exit
y

1.insert
2.delete
3.display
3
Start->56->34
y-continue n-exit
n
Q- Write a c++ program to convert infix expression to postfix form.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
const int size=50;
char infix[size],stack[size],postfix[size],oper,temp,p;
int top =-1;

void main()
{
clrscr();

int brace,i,j=0;

char pop();

void push (char c);


int precidence(char ch);
int braces(char *);
cout<<"enter the infix exp"<<endl;
gets(infix);
brace=braces(infix);
if (brace!=0)
{
cout<<"The number of braces are not equal"<<endl;
exit(1);
}
//converts the given exp from infix to post fix
for (i=0;infix[i]!='\0';i++)
{
if (infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-'&&infix[i]!
='^'&&infix[i]!='('&&infix[i]!=')')
{
postfix[j++]=infix[i]; //add the operand to post fix
}
else if (infix[i]=='(') //add to stack
{
temp=infix[i];
push(temp);
}
else if (infix[i]==')') //repeadly pop from stack and add to post fix each
operator until a left paranthesis is encountered
{
while((p=pop())!='(')
{
postfix[j++]=p;
}
}
else if (top!=-1)
{
oper=pop(); //if a operator is encounted
while (precidence (oper)>=precidence(infix[i])) //chk precidence
{
postfix[j++]=oper;
oper=pop(); //repeatdely pop from stck and add to post fix
}
push(oper); //which has same or higher precsidence than the operator
push(infix[i]);
}
else //if oper has lower precidence than the character scanned then the loop is
terminated and oper is pushed back to the stack
{
push(infix[i]);
}
}

//displays the postfix form of the given expression

while((p=pop())!='$')
{
postfix[j++]=p;
postfix[j]='\0';
cout<<"postfix expression is "<<postfix<<endl;
}
getch();
}

int braces(char *s)


{
int leftb,rightb,i;
leftb=rightb=0;
for (i=0;s[i]!='\0';i++)
{
if (s[i]=='(')
leftb++;
else if (s[i]==')')
rightb++;
}
if (leftb==rightb)
return 0;
else if (leftb<rightb)
return 1;
else
return -1;
}

char pop()
{
char ch;
if (top==-1)
return'$';
else
{
ch=stack[top];
top--;
return (ch);
}
}

void push(char c)
{
if (top==size)
cout<<"stck overflow"<<endl;
else
{
top++;
stack [top]=c;
}
}

int precidence(char ch)


{
if (ch=='^')
return 3;
else if (ch=='*'||ch=='/')
return 2;
else
{
if (ch=='+'|| ch=='-')
return 1;
else
return 0;
}
}

OUTPUT

Enter the infix exp:


(A-C)*B

Postfix expression is AC-B*


SCREENSHOTS
End User Application

Computer has become a necessity of


every aspect of life. With such great dependence on
computers for our work, it is essential to master them.
Typing is a basic skill in using computers. Typing
tutor is therefore intended to improve user’s typing
skills by providing essential data regarding their
typing habits. It can be used by every person who
wants to operate computer via a QWERTY standard
keyboard.
CONTENTS

Certificate
Acknowledgement
System Requirements
Introduction
Data Dictionary
Source Code
End User Application
Screenshots
Bibliography

You might also like