10.10 编程练习

  1. 第一题

main.cpp

#include <iostream>
#include "golf.h"


int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    BankAccount bank {"ycy", "20210226", 1000};
    bank.show();
    bank.deposit(500);
    bank.show();
    bank.withdraw(200);
    bank.show();

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include "golf.h"

BankAccount::BankAccount(const std::string &clinet, const std::string &num, double bal)
{
    name = clinet;
    acctnum = num;
    balance = bal;
}

void BankAccount::show() const
{
    std::cout << "name:" << name << " acctnum:" << acctnum << " balance:" << balance << std::endl;
}

void BankAccount::deposit(double cash)
{
    balance += cash;
}

void BankAccount::withdraw(double cash)
{
    balance -= cash;
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H

#include <cstring>
class BankAccount
{
private:
    std::string name;
    std::string acctnum;
    double balance;
public:
    BankAccount(const std::string & clinet, const std::string & num, double bal = 0.0);
    void show() const;
    void deposit(double cash);
    void withdraw(double cash);
};


#endif //FIRST_GOLF_H

运行结果

name:ycy acctnum:20210226 balance:1000
name:ycy acctnum:20210226 balance:1500
name:ycy acctnum:20210226 balance:1300
  1. 第二题
    同第一题差不了多少
    main.cpp
#include <iostream>
#include "golf.h"


int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    Person one;
    Person two("Smythecraft");
    Person three("Dimwiddy", "Sam");
    one.show();
    one.FormalShow();
    two.show();
    two.FormalShow();
    three.show();
    three.FormalShow();

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include <cstring>
#include "golf.h"

Person::Person()
{
    lname = "";
    fname[0] = '\0';
}

Person::Person(const std::string & ln, const char * fn)
{
    lname = ln;
    strcpy(fname, fn);
}
void Person::show() const
{
    std::cout << "first name last name:" << fname << " " << lname << std::endl;
}
void Person::FormalShow() const
{
    std::cout << "last name, first name:" << lname << ", " << fname << std::endl;
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H

#include <string>
class Person
{
private:
    static const int LIMIT = 25;
    std::string lname;
    char fname[LIMIT]{};
public:
    Person();
    explicit Person(const std::string & ln, const char * fn = "Heyyou");
    void show() const;
    void FormalShow() const;
};


#endif //FIRST_GOLF_H

运行结果:

first name last name:
last name, first name:,
first name last name:Heyyou Smythecraft
last name, first name:Smythecraft, Heyyou
first name last name:Sam Dimwiddy
last name, first name:Dimwiddy, Sam
  1. 第三题
    main.cpp
#include <iostream>
#include <cstring>
#include "golf.h"

const int Max = 3;
const int Len = 40;
int main() {

    char name[Len];
    int hand;
    std::cout << "非交互式版本:请输入用户的全名和handicap:\n";
    go people[Max] = {
            go(people[0], "zxc vb", 1),
            go(people[1], "asd fg", 2),
            go(people[2], "qwe rt", 3)
    };
    for (auto & i : people)
        i.showgolf();
    std::cout << "交互式版本:请输入用户的全名和handicap:\n";
    go peo[Max] = {go(),go(),go()};

    std::cout << "将第一个用户的handicap设置为5:\n";
    peo[0].hand(5);

    for (auto & i : peo)
        i.showgolf();

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include <cstring>
#include "golf.h"

go::go()
{
    std::cin.getline(fullname, Len);
    std::cin >> handicap;
    std::cin.get();
}

go::go(go & g, const char * name, int hc)
{
    strcpy(g.fullname, name);
    g.handicap = hc;
}

void go::hand(int hc)
{
    handicap = hc;
}

void go::showgolf()
{
    std::cout << fullname << " " << handicap << std::endl;
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H

class go
{
private:
    static const int Len = 40;
    char fullname[Len];
    int handicap;
public:
    go();
    go(go & g, const char * name, int hc);
    void hand(int hc);
    void showgolf();
};

#endif //FIRST_GOLF_H


运行结果:

非交互式版本:请输入用户的全名和handicap:
zxc vb 1
asd fg 2
qwe rt 3
交互式版本:请输入用户的全名和handicap:
qwe
4
rty
5
yui
6
将第一个用户的handicap设置为5:
qwe 5
rty 5
yui 6
  1. 第四题
    main.cpp
#include <iostream>
#include "golf.h"


int main() {

    using std::cin;
    using std::cout;
    using std::endl;
    using namespace SALES;

    double arr[4] = {1.0, 2.0, 3.0, 4.0};

    // 非交互式代码
    cout << "非交互式代码" << endl;
    sales s1 = sales(s1, arr, 4);
    s1.showSales();
    // 交互式代码
    cout << "交互式代码" << endl;
    sales s2;
    s2.showSales();

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include "golf.h"
namespace SALES
{
    using std::cin;
    using std::cout;
    using std::endl;
    sales::sales(sales &s, const double *ar, int n)
    {
        for (int i=0;i<QUARTERS;++i)
        {
            s.sa[i] = ar[i];
        }
        double sum = 0, max, min;
        max = ar[0];
        min = ar[0];
        sum += ar[0];
        for (int i=1;i<QUARTERS;++i)
        {
            if (max < ar[i])
                max = ar[i];
            if (min > ar[i])
                min = ar[i];
            sum += ar[i];
        }
        s.average = sum / QUARTERS;
        s.max = max;
        s.min = min;
    }

    sales::sales()
    {
        for (double & sale : sa)
        {
            cin >> sale;
        }
        double sum = 0, mx, mn;
        mx = sa[0];
        mn = sa[0];
        sum += sa[0];
        for (int i=1;i<QUARTERS;++i)
        {
            if (mx < sa[i])
                mx = sa[i];
            if (mn > sa[i])
                mn = sa[i];
            sum += sa[i];
        }
        average = sum / QUARTERS;
        max = mx;
        min = mn;
    }

    void sales::showSales()
    {
        cout << "All number:";
        for (double sale : sa)
        {
            cout << sale << " ";
        }
        cout << "\naverage:";
        cout << average;
        cout << "\nmax:";
        cout << max;
        cout << "\nmin:";
        cout << min << endl;
    }
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H

namespace SALES
{
    class sales
    {
    private:
        static const int QUARTERS = 4;
        double sa[QUARTERS];
        double average;
        double max;
        double min;
    public:
        sales(sales & s, const double ar[], int n);

        sales();

        void showSales();
    };

}

#endif //FIRST_GOLF_H


运行结果:

非交互式代码
All number:1 2 3 4
average:2.5
max:4
min:1
交互式代码
4 5 6 7
All number:4 5 6 7
average:5.5
max:7
min:4

  1. 第五题
    main.cpp
#include <iostream>
#include "golf.h"

const int MAX = 10;

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    double sum = 0;
    Stack st;
    Item it[MAX];
    cout << "请输入结构数组:\n";
    for (auto & i : it)
    {
        cin.getline(i.fullname, 35);
        cin >> i.payment;
        cin.get();
        if (!st.isfull())
            st.push(i);
    }
    cout << "栈中元素有" << st.get() << "个\n";
    cout << "删除栈顶的两个元素:\n";
    if (!st.isempty())
    {
        sum += it[9].payment;
        st.pop(it[9]);
    }
    cout << "sum总和为:" << sum << endl;

    if (!st.isempty())
    {
        sum += it[8].payment;
        st.pop(it[8]);
    }
    cout << "sum总和为:" << sum << endl;
    cout << "栈中元素有" << st.get() << "个\n";

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include "golf.h"

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;
        return true;
    }
    else
        return false;
}
bool Stack::pop(Item & item)
{
    if (top > 0)
    {
        item = items[--top];
        return true;
    }
    else
        return false;
}
int Stack::get() const
{
    return this->top;
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H

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

typedef customer Item;

class Stack
{
private:

    enum {MAX = 10};
    Item items[MAX];
    int top;
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item & item);
    bool pop(Item & item);
    int get() const;
};

#endif //FIRST_GOLF_H

运行结果:

请输入结构数组:
qwe
1
asd
2
zxc
3
ert
4
dfg
5
cvb
6
tyu
7
ghj
8
bnm
9
jkl
10
栈中元素有10个
删除栈顶的两个元素:
sum总和为:10
sum总和为:19
栈中元素有8
  1. 第六题
    main.cpp
#include <iostream>
#include "golf.h"

const int MAX = 10;

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    Move mo {1.0, 2.0};
    mo.showmove();
    Move mo1 {3.0, 4.0};
    mo1.showmove();
    Move mo2;
    mo2 = mo.add(mo1);
    mo2.showmove();
    mo2.reset(7.0, 7.0);
    mo2.showmove();

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include "golf.h"

Move::Move(double a, double b)
{
    x = a;
    y = b;
}
void Move::showmove() const
{
    std::cout << "x:" << x << " y:" << y << std::endl;
}
Move Move::add(const Move & m) const
{
    std::cout << "new a move object:\n";
    Move mo;
    mo.x = (*this).x + m.x;
    mo.y = (*this).y + m.y;
    return mo;
}
void Move::reset(double a, double b)
{
    std::cout << "reset x and y:\n";
    x = a;
    y = b;
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H


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

#endif //FIRST_GOLF_H

运行结果:

x:1 y:2
x:3 y:4
new a move object:
x:4 y:6
reset x and y:
x:7 y:7

  1. 第七题
    main.cpp
#include <iostream>
#include "golf.h"

const int MAX = 10;

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    plorg pl;
    plorg pl1 {"plorg1"};
    plorg pl2 {"plorg2", 60};
    pl.show();
    pl1.show();
    pl2.show();
    pl.reset("ycy");
    pl.show();

    return 0;
}

golf.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include <cstring>
#include "golf.h"

plorg::plorg()
{
    strcpy(name, "Plorga");
    CI = 50;
}
plorg::plorg(char * n, int c)
{
    strcpy(name, n);
    CI = c;
}
void plorg::reset(char * n)
{
    strcpy(name, n);
}
void plorg::show() const
{
    std::cout << "name:" << name << " CI:" << CI << std::endl;
}

golf.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H


class plorg
{
private:
    char name[19];
    int CI;
public:
    plorg();
    plorg(char * n, int c = 50);
    void reset(char * n);
    void show() const;
};

#endif //FIRST_GOLF_H

运行结果:

name:Plorga CI:50
name:plorg1 CI:50
name:plorg2 CI:60
name:ycy CI:50

  8. 第八题
main.cpp

#include <iostream>
#include "golf.h"

const int MAX = 10;
void li(Item &);

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    List list;
    int temp;
    for (int i = 0; i < MAX; ++i)
    {
        cin >> temp;
        if (!list.isfull())
            list.insert(temp);
        else
            cout << "list is full!\n";
    }
    if (!list.isempty())
        cout << "list is empty!\n";
    else
        cout << "list is not empty!\n";

    list.visit(li);
    list.operate();
    list.visit(li);

    return 0;
}

void li(Item & x)
{
    std::cout << x << std::endl;
}

list.cpp

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include "golf.h"

List::List()
{
    top = 0;
}
bool List::isempty() const
{
    return top == 0;
}
bool List::isfull() const
{
    return top == MAX;
}
bool List::insert(const Item & item)
{
    if (top < MAX)
    {
        items[top++] = item;
        return true;
    }
    else
        return false;
}
void List::visit(void (*pf)(Item &))
{
    for (auto & i : items)
        pf(i);
}
void List::operate()
{
    int temp;
    for (int i = 0, j = top-1; i < j; ++i,--j)
    {
        temp = items[i];
        items[i] = items[j];
        items[j] = temp;
    }
}

list.h

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_H

typedef int Item;

class List
{
private:
    enum {MAX = 10};
    Item items[MAX];
    int top;
public:
    List();
    bool isempty() const;
    bool isfull() const;
    bool insert(const Item & item);
    void visit(void (*pf)(Item &));
    void operate();
};

#endif //FIRST_GOLF_H

运行结果:(我做的操作是将数组元素颠倒)

1
2
3
4
5
6
7
8
9
10
list is empty!
1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1


var code = "6bf4a2e1-7c91-44cf-862c-a34d883cfc5f"

posted @ 2021-02-27 14:42  小Aer  阅读(127)  评论(0编辑  收藏  举报