學(xué)堂在線C++練習(xí)

類的練習(xí)

#pragma warning(disable:4996)
#include <string.h>
#include <cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
enum CPU_Rank { P1 = 1,P2,P3,P4,P5,P6,P7 };
class CPU {
private:
    CPU_Rank rank;
    int frequency;
    float voltage;
public:
    CPU(CPU_Rank r,int f, float v){//構(gòu)造函數(shù)
        rank = r;
        frequency = f;
        voltage = v;
        cout << "構(gòu)造了一個(gè)CPU!" << endl;
    }
    ~CPU() {//析構(gòu)函數(shù)
        cout << "析構(gòu)了一個(gè)CPU!" << endl;
    }
    //接口
    CPU_Rank GetRank() const { return rank; }
    int GetFrequency() const { return frequency; }
    float GetVoltage() const { return voltage; }

    void SetRank(CPU_Rank r) {
        rank = r;
    }
    void  SetFrequency(int f) {
        frequency = f;
    }
    void SetVoltage(float v) {
        voltage = v;
    }
    void Run() { cout << "CPU開始運(yùn)行" << endl; }
    void Stop() { cout << "CPU停止運(yùn)行" << endl; }
};
enum RAM_Type{DDR2=2,DDR3,DDR4};
class RAM {
private:
    enum RAM_Type type;
    unsigned int frequency;
    unsigned int size;
public:
    RAM(RAM_Type t, unsigned int f, unsigned int s) {
        type = t;
        frequency = f;
        size = s;
        cout << "構(gòu)造了一個(gè)RAM" << endl;
    }
    ~RAM() {
        cout << "析構(gòu)了一個(gè)RAM" << endl;
    }
    RAM_Type GetType() const { return type; }
    unsigned int GetFrequency() const { return frequency; }
    unsigned int GetSize() const { return size; }

    void SetType(RAM_Type t) {
        type = t;
    }
    void  SetFrequency( unsigned int f) {
        frequency = f;
    }
    void SetSize( unsigned int s) {
        size = s;

    }
    void Run() { cout << "RAM開始運(yùn)行" << endl; }
    void Stop() { cout << "RAM停止運(yùn)行" << endl; }

};
enum CDROM_Interface{SATA,USB};
enum CDROM_Install_type{external,build_in};
class CD_ROM {
private:
    enum CDROM_Interface interface_type;
    unsigned int cache_size;
    CDROM_Install_type install_type;
public:
    CD_ROM(CDROM_Interface i, unsigned int s, CDROM_Install_type it) {
        interface_type = i;
        cache_size = s;
        install_type = it;
        cout << "構(gòu)造了一個(gè)CD_ROM!" << endl;
    }
    ~CD_ROM() {
        cout << "析構(gòu)了一個(gè)CD_ROM" << endl;
    }
    CDROM_Interface GetInterfaceType() const  {
        return interface_type;
    }
    unsigned int GetSize() const {
        return cache_size;
    }
    CDROM_Install_type GetInstallType() const {
        return install_type;
    }

    void SetInterfaceType(CDROM_Interface i) {
        interface_type = i;
    }
    void SetSize( unsigned int s) { 
        cache_size = s;
    }
    void SetInstallType(CDROM_Install_type it) {
        install_type = it;
    }
    void Run() {
        cout << "CD_ROM開始運(yùn)行!" << endl;
    }
    void Stop() {
        cout << "CD_ROM停止運(yùn)行" << endl;
    }

};
class COMPUTER {
private:
    CPU my_cpu;
    RAM my_ram;
    CD_ROM my_cdrom;
    unsigned int storage_size;
    unsigned int bandwidth;
public:
    COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int s, unsigned int b);

    ~COMPUTER() {
        cout << "析構(gòu)了一個(gè)COMPUTER!" <<endl;
    }
    void Run() {
        my_cpu.Run();
        my_ram.Run();
        my_cdrom.Run();
        cout << "COMPUTER開始運(yùn)行!" << endl;
    }
    void Stop() {
        my_cpu.Stop();
        my_ram.Stop();
        my_cdrom.Stop();
        cout << "COMPUTER結(jié)束運(yùn)行!" << endl;
    }
};

COMPUTER::COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int s, unsigned int b) :
    my_cpu(c), my_ram(r), my_cdrom(cd) {
    storage_size = s;
    bandwidth = b;
    cout << "構(gòu)造了一個(gè)COMPUTER!" << endl;
}
int main()

