簡體   English   中英

Union Function C ++

[英]Union Function C++

我需要創建一個Union函數來生成兩個包的聯合。 我的問題是,當我運行程序時,該函數將添加元素,如果多次輸入,有時它可能會重復相同的數字。 如何改進此功能,使數字只顯示一次?

#pragma once
//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 Pearson Education. All rights reserved.

/** Header file for an array-based implementation of the ADT bag.
@file ArrayBag.h */

#ifndef _ARRAY_BAG
#define _ARRAY_BAG

#include "BagInterface.h"
#include <cstddef>

template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private:
    static const int DEFAULT_CAPACITY = 15; // Small size to test for a full bag
    ItemType items[DEFAULT_CAPACITY];      // Array of bag items
    int itemCount;                         // Current count of bag items 
    int maxItems;                          // Max capacity of the bag

                                           // Returns either the index of the element in the array items that
                                           // contains the given target or -1, if the array does not contain 
                                           // the target.
                                           //int getIndexOf(const ItemType& target) const;

public:
    ArrayBag();
    int getIndexOf(const ItemType& target) const;
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const ItemType& newEntry);
    bool remove(const ItemType& anEntry);
    bool remove2(const ItemType& anEntry);
    void clear();
    bool contains(const ItemType& anEntry) const;
    int getFrequencyOf(const ItemType& anEntry) const;
    void  displayBag2()const;
    vector<ItemType> toVector() const;
    ArrayBag<ItemType> Union(const ArrayBag<ItemType> myBag2);
    ArrayBag<ItemType> Intersection(const ArrayBag<ItemType> myBag2);
    ArrayBag<ItemType> Difference(const ArrayBag<ItemType> myBag2);
}; // end ArrayBag

   //  Created by Frank M. Carrano and Tim Henry.
   //  Copyright (c) 2013 Pearson Education. All rights reserved.

   /** Implementation file for the class ArrayBag.
   @file ArrayBag.cpp */

#include "ArrayBag.h"
#include <cstddef>

template<class ItemType>
ArrayBag<ItemType>::ArrayBag() : itemCount(0), maxItems(DEFAULT_CAPACITY)
{
}  // end default constructor

template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
    return itemCount;
}  // end getCurrentSize

template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
    return itemCount == 0;
}  // end isEmpty

template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
    bool hasRoomToAdd = (itemCount < maxItems);
    if (hasRoomToAdd)
    {
        items[itemCount] = newEntry;
        itemCount++;
    }  // end if

    return hasRoomToAdd;
}  // end add

   /*
   template<class ItemType>
   bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
   {

   return false; // STUB
   }  // end remove
   */

template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
{
    int locatedIndex = getIndexOf(anEntry);
    bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
    if (canRemoveItem)
    {
        itemCount--;
        items[locatedIndex] = items[itemCount];
    }  // end if

    return canRemoveItem;
}  // end remove


template<class ItemType>
bool ArrayBag<ItemType>::remove2(const ItemType& anEntry)
{
    int locatedIndex = getIndexOf(anEntry);
    bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
    if (canRemoveItem)
    {
        for (int i = 0; i < getCurrentSize(); i++)
        {
            if (items[i] == anEntry)
            {
                for (int j = i; j < (getCurrentSize() - 1); j++)
                {
                    items[j] = items[j + 1];
                }
                itemCount--;
            }
        }

        //items[locatedIndex] = items[itemCount];

    }  // end if

    return canRemoveItem;
}  // end remove


   /*
   // STUB
   template<class ItemType>
   void ArrayBag<ItemType>::clear()
   {
   // STUB
   }  // end clear
   */

template<class ItemType>
void ArrayBag<ItemType>::clear()
{
    itemCount = 0;
}  // end clear

template<class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
    int frequency = 0;
    int curIndex = 0;       // Current array index
    while (curIndex < itemCount)
    {
        if (items[curIndex] == anEntry)
        {
            frequency++;
        }  // end if

        curIndex++;          // Increment to next entry
    }  // end while

    return frequency;
}  // end getFrequencyOf


template<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
    return getIndexOf(anEntry) > -1;
}  // end contains

   /* ALTERNATE 1: First version
   template<class ItemType>
   bool ArrayBag<ItemType>::contains(const ItemType& target) const
   {
   return getFrequencyOf(target) > 0;
   }  // end contains

   // ALTERNATE 2: Second version
   template<class ItemType>
   bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
   {
   bool found = false;
   int curIndex = 0;        // Current array index
   while (!found && (curIndex < itemCount))
   {
   if (anEntry == items[curIndex])
   {
   found = true;
   } // end if

   curIndex++;           // Increment to next entry
   }  // end while

   return found;
   }  // end contains
   */

template<class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
    vector<ItemType> bagContents;
    for (int i = 0; i < itemCount; i++)
        bagContents.push_back(items[i]);

    return bagContents;
}  // end toVector

   // private
