2 no goddess No_goddess 于 2016.09.10 19:56 提问

c++面性对象程序设计,谢谢

声明一个银行账户类Account,该类有账号(id),余额(balance)两个数据成员,有获取账号,获取余额,存款和取款的函数,以及必要的构造函数。请按上述要求声明该银行账户类并在main函数中定义该类的多个对象,然后对它们进行存取款和查询余额的操作。

4个回答

FrankLi101000100
FrankLi101000100   2016.09.10 21:29

class Account {
string id;
int balance;
public:
Account(string ID) {
id = ID;
}
int deposit(int amount) { balance += amount; return amount; }
int withdraw(int amount) { balance -= amount; return amount; }
string ID() { return id; }
int balance() { return balance; }
};

FrankLi101000100
FrankLi101000100   2016.09.10 21:31

可以写一颗以id为键值的查找树

FrankLi101000100
FrankLi101000100   2016.09.10 21:43

#include
using namespace std;
template
class Size_Balanced_Tree {
struct node {
int Size;//important!!!
//cnt the number of child nodes
KeyType key;
ValType val;

    node *lchild, *rchild;
    node(KeyType k, ValType v) {
        lchild = 0, rchild = 0; Size = 0;
        val = v; key = k;
    }
};
node *root;
inline int SizeOf(node *&target) {
    return target ? target->Size : 0;
}
void Lrotate(node *&target) {
    node *temp = target->lchild;
    target->lchild = temp->rchild;
    temp->rchild = target;
    temp->Size = target->Size;
    target->Size = SizeOf(target->lchild) + SizeOf(target->rchild) + 1;
    target = temp;
}
void Rrotate(node *&target) {
    node *temp = target->rchild;
    target->rchild = temp->lchild;
    temp->lchild = target;
    temp->Size = target->Size;
    target->Size = SizeOf(target->lchild) + SizeOf(target->rchild) + 1;
    target = temp;
}
void Maintain(node *& target) {
    if (target->lchild != NULL) {
        if (SizeOf(target->rchild) < SizeOf(target->lchild->lchild))
            Lrotate(target);
        else if (SizeOf(target->rchild) < SizeOf(target->lchild->rchild)) {
            Rrotate(target->lchild);
            Lrotate(target);
        }Maintain(target->lchild);
    }
    if (target->rchild != NULL) {
        if (SizeOf(target->lchild) < SizeOf(target->rchild->rchild))
            Rrotate(target);
        else if (SizeOf(target->lchild) < SizeOf(target->rchild->lchild)) {
            Lrotate(target->rchild);
            Rrotate(target);
        }Maintain(target->rchild);
    }
    return;
}
void insert(node *&target, KeyType &key, ValType &val) {
    if (!target) {
        target = new node(key, val);
        return;
    }
    target->Size++;
    if (key > target->key)insert(target->rchild, key, val);
    else if (key != target->key)insert(target->lchild, key, val);
    else return;
    Maintain(target);
}
ValType& query(node *&target, KeyType key) {
    if (target == NULL) {
        int r = 0;
        return r;
    }
    if (target->key == key)return target->val;
    else if (target->key < key)return query(target->rchild, key);
    else return query(target->lchild, key);
}

public:
Size_Balanced_Tree() {
root = 0;
}
void Create_Account(string id) {
int init = 0;
insert(root, id, init);
}
ValType deposit(KeyType id, int amount) {
return query(root, id)+=amount;
}
ValType withdraw(KeyType id, int amount) {
return query(root, id) -= amount;
}
ValType amount(KeyType id) {
return query(root, id);
}
};
Size_Balanced_Treeacmngr;
int main() {
acmngr.Create_Account("尼玛");
acmngr.Create_Account("泥煤");
acmngr.deposit("尼玛", 3);
cout << acmngr.amount("尼玛") << endl << acmngr.amount("泥煤");
}

FrankLi101000100
FrankLi101000100   2016.09.10 21:50
#include<iostream>
using namespace std;
template<typename KeyType, typename ValType>
class Size_Balanced_Tree {
    struct node {
        int Size;//important!!!
                 //cnt the number of child nodes
        KeyType key;
        ValType val;

        node *lchild, *rchild;
        node(KeyType k, ValType v) {
            lchild = 0, rchild = 0; Size = 0;
            val = v; key = k;
        }
    };
    node *root;
    inline int SizeOf(node *&target) {
        return target ? target->Size : 0;
    }
    void Lrotate(node *&target) {
        node *temp = target->lchild;
        target->lchild = temp->rchild;
        temp->rchild = target;
        temp->Size = target->Size;
        target->Size = SizeOf(target->lchild) + SizeOf(target->rchild) + 1;
        target = temp;
    }
    void Rrotate(node *&target) {
        node *temp = target->rchild;
        target->rchild = temp->lchild;
        temp->lchild = target;
        temp->Size = target->Size;
        target->Size = SizeOf(target->lchild) + SizeOf(target->rchild) + 1;
        target = temp;
    }
    void Maintain(node *& target) {
        if (target->lchild != NULL) {
            if (SizeOf(target->rchild) < SizeOf(target->lchild->lchild))
                Lrotate(target);
            else if (SizeOf(target->rchild) < SizeOf(target->lchild->rchild)) {
                Rrotate(target->lchild);
                Lrotate(target);
            }Maintain(target->lchild);
        }
        if (target->rchild != NULL) {
            if (SizeOf(target->lchild) < SizeOf(target->rchild->rchild))
                Rrotate(target);
            else if (SizeOf(target->lchild) < SizeOf(target->rchild->lchild)) {
                Lrotate(target->rchild);
                Rrotate(target);
            }Maintain(target->rchild);
        }
        return;
    }
    void insert(node *&target, KeyType &key, ValType &val) {
        if (!target) {
            target = new node(key, val);
            return;
        }
        target->Size++;
        if (key > target->key)insert(target->rchild, key, val);
        else if (key != target->key)insert(target->lchild, key, val);
        else return;
        Maintain(target);
    }
    ValType& query(node *&target, KeyType key) {
        if (target == NULL) {
            int r = 0;
            return r;
        }
        if (target->key == key)return target->val;
        else if (target->key < key)return query(target->rchild, key);
        else return query(target->lchild, key);
    }
public:
    Size_Balanced_Tree() {
        root = 0;
    }
    void Create_Account(string id) {
        int init = 0;
        insert(root, id, init);
    }
    ValType deposit(KeyType id, int amount) {
        return query(root, id)+=amount;
    }
    ValType withdraw(KeyType id, int amount) {
        return query(root, id) -= amount;
    }
    ValType amount(KeyType id) {
        return query(root, id);
    }
};
Size_Balanced_Tree<string, int>acmngr;
int main() {
    acmngr.Create_Account("尼玛");
    acmngr.Create_Account("泥煤");
    acmngr.deposit("尼玛", 3);
    acmngr.withdraw("尼玛", 2);
    cout << acmngr.amount("尼玛") << endl << acmngr.amount("泥煤");
}

Csdn user default icon
上传中...
上传图片
插入图片