Monday, 17 October 2011

to find the least petrol consuming department


 //to find the least petrol consuming department
#include<iostream.h>
#include<string.h>
class check
            //define class check
{
        int dep;
        float con,km,r;
        char na[20];
        public:
        check ()        //default constructor
     
        {
                 dep=0;
                na[0]=0;
                con=0;
                km=0;
        }
        void get(int d,char n[20],int c,int k)
        {
            //get the details of each department and store it in the resp data members
                dep=d;
                strcpy(na,n);
                con=c;
                km=k;
                calc();//calls function calc
        }
        void calc()
        {
                r=km/con;     //calculates the km per lit
        }
        void operator > (check &k)//overloads >operator
        {
                check t;//creates class object
                if(r<k.r)//checks if r of an object is greater than its next
                {//     if so swaps the values and the details of the department
     
                        t.r=r;
                        r=k.r;
                        k.r=t.r;
            //swaping  the names of dept
                        strcpy(t.na,na);
                        strcpy(na,k.na);
                        strcpy(k.na,t.na);
                 }
        }
        void put()//used to display the dpt which consumes least amt of petrol
        {
                cout<<"\nthe dept with least petrol consumption is"<<na<<"\nits km per lit is"<<r;
        }
}p[20],k[20];//creates class objects
int main()
{
        system("clear");
        int d1,n,i;
        float c1,k1,r1;
        char n1[20];
        cout<<"\n enter the no of departments";  //get the no of departments
        cin>>n;
        for(i=1;i<=n;i++)
        {
                cout<<"\n enter the depno,name,km travelled and amt of petrol consumed";
      //get the department no ,name,km traveled and amt of petrol consumed
                cin>>d1>>n1>>k1>>c1;
                 p[i].get(d1,n1,c1,k1); //call member function get
        }
        for(i=1;i<=n;i++)
        {
                p[i]>p[i+1];  //call operator <function
        }
        p[1].put();//displays  the first department details
}
























output:
enter the no of departments3
 enter the depno,name,km travelled and amt of petrol consumed1
cse
115
6
 enter the depno,name,km travelled and amt of petrol consumed2
ece
120
6
 enter the depno,name,km travelled and amt of petrol consumed3
ee
100
6
the dept with least petrol consumption is ece
its km per lit is20

No comments:

Post a Comment