c++primer第十章编程练习

发布于:2024-12-07 ⋅ 阅读:(80) ⋅ 点赞:(0)

题一

#include <iostream>
#include <cstring>
using namespace std;
class bank_account
{
private:
    char name[30];
    char account_ID[30];
    double balance;

public:
    bank_account(char n[], char a[], double b);
    void show();
    void deposit(double amount);
    void withdraw(double amount);
};

bank_account::bank_account(char n[], char a[], double b)
{
    strcpy(name, n);
    strcpy(account_ID, a);
    balance = b;
}
void bank_account::show()
{
    cout << "Name: " << name << endl;
    cout << "Account ID: " << account_ID << endl;
    cout << "Balance: " << balance << endl;
}

void bank_account::deposit(double amount)
{
    balance += amount;
}

void bank_account::withdraw(double amount)
{
    if (balance < amount)
    {
        cout << "Insufficient balance" << endl;
    }
    else
    {
        balance -= amount;
    }
}

int main()
{
    bank_account a("Alice", "123456", 1000);
    a.show();
    a.deposit(500);
    a.show();
    a.withdraw(2000);
    a.show();
    return 0;
}

题二

#include <iostream>
#include <cstring>
using namespace std;

class Person
{
private:
    static const int LIMIT = 25;
    string lname;
    char fname[LIMIT];

public:
    Person()
    {
        lname = "";
        fname[0] = '\0';
    };
    Person(const string &ln, const char *fn = "Heyyou");
    void Show() const;
    void FormalShow() const;
};
Person::Person(const string &ln, const char *fn)
{
    lname = ln;
    strcpy(fname, fn);
}
void Person::Show() const
{
    cout << "name: " << fname << " " << lname << endl;
}
void Person::FormalShow() const
{
    cout << "name: " << lname << ", " << fname << endl;
}
int main()
{
    Person one;
    Person two("Smythecraft");
    Person three("Dimwiddy", "Sam");
    one.Show();
    cout << endl;
    one.FormalShow();
    cout << endl;
    two.Show();
    cout << endl;
    two.FormalShow();
    cout << endl;
    three.Show();
    cout << endl;
    three.FormalShow();
}

题三

#ifndef TEST9_1_H_
#define TEST9_1_H_
const int Len = 40;
class golf
{
private:
    char fullname[Len];
    int handicap;

public:
    golf(const char *name = "", int hc = 0);
    int setgolf();
    void sethandicap(int hc);
    void showgolf() const;
};
#endif

#include <iostream>
#include "test9_1.h"
#include <cstring>
using namespace std;

golf::golf(const char *name, int hc)
{
    strcpy(fullname, name);
    handicap = hc;
}

int golf::setgolf()
{
    golf g;
    cout << "Enter the fullname :";
    cin.get(g.fullname, Len);
    cout << "Enter the handicap: ";
    cin >> g.handicap;
    cin.get();
    if (g.fullname[0] == '\0')
        return 0;
    else
        return 1;
    *this = g;
}

void golf::sethandicap(int hc)
{
    handicap = hc;
}

void golf::showgolf() const
{
    if (fullname[0] != '\0')
    {
        cout << "fullname: " << fullname << endl;
        cout << "handicap: " << handicap << endl;
    }
}

#include <iostream>
#include "test9_1.h"
using namespace std;
int main()
{
    golf ann("Ann Birdfree", 24);
    ann.showgolf();

    golf *golfarr = new golf[3];
    for (int i = 0; i < 3 && golfarr[i].setgolf(); i++)
    {
    }
    for (int i = 0; i < 3; i++)
        golfarr[i].showgolf();
    delete[] golfarr;
    return 0;
}

题四

#ifndef TEST9_4_H_
#define TEST9_4_H_
namespace SALES
{
    const int QUARTERS = 4;
    class Sales
    {
    private:
        double sales[QUARTERS];
        double average;
        double max;
        double min;

    public:
        Sales(const double ar[], int n);
        Sales();
        void showSales() const;
    };
}
#endif
#include "test9_4.h"
#include <iostream>
using namespace std;
SALES::Sales::Sales(const double ar[], int n)
{
    double sum;
    max = ar[0];
    min = ar[0];
    // int length = sizeof(ar) / sizeof(double);
    for (int i = 0; i < QUARTERS; i++)
    {
        if (i < n)
        {
            sum += ar[i];
            sales[i] = ar[i];
            if (ar[i] > max)
                max = ar[i];
            if (ar[i] < min)
                min = ar[i];
        }
        else
            sales[i] = 0;
    }
    average = sum / n;
}

SALES::Sales::Sales()
{
    max = 0;
    min = 999999;
    for (int i = 0; i < QUARTERS; i++)
    {
        cout << "Enter " << i + 1 << "-th quarter sales:";
        cin >> sales[i];
        average += sales[i];
        if (sales[i] > max)
            max = sales[i];
        if (sales[i] < min)
            min = sales[i];
    }
    average /= QUARTERS;
}
void SALES::Sales::showSales() const
{
    for (int i = 0; i < QUARTERS; i++)
        cout << i + 1 << "-th quarter sales:" << sales[i] << endl;

    cout << "average :" << average << endl;
    cout << "max :" << max << endl;
    cout << "min :" << min << endl;
}
#include <iostream>
#include "test9_4.h"
using namespace std;
using namespace SALES;
int main()
{
    double earning[3] = {10, 20, 30};
    Sales FirstYear(earning, 3);
    FirstYear.showSales();

    Sales SecondYear;
    SecondYear.showSales();
    return 0;
}

 

题五

#include <iostream>
using namespace std;
#include <cstring>

struct customer
{
    char fullname[35];
    double payment;
};

typedef customer Item;

class Stack
{
private:
    int top;
    enum
    {
        MAX = 3A
    };
    Item items[MAX];
    double sum = 0;

public:
    Stack();
    bool isEmpty() const;
    bool isFull() const;
    bool push(const Item &item);
    void pop(Item &item);
};

Stack::Stack()
{
    top = 0;
}

bool Stack::isEmpty() const
{
    return top == 0;
}

bool Stack::isFull() const
{
    return top == MAX;
}

bool Stack::push(const Item &item)
{
    if (top < MAX)
    {
        items[top] = item;
        top++;
        return true;
    }
    else
        return false;
}

void Stack::pop(Item &item)
{
    if (top > 0)
    {
        item = items[top - 1];
        top--;
        sum += item.payment;
        cout << "Sum payment: " << sum << endl;
    }
    else
        cout << "Stack is empty!" << endl;
}

int main()
{
    Stack s;
    customer c;
    char ch;
    cout << "Please enter A to add a customer order,\n"
         << "or P to pop a customer order,\n"
         << "or Q to quit: ";
    while (cin >> ch && ch != 'Q')
    //while (cin >> ch && toupper(ch) != 'Q')
        while (cin.get() != '\n')
            continue;
        if (!isalpha(ch))
        {
            cout << "\a";
            continue;
        }
        switch (ch)
        {
        case 'A':
        case 'a':
            cout << "Please enter customer name: ";
            cin.getline(c.fullname, 35);
            cout << "Please enter payment amount: ";
            cin >> c.payment;
            if (s.isFull())
                cout << "Stack is full!" << endl;
            else
                s.push(c);
            break;
        case 'P':
        case 'p':
            if (s.isEmpty())
                cout << "Stack is empty!" << endl;
            else
            {
                s.pop(c);
                cout << "Customer name: " << c.fullname << endl;
                cout << "Payment amount: " << c.payment << endl;
            }
            break;
        }
        cout << "Please enter A to add a customer order,\n"
             << "or P to pop a customer order,\n"
             << "or Q to quit: ";
    }
    cout << "Bye!" << endl;
    return 0;
}

题六

#include <iostream>
using namespace std;
class Move
{
private:
    double x, y;

public:
    Move(double a = 0, double b = 0);
    void showmove() const;
    Move add(const Move &m) const;
    Move reset(double x = 0, double y = 0);
};

Move::Move(double a, double b)
{
    x = a;
    y = b;
}
void Move::showmove() const
{
    cout << "Position: (" << x << "," << y << ")" << endl;
}
Move Move::add(const Move &m) const
{
    Move temp;
    temp.x = x + m.x;
    temp.y = y + m.y;
    return temp;
}
Move Move::reset(double x, double y)
{
    this->x = x;
    this->y = y;
    return *this;
}
int main()
{
    Move m1(1, 2);
    Move m2(3, 4);
    Move m3 = m1.add(m2);
    m3.showmove();
    m3.reset(5, 6);
    m3.showmove();
    return 0;
}

 

题七

#include <iostream>
#include <cstring>
using namespace std;
class plorg
{
private:
    int CI;
    char Name[20];

public:
    plorg(int ci = 50, char name[] = "Plorg");
    void Amend(int ci);
    void Show() const;
};
plorg::plorg(int ci, char name[])
{
    CI = ci;
    strcpy(Name, name);
}
void plorg::Amend(int ci)
{
    CI = ci;
}
void plorg::Show() const
{
    cout << "CI: " << CI << endl;
    cout << "Name: " << Name << endl;
}
int main()
{
    plorg p1(100, "Plorg1");
    p1.Show();
    p1.Amend(200);
    p1.Show();
    plorg p2;
    p2.Show();
    return 0;
}

 

题八

#include <iostream>
#include <functional> // 用于 std::function

// 定义一个模板类,用于实现简单的列表
template <typename T, size_t MaxSize>
class SimpleList
{
private:
    T data[MaxSize]; // 固定大小的数组存储列表元素
    size_t size;     // 当前列表中的元素数量

public:
    // 构造函数:创建空列表
    SimpleList() : size(0) {}

    // 判断列表是否为空
    bool isEmpty() const
    {
        return size == 0;
    }

    // 判断列表是否为满
    bool isFull() const
    {
        return size == MaxSize;
    }

    // 向列表中添加数据项
    bool add(const T &item)
    {
        if (isFull())
        {
            std::cerr << "列表已满,无法添加数据项。" << std::endl;
            return false;
        }
        data[size++] = item;
        return true;
    }

    // 访问列表中的每个数据项并对其执行某种操作
    void forEach(const std::function<void(T &)> &operation)
    {
        for (size_t i = 0; i < size; ++i)
        {
            operation(data[i]);
        }
    }

    // 显示列表中的所有数据项
    void display() const
    {
        for (size_t i = 0; i < size; ++i)
        {
            std::cout << data[i] << " ";
        }
        std::cout << std::endl;
    }

    // 返回列表中的数据项数量
    size_t getSize() const
    {
        return size;
    }
};

int main()
{
    // 创建一个最多存储5个整数的列表
    SimpleList<int, 5> myList;

    // 判断列表是否为空
    if (myList.isEmpty())
    {
        std::cout << "列表为空。" << std::endl;
    }

    // 向列表中添加数据项
    myList.add(10);
    myList.add(20);
    myList.add(30);

    // 显示列表内容
    std::cout << "列表内容:";
    myList.display();

    // 判断列表是否为满
    if (myList.isFull())
    {
        std::cout << "列表已满。" << std::endl;
    }
    else
    {
        std::cout << "列表未满。" << std::endl;
    }

    // 对每个数据项执行某种操作(例如加10)
    myList.forEach([](int &x)
                   { x += 10; });

    // 显示修改后的列表内容
    std::cout << "修改后的列表内容:";
    myList.display();

    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到