- 最後登錄
- 2024-9-20
- 在線時間
- 244 小時
- 註冊時間
- 2008-3-25
- 閱讀權限
- 50
- 精華
- 0
- UID
- 3819761
- 帖子
- 487
- 積分
- 11596 點
- 潛水值
- 28699 米
| 回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。 公布答案:- // newBag.h
- #ifndef NEWBAG_INCLUDED
- #define NEWBAG_INCLUDED
- // Later in the course, we'll see that templates provide a much nicer
- // way of enabling us to have Bags of different types. For now, we'll
- // use a typedef.
- typedef unsigned long ItemType;
- const int DEFAULT_MAX_ITEMS = 200;
- class Bag
- {
- public:
- Bag(int capacity = DEFAULT_MAX_ITEMS);
- // Create an empty bag with the given capacity.
- bool empty() const; // Return true if the bag is empty, otherwise false.
- int size() const;
- // Return the number of items in the bag. For example, the size
- // of a bag containing "cumin", "cumin", "cumin", "turmeric" is 4.
- int uniqueSize() const;
- // Return the number of distinct items in the bag. For example,
- // the uniqueSize of a bag containing "cumin", "cumin", "cumin",
- // "turmeric" is 2.
- bool insert(const ItemType& value);
- // Insert value into the bag. Return true if the value was
- // actually inserted. Return false if the value was not inserted
- // (perhaps because the bag has a fixed capacity and is full).
- int erase(const ItemType& value);
- // Remove one instance of value from the bag if present.
- // Return the number of instances removed, which will be 1 or 0.
- int eraseAll(const ItemType& value);
- // Remove all instances of value from the bag if present.
- // Return the number of instances removed.
- bool contains(const ItemType& value) const;
- // Return true if the value is in the bag, otherwise false.
- int count(const ItemType& value) const;
- // Return the number of instances of value in the bag.
- void swap(Bag& other);
- // Exchange the contents of this bag with the other one.
- // Iteration functions
- void start(); // start an iteration
- void next(); // advance to next item
- bool ended() const; // iteration has passed end
- const ItemType& currentValue() const; // item at current position
- int currentCount() const; // count of current item
- // Housekeeping functions
- ~Bag();
- Bag(const Bag& other);
- Bag& operator=(const Bag& rhs);
- private:
- // Since this structure is used only by the implementation of the
- // Bag class, we'll make it private to Bag. Alternatively,
- // we could have declared it outside the Bag class, but then
- // clients could use it, and there's no need for them to.
- struct Node
- {
- ItemType m_value;
- int m_count;
- };
- Node* m_data; // dynamic array of the items in the bag, with counts
- int m_uniqueSize; // how many distinct items in the bag
- int m_size; // total number of items in the bag
- int m_capacity; // the maximum number of items
- int m_current; // index of current node during iteration
- // At any time, the elements of m_data indexed from 0 to m_uniqueSize-1
- // are in use. m_size is the sum of the m_counts of those elements.
- int find(const ItemType& value) const;
- // Return index of the node in m_data whose m_value == value if there is
- // one, else -1
- int doErase(const ItemType& value, bool all);
- // Remove one or all instances of value from the bag if present,
- // depending on the second parameter. Return the number of instances
- // removed.
- };
- // Inline implementations
- inline
- int Bag::size() const
- {
- return m_size;
- }
- inline
- int Bag::uniqueSize() const
- {
- return m_uniqueSize;
- }
- inline
- bool Bag::empty() const
- {
- return size() == 0;
- }
- inline
- int Bag::erase(const ItemType& value)
- {
- return doErase(value, false);
- }
- inline
- int Bag::eraseAll(const ItemType& value)
- {
- return doErase(value, true);
- }
- inline
- bool Bag::contains(const ItemType& value) const
- {
- return find(value) != -1;
- }
- inline
- void Bag::start()
- {
- m_current = 0;
- }
- inline
- void Bag::next()
- {
- m_current++;
- }
- inline
- bool Bag::ended() const
- {
- return m_current >= m_uniqueSize;
- }
- inline
- const ItemType& Bag::currentValue() const
- {
- return m_data[m_current].m_value;
- }
- inline
- int Bag::currentCount() const
- {
- return m_data[m_current].m_count;
- }
- #endif // NEWBAG_INCLUDED
複製代碼
- // newBag.cpp
- #include "newBag.h"
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- void exchange(int& a, int& b)
- {
- int t = a;
- a = b;
- b = t;
- }
- Bag::Bag(int capacity)
- : m_uniqueSize(0), m_size(0), m_capacity(capacity), m_current(0)
- {
- if (capacity < 0)
- {
- cout << "A Bag capacity must not be negative." << endl;
- exit(1);
- }
- m_data = new Node[m_capacity];
- }
- Bag::Bag(const Bag& other)
- : m_uniqueSize(other.m_uniqueSize), m_size(other.m_size),
- m_capacity(other.m_capacity), m_current(other.m_uniqueSize)
- {
- // Iterator state in fresh copy is undefined. Let's say it's ended.
- // Setting m_current above to other.uniqueSize does that.
- m_data = new Node[m_capacity];
- for (int k = 0; k < m_uniqueSize; k++)
- m_data[k] = other.m_data[k];
- }
- Bag::~Bag()
- {
- delete [] m_data;
- }
- Bag& Bag::operator=(const Bag& rhs)
- {
- if (this != &rhs)
- {
- Bag temp(rhs);
- swap(temp);
- }
- return *this;
- }
- bool Bag::insert(const ItemType& value)
- {
- int pos = find(value);
- if (pos != -1) // found
- m_data[pos].m_count++;
- else
- {
- if (uniqueSize() == m_capacity) // no room to insert
- return false;
- m_data[m_uniqueSize].m_value = value;
- m_data[m_uniqueSize].m_count = 1;
- m_uniqueSize++;
- }
- m_size++;
- return true;
- }
- int Bag::count(const ItemType& value) const
- {
- int pos = find(value);
- return pos == -1 ? 0 : m_data[pos].m_count;
- }
- void Bag::swap(Bag& other)
- {
- // Swap the m_data pointers to dynamic arrays.
- Node* tempData = m_data;
- m_data = other.m_data;
- other.m_data = tempData;
- // Swap uniqueSize, size, and capacity.
- exchange(m_uniqueSize, other.m_uniqueSize);
- exchange(m_size, other.m_size);
- exchange(m_capacity, other.m_capacity);
- // Iterator state after swap is undefined. Let's say it's ended.
- m_current = m_uniqueSize;
- other.m_current = other.m_uniqueSize;
- }
- int Bag::find(const ItemType& value) const
- {
- // Do a linear search through the array.
- for (int pos = 0; pos < m_uniqueSize; pos++)
- if (m_data[pos].m_value == value)
- return pos;
- return -1;
- }
- int Bag::doErase(const ItemType& value, bool all)
- {
- int pos = find(value);
- if (pos == -1) // not found
- return 0;
- // If erasing one, and there are more than one, just decrement
- if (!all && m_data[pos].m_count > 1)
- {
- m_data[pos].m_count--;
- m_size--;
- return 1;
- }
- // If erasing all, or erasing one whose count is 1, move last array
- // item to replace the one to be erased
- int nErased = m_data[pos].m_count;
- m_size -= nErased;
- m_uniqueSize--;
- m_data[pos] = m_data[m_uniqueSize];
- return nErased;
- }
複製代碼 ... |
|