{
    CPU a(P6, 300, 2.8);
    a.Run();
    a.Stop();
    cout << "*********\n";

    RAM b(DDR3,1600,8);
    b.Run();
    b.Stop();
    cout << "*********\n";

    CD_ROM c(SATA,2, build_in);
    c.Run();
    c.Stop();
    cout << "*********\n";

    COMPUTER my_computer(a, b, c, 128, 10);
    cout << "**********\n";

    my_computer.Run();
    my_computer.Stop();
    cout << "***********\n";
    return 0;

}

用類求最大公約數(shù)

using namespace std;
class Integer {
private:
    int _num;
public:
    //構(gòu)造函數(shù)
    Integer(int num) {
        _num = num;
    }
    //計(jì)算當(dāng)前Integer 和 b之間的最大公約數(shù)
    int gcdm(int a, int b) {
        if (b == 0) {
            return a;
        }
        else {
            return gcdm(b, a % b);
        }
    }
    int gcd(Integer b) {
        int x = _num;
        int y = b._num;
        return gcdm(x, y);
    }
};
int main() {
    int a, b;
    cin >> a >> b;
    Integer A(a);
    Integer B(b);
    cout << A.gcd(B) << endl;
    return 0;
}

數(shù)字反轉(zhuǎn)

輸入12345
輸出54321

/* students please write your program here */

#include <iostream>
#include<math.h>
#include<vector>
using namespace std;

class Integer {
private:
    int _num;
    //getLength()函數(shù)獲取_num長(zhǎng)度
    int getLength() {
        double   x = 10;
        int   y = 0;
        while (_num % 10 != 0) {
            _num /= 10;
            ++y;
        }
        return y;
    }
public:
    //Integer類構(gòu)造函數(shù)
    Integer(int num) {
        _num = num;
    }
    //反轉(zhuǎn)_num
    int inversed() {
        vector<int> nums;
        int num1 = _num;
        while (num1 > 0) {
            nums.push_back(num1 % 10);
            num1 /= 10;
        }
        int result=0;
        int m = 0;
        vector<int>::const_iterator iterator = nums.end()-1;
        //vector迭代器相關(guān)操作可以從前往后,不能從后往前,不然會(huì)越界
             

        /*
        int n = getLength()-1;
        for (int j=n;j>=0;j--)
        {
            
            result = result + nums[j] * (pow(10, m++));
        }
        */
        while (!nums.empty()) {
            result = result + nums.back() * (pow(10, m++));
            nums.pop_back();//使用棧的相關(guān)操作可以免去計(jì)數(shù)的麻煩
        }
        return result;
    }
};

int main() {
    int n;
    cin >> n;
    Integer integer(n);
    cout << integer.inversed() << endl;
    return 0;
}

??額寶

#include <iostream>
using namespace std;

class Yuebao
{
    static double profitRate;
public:
    static void setProfitRate(double rate);
    Yuebao(double x) {
        _x = x;
    }
    void deposit(double m);
    void addProfit();
    void withdraw(double n);
    double getBalance()  const {
        return  _x;
    }
    /* Your code here! */
private:
    double _x = 0;//余額

};

void Yuebao::deposit(double m) {
    _x +=m;
}
void Yuebao::withdraw(double n) {
    _x  -= n;
}
void Yuebao::setProfitRate(double rate) {
    profitRate = rate;
}
void Yuebao::addProfit() {
    _x = _x * (1 + profitRate);
}
double Yuebao::profitRate = 0;

int main()
{
    int n;
    while (cin >> n)
    {
        double profitRate;
        cin >> profitRate;
        Yuebao::setProfitRate(profitRate);//設(shè)定魚額寶的利率
        Yuebao y(0); //新建魚額寶賬戶,余額初始化為0
        int operation;//接受輸入判斷是存還是取
        double amount;//接受輸入存取金額
        for (int i = 0; i < n; ++i)
        {
            y.addProfit();//加入前一天余額產(chǎn)生的利息
            cin >> operation >> amount;
            if (operation == 0)
                y.deposit(amount);//存入金額
            else
                y.withdraw(amount);//取出金額
        }
        cout << y.getBalance() << endl;//輸出最終賬戶余額
    }
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容