[ create a new paste ] login | about

Link: http://codepad.org/gRJaWLra    [ raw code | output | fork ]

C++, pasted on Dec 8:
#include<iostream>

using namespace std;

class Gach{

      private :

          char* maso;

          char* mau;

          int soluong;

          int chdai;

          int chngang;

          float gia;

      public :

          Gach();

          Gach(char*,char*,int,int,int,float);

          Gach(const Gach&);

          Gach operator = (const Gach&);

          ~Gach();

          friend istream& operator >> (istream& is, Gach& t);

          friend ostream& operator << (ostream& is, Gach t);

          float GiaBanLe();

          int DienTichNenMax();

          int Laychdai();

          int Laychngang();

          char* LayMau();

          float Laygia();

          int Laysoluong();

};

Gach :: Gach(){

     maso = new char[11];

     mau = new char[11];

     soluong = 0;

     chdai = 0;

     chngang = 0;

     gia = 0;

}

Gach :: Gach(char* ms,char* m,int sl,int cd,int cn,float g){

     maso = strdup(ms);

     mau = strdup(m);

     soluong = sl;

     chdai = cd;

     chngang = cn;

     gia = g;

}

Gach :: Gach(const Gach &t){

     maso = strdup(t.maso);

     mau = strdup(t.mau);

     soluong = t.soluong;

     chdai = t.chdai;

     chngang = t.chngang;

     gia = t.gia;

}

Gach :: ~Gach(){

     delete[] maso,mau;

}

istream& operator >> (istream& is, Gach& t){

    

     cout<<"\nNhap ma so: "; is.getline(t.maso,11);

     cout<<"Nhap mau: "; is.getline(t.mau,11);

     cout<<"Nhap so luong vien trong 1 hop: "; is >> t.soluong;

     cout<<"Nhap chieu dai (cm): "; is >> t.chdai;

     cout<<"Nhap chieu ngang (cm): "; is >> t.chngang;

     cout<<"Nhap gia ban 1 hop: "; is >> t.gia;

     return is;

}

ostream& operator << (ostream& os, Gach t){

     os << "\nMa so: "<<t.maso<<endl;

     os << "Mau: "<<t.mau<<endl;

     os << "So luong vien trong 1 hop: "<<t.soluong<<endl;

     os << "Chieu dai: "<<t.chdai<<" (cm)"<<endl;

     os << "Chieu ngang: "<<t.chngang<<" (cm)"<<endl;

     os << "Gia ban 1 hop: "<<t.gia<<endl;

     return os;

}

float Gach :: GiaBanLe(){

      return gia/(float)soluong + gia/(float)soluong*0.2;

}

int Gach :: DienTichNenMax(){

    return chdai*chngang*soluong;

}

int Gach :: Laychdai(){

    return chdai;

}

int Gach :: Laychngang(){

    return chngang;

}

char* Gach :: LayMau(){

    return mau;

}

float Gach :: Laygia(){

    return gia;

}

int Gach :: Laysoluong(){

    return soluong;

}



int SoLuong(Gach G,int x,int y){

    int slngang =x/G.Laychngang();

    int sldai = y/G.Laychdai();

    if(x%G.Laychngang()!=0) slngang++;

    if(y%G.Laychdai()!=0) sldai++;

    int slvien=slngang*sldai;

    int slhop = slvien/G.Laysoluong();

    if(slvien%G.Laysoluong()!=0) slhop++;

    return slhop;

}

float chiphi(Gach G){

    return G.Laygia()/G.DienTichNenMax();

    

}

int main(){

    cout.precision(5);

    int n,count = 0;

    Gach G;

    cout<<"\tNHAP DANH SACH\n";

    cout<<"Nhap so loai gach co trong danh sach: ";

    cin>>n;

    Gach *ds = new Gach[n];

    for(int i=0;i<n;i++){

       cout<<"\nNhap thong tin cho loai "<<i+1;

       cin >> ds[i];

    }

    cout<<"\n\tDANH SACH CAC LOAI GACH VUA NHAP\n";

    

    for(int i=0;i<n;i++){

       cout << ds[i];

       cout <<"Dien tich nen toi da co the lot (cm2): "<<ds[i].DienTichNenMax()<<endl;

       if(strcmp(ds[i].LayMau(),"xam")==0)

                   count++;

       

       else if(strcmp(ds[i].LayMau(),"Xam")==0)

                   count++;

       

       else if(strcmp(ds[i].LayMau(),"XAM")==0)

                   count++;

       

    }

    cout<<"\n\n\tSo luong gach co mau Xam la: "<<count<<endl;

    cout<<"\n\tLoai gach co chi phi lot thap nhat la: ";

    float p=chiphi(ds[0]);

    int k=0;

    for(int i=0;i<n;i++){

       if(chiphi(ds[i])<p){ 

          p = chiphi(ds[i]);

          k=i;

       }

    }

    cout << ds[k];

    int x,y;

    cout<<"\n\tKICH THUOC DIEN TICH CAN LOT GACH CHO TUNG LOAI\n";

    cout<<"Nhap chieu dai (cm): "; cin>>y;

    cout<<"Nhap chieu ngang (cm): "; cin>>x;    

    cout<<"\n\tSo luong gach can de lot  dien tich "<<x<<" x "<<y<<" (cm2):";

    for(int i=0;i<n;i++){

        cout << ds[i];

        cout<<"So luong: "<<SoLuong(ds[i],x,y)<<" hop";

        cout<<endl;        

    }



}


Output:
1
2
3
	NHAP DANH SACH
Nhap so loai gach co trong danh sach: std::bad_alloc: St9bad_alloc
Aborted.


Create a new paste based on this one


Comments: