Вы находитесь на странице: 1из 7

// program 1: program to create class employee and calculate netsalary

#include<iostream.h>
#include<ctype.h>

class employee
{
char emp_no[5],name[20];
double basic,da,it,gross,netsal;

public:
void read()
{
up: cin>>emp_no;
int i=0;
while(emp_no[i]!='\0')
{
if(!isdigit(emp_no[i++]))
{
cout<<"invalid employee no\nplease reenter the no :";
goto up;
}
}
cin>>name>>basic;
calsal();
}
void display()
{
cout<<emp_no<<"\t"<<name<<" "<<basic
<<"\t"<<da<<"\t"<<it<<"\t"<<gross<<"\t"
<<netsal<<"\n";
}
void calsal()
{
da=0.52*basic;
gross=basic+da;
it=0.32*gross;
netsal=gross-it;
}
};

int main()
{
cout<<"enter no. of employees : ";
int n;
cin>>n;
employee e[10];
cout<<"\nemp_no\tname\tbasic\n"
<<"--------------------------------\n";
for(int i=0;i<n;i++)
e[i].read();
cout<<"\nemp_no\tname\tbasic\tda\tit\tgross\tnet_sal\n"
<<"------------------------------------------------------\n";
for( i=0;i<n;i++)
e[i].display();
return 0;
}
/* -------- output --------

enter no. of employees : 5

emp_no name basic


--------------------------------
1 vishu 5000
2 venki 6000
3 rock 7000
4 vijay 6000
5 patil 5000

emp_no name basic da it gross net_sal


------------------------------------------------------
1 vishu 5000 2600 2432 7600 5168
2 venki 6000 3120 2918.4 9120 6201.6
3 rock 7000 3640 3404.8 10640 7235.2
4 vijay 6000 3120 2918.4 9120 6201.6
5 patil 5000 2600 2432 7600 5168

*/
plain text attachment [ scan and save to computer | save to yahoo! briefcase ]

/* program 15 : program to convert given infix expression to


postfix expression
*/

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

class exp
{
char s[100],pfix[100],inf[100],post[100];
int top;
public:
exp(char in[50])
{
strcpy(inf,in);
top=-1;
}
void push(char c);
char pop();
int st_precedence(char c);
int ip_precedence(char c);
void inf_pf();
void display();
};

void exp::push(char c)
{
s[++top]=c;
}
char exp::pop()
{
return(s[top--]);
}

int exp::st_precedence(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 exp::ip_precedence(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 exp::inf_pf()
{
int i,j=0;
char symb;
push('#');
for(i=0;i<strlen(inf);i++)
{
symb=inf[i];
while((st_precedence(s[top]))>(ip_precedence(symb)))
pfix[j++]=pop();
if((st_precedence(s[top]))!=(ip_precedence(symb)))
push(symb);
else
pop();
}
while(s[top]!='#')
pfix[j++]=pop();
pfix[j]='\0';
}
void exp::display()
{
cout<<pfix;
}

void main()
{
char infix[50];
cout<<"enter a valid exp : ";
cin>>infix;
exp exp=infix;
exp.inf_pf();
cout<<"\n\npostfix exp is :\n";
exp.display();
}

/* ---------- output -------------

enter a valid exp :((a+b)*c)$d

postfix exp is :ab+c*d$

*/
plain text attachment [ scan and save to computer | save to yahoo! briefcase ]

// program 4: program to implement linked list.

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

class list
{
int data;
list *next;
list *start;
public:
list()
{
start=null;
}
void insert_front();
void delete_front();
void display();
};

void list :: insert_front()


{
list *t=new list;
cout<<"\nenter the element to be inserted :";
cin>>t->data;
t->next=null;
if(start==null)
start=t;
else
{
t->next =start;
start=t;
}
}

void list::delete_front()
{
list *t;
t=start;
if(t==null)
{
cout<<"\nlist is empty\n";
return ;
}
else
{
start=t->next;
cout<<"\ndeleted element is : "<<t->data;
delete t;
}
cout<<endl;
}

void list ::display ()


{
list *t;
t=start;
if(start==null)
{
cout<<"\nlist is empty\n";
return ;
}
cout<<"\nthe list contents are : s";
while(t!=null)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
}

int main()
{
int ch;
list l;
while(1)
{
cout<<"\n-------- menu --------\n"
<<"1.insert.\n2.delete.\n3.display.\n4.exit.\n"
<<"----------------------\n"
<<"enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: l.insert_front();
break;
case 2: l.delete_front();
break;

case 3: l.display();
break;

case 4: exit(0);
}
}
return 0;
}

/* -------- output ---------

-------- menu --------


1.insert.
2.delete.
3.display.
4.exit.
----------------------
enter your choice : 1

enter the element to be inserted :10

-------- menu --------


1.insert.
2.delete.
3.display.
4.exit.
----------------------
enter your choice : 1

enter the element to be inserted :20

-------- menu --------


1.insert.
2.delete.
3.display.
4.exit.
----------------------
enter your choice : 1

enter the element to be inserted :30

-------- menu --------


1.insert.
2.delete.
3.display.
4.exit.
----------------------
enter your choice : 1

enter the element to be inserted :40


-------- menu --------
1.insert.
2.delete.
3.display.
4.exit.
----------------------

Вам также может понравиться