Truc

Laptop.cpp
mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.0 kB
3
Indexable
Never
#include<iostream>
#include<vector>
using namespace std;
class Laptop
{
public:
    int ma_may; //khong trung nhau
    string hang_san_xuat;
    string ten_may;
    string cau_hinh;
    double don_gia;
    int so_luong;
    friend ostream& operator<<(ostream& os, const Laptop& laptop);
    friend ostream& operator>>(ostream& os, const Laptop& laptop);
};
class LaptopManager
{
public:
    vector<Laptop> laptops;
    int them_mot_laptop(Laptop laptop);  // return the newly added laptop's Id
    double tinh_tong_gia_tri_laptop_theo_hang_san_xuat(string hangsx);
    void in_danh_sach_lap_top();
    // void sap_xep_danh_sach_theo_ma_may();
    int tim_kiem_theo_ma_may(int ma_may);
};
int LaptopManager::them_mot_laptop(Laptop laptop)
{
    laptops.push_back(laptop);
    return laptop.ma_may;
}
double LaptopManager::tinh_tong_gia_tri_laptop_theo_hang_san_xuat(string hangsx)
{
    double sum = 0.0;
    for(auto laptop : laptops)
    {
        if(laptop.hang_san_xuat == hangsx)
        {
            sum+= laptop.don_gia*laptop.so_luong;
        }
    }
    return sum;
}
ostream& operator<<(ostream& os, const Laptop& lt)
{
    os << lt.ma_may << '\t' <<lt.ten_may<<'\t'<< lt.cau_hinh <<'\t'<< lt.don_gia <<'\t'<<lt.hang_san_xuat<<'\t'<<lt.don_gia<<'\t'<<lt.so_luong;
    return os;
}
ostream& operator>>(ostream& os, Laptop& lt)
{
    cin >> lt.ma_may >> lt.ten_may>>lt.cau_hinh >> lt.don_gia >>lt.hang_san_xuat>>lt.don_gia>>lt.so_luong;
    return os;
}
void LaptopManager::in_danh_sach_lap_top()
{
    for(auto laptop : laptops)
    {
        cout<<laptop;
        cout<<endl;
    }
}
int LaptopManager::tim_kiem_theo_ma_may(int ma_may)
{
    int low = 0;
    int high = laptops.size() - 1;
    while(low<=high)
    {
        int mid = low + (high-low)/2;

        if(laptops[mid].ma_may==ma_may)
        {
            return laptops[mid].ma_may;
        }
        else if(laptops[mid].ma_may<ma_may)
        {
            low = mid +1 ;
        }
        else
        {
            high = mid -1;
        }
    }
    return -1;
}

int main()
{
    cout<<"Hello world"<<endl;
    LaptopManager LaptopManager;

    Laptop Laptop1;
    Laptop1.ma_may = 123;
    Laptop1.cau_hinh = "15G-RAM";
    Laptop1.don_gia = 123.23;
    Laptop1.so_luong = 2;
    Laptop1.ten_may = "asus";

    Laptop Laptop2;
    Laptop2.ma_may = 127;
    Laptop2.cau_hinh = "12G-RAM";
    Laptop2.don_gia = 13.23;
    Laptop2.so_luong = 4;
    Laptop2.ten_may = "dell";

    Laptop Laptop3;
    Laptop3.ma_may = 130;
    Laptop3.cau_hinh = "12G-RAM";
    Laptop3.don_gia = 16.23;
    Laptop3.so_luong = 4;
    Laptop3.ten_may = "hp";

    LaptopManager.them_mot_laptop(Laptop1);
    LaptopManager.them_mot_laptop(Laptop2);
    LaptopManager.them_mot_laptop(Laptop3);

    LaptopManager.in_danh_sach_lap_top();
    cout<<LaptopManager.tim_kiem_theo_ma_may(130);






}