template<class ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const
{
    bool found = false;
    int result = -1;
    int searchIndex = 0;

    // If the bag is empty, itemCount is zero, so loop is skipped
    while (!found && (searchIndex < itemCount))
    {
        if (items[searchIndex] == target)
        {
            found = true;
            result = searchIndex;
        }
        else
        {
            searchIndex++;
        }  // end if
    }  // end while

    return result;
}  // end getIndexOf

template<class ItemType>
void  ArrayBag<ItemType>::displayBag2()const
{
    cout << "The bag contains: < ";
    for (int i = 0; i < getCurrentSize(); i++)
    {
        cout << items[i] << " ";
    }
    cout << ">";
} // end displayBag 


template <class ItemType>
ArrayBag<ItemType> ArrayBag <ItemType> ::Union(const ArrayBag<ItemType> bag2)
{
    ArrayBag<ItemType> unionBag;

    for (int i = 0; i < getCurrentSize(); i++)
    {
        unionBag.add(items[i]);
    }

    for (int i = 0; i < bag2.getCurrentSize(); i++)
    {
        unionBag.add(bag2.items[i]);
        if (items[i] == bag2.items[i])
            unionBag.remove(bag2.items[i]);
    }   

    return unionBag;
}


template <class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::Intersection(const ArrayBag<ItemType> bag2)
{
    ArrayBag<ItemType> interBag;

    for (int i = 0; i < getCurrentSize(); i++)
    {
        if (bag2.contains(items[i]))
            interBag.add(items[i]);

    }

    return interBag;
}

template<class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::Difference(const ArrayBag<ItemType> bag2)
{
    ArrayBag<ItemType> diffBag;

    for (int i = 0; i < getCurrentSize(); i++)
    {


        if (!bag2.contains(items[i]))
                diffBag.add(items[i]);
        }



        return diffBag;
    }


    #endif

    #pragma once
    //  Created by Frank M. Carrano and Tim Henry.
    //  Copyright (c) 2013 Pearson Education. All rights reserved.

    /** Listing 1-1.
    @file BagInterface.h */
    #ifndef _BAG_INTERFACE
    #define _BAG_INTERFACE

    #include <vector>
    using namespace std;

    template<class ItemType>
    class BagInterface
    {
    public:
        /** Gets the current number of entries in this bag.
        @return The integer number of entries currently in the bag. */
        virtual int getCurrentSize() const = 0;

        /** Sees whether this bag is empty.
        @return True if the bag is empty, or false if not. */
        virtual bool isEmpty() const = 0;

        /** Adds a new entry to this bag.
        @post  If successful, newEntry is stored in the bag and
        the count of items in the bag has increased by 1.
        @param newEntry  The object to be added as a new entry.
        @return  True if addition was successful, or false if not. */
        virtual bool add(const ItemType& newEntry) = 0;

        /** Removes one occurrence of a given entry from this bag,
        if possible.
        @post  If successful, anEntry has been removed from the bag
        and the count of items in the bag has decreased by 1.
        @param anEntry  The entry to be removed.
        @return  True if removal was successful, or false if not. */
        virtual bool remove(const ItemType& anEntry) = 0;

        /** Removes all entries from this bag.
        @post  Bag contains no items, and the count of items is 0. */
        virtual void clear() = 0;

        /** Counts the number of times a given entry appears in bag.
        @param anEntry  The entry to be counted.
        @return  The number of times anEntry appears in the bag. */
        virtual int getFrequencyOf(const ItemType& anEntry) const = 0;

        /** Tests whether this bag contains a given entry.
        @param anEntry  The entry to locate.
        @return  True if bag contains anEntry, or false otherwise. */
        virtual bool contains(const ItemType& anEntry) const = 0;

        /** Empties and then fills a given vector with all entries that
        are in this bag.
        @return  A vector containing all the entries in the bag. */
        virtual vector<ItemType> toVector() const = 0;

    }; // end BagInterface
    #endif

#include <iostream> 
#include <string> 
#include <algorithm>
#include <vector>
#include "ArrayBag.h" 


using  namespace std;
void  displayBag(ArrayBag<string>& bag)
{
    cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl;
    vector<string> bagItems = bag.toVector();
    int  numberOfEntries = (int)bagItems.size();
    for (int i = 0; i < numberOfEntries; i++) {
        cout << bagItems[i] << " ";
    } // end for     cout << endl << endl; 
} // end displayBag 


void  bagTester(ArrayBag<string>& bag)
{
    cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
    displayBag(bag);
    string items[] = { "one", "two", "three", "four", "five", "one" };
    cout << "Add 6 items to the bag: " << endl; for (int i = 0; i < 6; i++) { bag.add(items[i]); } // end for 
    displayBag(bag);
    cout << "isEmpty: returns " << bag.isEmpty() << "; should be 0 (false)" << endl;
    cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
    cout << "Try to add another entry: add(\"extra\") returns " << bag.add("extra") << endl;
}   // end bagTester 

