#include<iostream.h>
class Price
{
private:
int rupees;
int paisa;
public:
Price()
{
rupees=0;
paisa=0;
};
Price operator +(Price& p)
{
Price e;
int t1,t2,t3=0;
t1=paisa+p.paisa;
if(t1>=100)
{
t2=t1/100;
t3=t1%100;
e.paisa=t3;
e.rupees=p.rupees+t2+rupees;
}
else if(t1<100)
{
e.paisa=t1;
e.rupees=p.rupees+rupees;
}
return e;
};
Price(int pr,int pa)
{
int tmp=0;
int tmp2;
if(pa<100)
{
paisa=pa;
}
else if(pa>=100)
{
tmp=pa/100;
tmp2=pa%100;
paisa=tmp2;
}
rupees=pr+tmp;
};
void print()
{
cout<<"Price is "<<rupees<<" rupees and "<<paisa<<" paisas"<<endl;
};
};
main()
{
Price l(10,60),m(12,80);
l.print();
m.print();
cout<<"After Addition "<<endl;
Price a = l + m;
a.print();
cout<<"\n\n\n\n\n"<<endl;
system("pause");
}
Sunday, January 23, 2011
Data Structure CS301____________4
#include<iostream.h>
#include<ctype.h>
class heap
{
public:
heap( int);
void insert( const int &);
const int& get_delMin(int &);
void print(int[],int);
private:
int Size;
int* array;
int cap;
void percolateDown( int hole );
};
void heap::print(int in[],int size )
{
cout<<"\tInserted Elements\tSorted Elements\n"<<endl;
for(int i=1;i<=size-1;i++)
{
cout<<"\t\t"<<in[i]<<"\t\t\t"<<this->get_delMin(array[0])<<endl;
}
}
heap::heap( int cap)
{
array = new int[cap + 1];
Size=0;
}
void heap::insert( const int & x )
{
int hole = ++Size;
for(; hole > 1 && x < array[hole/2 ]; hole /= 2)
array[ hole ] = array[ hole / 2 ];
array[hole] = x;
}
const int& heap::get_delMin(int& x )
{
x = array[ 1 ];
array[ 1 ] = array[ Size-- ];
percolateDown( 1 );
return x;
}
void heap::percolateDown( int hole )
{
int child;
int tmp = array[ hole ];
for( ; hole * 2 <= Size;hole = child)
{
child = hole * 2;
if( child != Size && array[child+1] < array[ child ] )
child++;
if( array[ child ] < tmp )
array[ hole ] = array[ child ];
else
break;
}
array[ hole ] = tmp;
}
main(int argc, char *argv[])
{
if (argc > 1)
{
if(isdigit(*(argv[1])))
{
heap h(argc);
int ar2[argc];
for(int i=1;i<=argc;i++)
{
ar2[i]=atoi((argv+i)[0]);
}
for(int i=0;i<argc;i++)
{
h.insert(ar2[i]);
}
cout<<"\n\tSorted Elements are:\n"<<endl;
h.print(ar2,argc);
}
}
else if(argc<=1)
{
cout<<"Heap is empty"<<endl;
cout<<" or"<<endl;
cout<<"Zero Elements are inserted through Command Prompt"<<endl;
cout<<"Please open it through Command Prompt & "<<endl;
cout<<"Insert Elements Through Command Line Argument\n\n\n\n\n"<<endl;
}
cout<<"\n\n\n\n";
system("pause");
}
#include<ctype.h>
class heap
{
public:
heap( int);
void insert( const int &);
const int& get_delMin(int &);
void print(int[],int);
private:
int Size;
int* array;
int cap;
void percolateDown( int hole );
};
void heap::print(int in[],int size )
{
cout<<"\tInserted Elements\tSorted Elements\n"<<endl;
for(int i=1;i<=size-1;i++)
{
cout<<"\t\t"<<in[i]<<"\t\t\t"<<this->get_delMin(array[0])<<endl;
}
}
heap::heap( int cap)
{
array = new int[cap + 1];
Size=0;
}
void heap::insert( const int & x )
{
int hole = ++Size;
for(; hole > 1 && x < array[hole/2 ]; hole /= 2)
array[ hole ] = array[ hole / 2 ];
array[hole] = x;
}
const int& heap::get_delMin(int& x )
{
x = array[ 1 ];
array[ 1 ] = array[ Size-- ];
percolateDown( 1 );
return x;
}
void heap::percolateDown( int hole )
{
int child;
int tmp = array[ hole ];
for( ; hole * 2 <= Size;hole = child)
{
child = hole * 2;
if( child != Size && array[child+1] < array[ child ] )
child++;
if( array[ child ] < tmp )
array[ hole ] = array[ child ];
else
break;
}
array[ hole ] = tmp;
}
main(int argc, char *argv[])
{
if (argc > 1)
{
if(isdigit(*(argv[1])))
{
heap h(argc);
int ar2[argc];
for(int i=1;i<=argc;i++)
{
ar2[i]=atoi((argv+i)[0]);
}
for(int i=0;i<argc;i++)
{
h.insert(ar2[i]);
}
cout<<"\n\tSorted Elements are:\n"<<endl;
h.print(ar2,argc);
}
}
else if(argc<=1)
{
cout<<"Heap is empty"<<endl;
cout<<" or"<<endl;
cout<<"Zero Elements are inserted through Command Prompt"<<endl;
cout<<"Please open it through Command Prompt & "<<endl;
cout<<"Insert Elements Through Command Line Argument\n\n\n\n\n"<<endl;
}
cout<<"\n\n\n\n";
system("pause");
}
Sunday, January 2, 2011
introduction to programming CS201___3
This is the idea solution.
Simple copy the code into dev C++ and run it.
#include<iostream.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class CalSalary {
private:
int ID;
int Grade;
string name;
public:
void calculate();
int get_ID();
int get_Grade();
string get_name();
void set_ID(int);
void set_Grade(int);
void set_name(string);
CalSalary();
};
CalSalary::CalSalary()
{
ID=0;
Grade=0;
name="NULL";
}
void CalSalary::calculate()
{
int grade=get_Grade();
int salary;
if(grade == 17)
{
salary = 15000 + 15000/100*45;
}
if(grade == 18)
{
salary = 20000 + 20000/100*45;
}
if(grade == 19)
{
salary = 25000 + 25000/100*45;
}
cout<<"\n\n\tThe net salary of "<<get_name()<<" is Rs."<<salary;
}
int CalSalary::get_Grade()
{
return Grade;
}
int CalSalary::get_ID()
{
return ID;
}
string CalSalary::get_name()
{
return name;
}
void CalSalary::set_Grade(int grade)
{
Grade=grade;
}
void CalSalary::set_ID(int id)
{
ID=id;
}
void CalSalary::set_name(string nam)
{
name=nam;
}
main()
{
CalSalary calsalary;
int id,grade;
string name;
cout<<"\tPlease enter employee ID : ";
cin>>id;
cout<<"\tPlease enter employee name : ";
cin>>name;
cout<<"\tPlease enter employee grade : ";
cin>>grade;
calsalary.set_Grade(grade);
calsalary.set_ID(id);
calsalary.set_name(name);
calsalary.calculate();
cout<<"\n\n\n\n\n";
system("pause");
}
Simple copy the code into dev C++ and run it.
#include<iostream.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class CalSalary {
private:
int ID;
int Grade;
string name;
public:
void calculate();
int get_ID();
int get_Grade();
string get_name();
void set_ID(int);
void set_Grade(int);
void set_name(string);
CalSalary();
};
CalSalary::CalSalary()
{
ID=0;
Grade=0;
name="NULL";
}
void CalSalary::calculate()
{
int grade=get_Grade();
int salary;
if(grade == 17)
{
salary = 15000 + 15000/100*45;
}
if(grade == 18)
{
salary = 20000 + 20000/100*45;
}
if(grade == 19)
{
salary = 25000 + 25000/100*45;
}
cout<<"\n\n\tThe net salary of "<<get_name()<<" is Rs."<<salary;
}
int CalSalary::get_Grade()
{
return Grade;
}
int CalSalary::get_ID()
{
return ID;
}
string CalSalary::get_name()
{
return name;
}
void CalSalary::set_Grade(int grade)
{
Grade=grade;
}
void CalSalary::set_ID(int id)
{
ID=id;
}
void CalSalary::set_name(string nam)
{
name=nam;
}
main()
{
CalSalary calsalary;
int id,grade;
string name;
cout<<"\tPlease enter employee ID : ";
cin>>id;
cout<<"\tPlease enter employee name : ";
cin>>name;
cout<<"\tPlease enter employee grade : ";
cin>>grade;
calsalary.set_Grade(grade);
calsalary.set_ID(id);
calsalary.set_name(name);
calsalary.calculate();
cout<<"\n\n\n\n\n";
system("pause");
}
Subscribe to:
Posts (Atom)