int  main()
{
    ArrayBag<int> bag1, bag2;
    char answer, answer2;
    int n, n2;

    cout << "How many numbers are going to be entered: ";
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        int num;
        cout << "Enter a number: ";
        cin >> num;
        bag1.add(num);
    }

    cout << endl;
    bag1.displayBag2();
    cout << endl;
    cout << "The bag contains: " << bag1.getCurrentSize() << " numbers.";
    cout << endl << endl;

    cout << "Now we are going to create another bag for comparison." << endl;

    cout << "How many numbers are going to be entered: ";
    cin >> n2;

    for (int i = 0; i < n2; i++)
    {
        int num;
        cout << "Enter a number: ";
        cin >> num;
        bag2.add(num);
    }

    cout << endl;
    bag2.displayBag2();
    cout << endl;
    cout << "The bag contains: " << bag2.getCurrentSize() << " numbers.";
    cout << endl << endl;

    cout << "The union of both bags is: " << endl;
    ArrayBag<int> bag3 = bag1.Union(bag2);
    bag3.displayBag2();
    cout << endl;

    cout << "The intersection of both bags is: " << endl;
    ArrayBag<int> bag4 = bag1.Intersection(bag2);
    bag4.displayBag2();
    cout << endl;

    cout << "The difference of both bags is: " << endl;
    ArrayBag<int> bag5 = bag1.Difference(bag2);
    bag5.displayBag2();
    cout << endl;


    cout << "The frequency of the number 4 is: " << bag1.getFrequencyOf(4);
    cout << endl;

    cout << "The index of the number 9 is: " << bag1.getIndexOf(9);
    cout << endl << endl;

    cout << "Now we are going to remove 4" << endl << endl;
    for (int a = 0; a < n; a++)
    {
        bag1.remove2(4);
    }
    bag1.displayBag2();

    cout << endl;
    cout << "The bag contains: " << bag1.getCurrentSize() << " numbers.";

    cout << endl << endl;

    cout << "Would you like to remove another number (Y/N)? ";
    cin >> answer;

    if (answer == 'Y' || answer == 'y')
    {
        int remove;
        cout << endl;
        cout << "Which number would you like to remove: ";
        cin >> remove;
        for (int a = 0; a < n; a++)
        {
            bag1.remove2(remove);
        }
        cout << endl;

        bag1.displayBag2();

    }
    else
        bag1.displayBag2();

    cout << endl;
    cout << "Would you like to add more numbers to the bag? (Y/N)";
    cin >> answer2;


    if (answer2 == 'Y' || answer2 == 'y')
    {
        int add, quantity;
        cout << endl;

        cout << "How many numbers would you like to add: ";
        cin >> quantity;
        for (int a = 0; a < quantity; a++)
        {
            cout << endl;
            cout << "Which number would you like to add: ";
            cin >> add;
            bag1.add(add);
        }
        cout << endl;

        bag1.displayBag2();
        cout << endl;
        cout << "The bag contains: " << bag1.getCurrentSize() << " numbers.";
        cout << endl;
    }

    cout << endl;
    cout << endl << "Good bye!" << endl << endl;
    system("pause");
    return  0;
} // end main
if (items[i] == bag2.items[i])

僅當您在bag2檢查的bag2存在當前對象中的完全相同的索引時 ,才會出現這種情況。

如果需要重復刪除,則需要檢查聯合中的任何項是否與源中的項匹配。 您也應該在插入過程中執行此操作,以避免insert-then-delete場景,並且您應該對兩個源( bag2 當前對象)執行此操作。 這將是這樣的:

template <class ItemType>
ArrayBag<ItemType> ArrayBag <ItemType> ::Union(const ArrayBag<ItemType> bag2) {
    ArrayBag<ItemType> unionBag;

    // Add my items, removing dupes.

    for (int i = 0; i < getCurrentSize(); i++)
        if (! unionBag.contains(bag2.items[i]))
            unionBag.add(items[i]);

    // Add other items, removing dupes.

    for (int i = 0; i < bag2.getCurrentSize(); i++)
        if (! unionBag.contains(bag2.items[i]))
            unionBag.add(bag2.items[i]);

    return unionBag;
}

如果你真的想要真正的設置行為(每個項目只有一個),你可能想要考慮使用一個集合而不是一個包的可能性,至少在構建聯合時,你可以將每個設置項目轉移到一個包。 無論您嘗試添加多少次,每個項目都只有一個。


當然,對於(2)對兩個袋的結合進行相反 解釋,一個涉及去除重復。 可以提出一個論點,即集合 {1,2,3}{2,3,4}是:

{1,2,3} U {2,3,4} => {1,2,3,4}

這兩個項目作為的工會應該是:

{1,2,3} U {2,3,4} => {1,2,2,3,3,4}

畢竟,一個袋子只是一個物體容器,你可以在袋子里放置一個以上的物品。 將一袋七個蘋果倒入另一個三個蘋果袋中,非常適合給你一袋十個蘋果,而不是一個,三個或四個(取決於重復的去除方式)。


(a)有人會說“更明智”,但我不想侮辱觀眾:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM