Ejemplo n.º 1
0
int main()
{
    Bag<int> mBag;
    
    cout << "size: " << mBag.getSize() << endl;
    cout << "isEmpty(): " << mBag.isEmpty() << endl;
    cout << "adding 1,2,3...\n";
    mBag.add(1);
    mBag.add(2);
    mBag.add(3);
    cout << "size: " << mBag.getSize() << endl;
    cout << "isEmpty(): " << mBag.isEmpty() << endl << endl;
    cout << "Contents: " << mBag;
    Bag<int> mBag2;
    cout << "\nTesting equal operator: ";
    mBag2 = mBag;
    cout << mBag2 << endl;
    cout << "Testing copy constructor: ";
    Bag<int> mBag3(mBag2);
    cout << mBag3 << endl;
    
    cout << "\nTesting remove(2): ";
    mBag3.remove(2);
    cout << mBag3 << endl;
    
    cout << "\nTesting clear(): ";
    mBag3.clear();
    cout << mBag3 << endl;
    
    cout << "\nTesting contains(3): " << mBag2.contains(3) << endl;
    mBag2.contains(3);
    cout << mBag2 << endl;
    cout << "\nTesting contains(7): " << mBag2.contains(7) << endl;
    mBag2.contains(7);
    cout << mBag2 << endl;
    
    Bag<int> mBag4;
    mBag4.add(1);
    mBag4.add(2);
    mBag4.add(3);
    mBag4.add(4);
    mBag4.add(2);
    cout << "\nTesting getFrequency(2): " << mBag4.getFrequency(2) << endl;
    cout << "Bag contains: " << mBag4 << endl;
    
    Bag<int> test;
    cout << "\nTesting == operator test==bag4: " << (test == mBag4) << endl;
    cout << "Testing != operator test!=bag4: " << (test != mBag4) << endl;
    
    test += 1;
    cout << "Testing +=: contents " <<  test << endl;
    test += 33;
    cout << "Contents: " << test <<endl;
    
    cout << "Testing -=: contents " <<  test << endl;
    test -= 33;
    cout << "Contents: " << test <<endl;
    
    return 0;
}
Ejemplo n.º 2
0
int main() {
   Bag<int> xi;
   Bag<char> xc;
   Bag<int*> xp; // Uses partial specialization for pointer types.

   xi.add(10);
   xi.add(9);
   xi.add(8);
   xi.print();

   xc.add('a');
   xc.add('b');
   xc.add('c');
   xc.print();

   int i = 3, j = 87, *p = new int[2];
   *p = 8;
   *(p + 1) = 100;
   xp.add(&i);
   xp.add(&j);
   xp.add(p);
   xp.add(p + 1);
   p = NULL;
   xp.add(p);
   xp.print();
}
Ejemplo n.º 3
0
Digraph& Digraph::operator=(const Digraph& G) {
	printf("Assigning Digraph\n");
	if (this == &G) return *this;

	// Free memory
	delete[] adj_;

	// Allocate memory
	V_ = G.V_;
	E_ = G.E_;
	Bag<int>* new_adj = new Bag<int>[G.V_];

	// Copy elements
	for (int v = 0; v < G.V_; v++) {
		// reverse so that adjacency list is in the same order as original
		Bag<int> reverse;
		for (int w : G.adj_[v])
			reverse.add(w);
		for (int w : reverse)
			new_adj[v].add(w);
	}

	// Reassign variables
	adj_ = new_adj;

	return *this;
}
Ejemplo n.º 4
0
int main() {
    Bag<int> lengths;
    Bag<char, 26> letters;
    Bag<string, 20000> words;
    
    ifstream is("/Users/alexandernohe/Downloads/labs_icpp/Exercises/Skeletons/Tale.txt");
    
    if(!is)
    {
        cerr << "Cannot open Tale.txt!" << endl;
        return 1;
    }
    
    string s;
    while (is >> s)
    {
        string w;
        for (int i=0; i < s.length(); i++)
        {
            char ch = s[i];
            
            if (isalpha(ch))
            {
                w += toupper(ch);
                letters.add(toupper(ch));
            }
        }
        lengths.add(int(w.length()));
        words.add(w);
    }
    
    cout << "-- lengths --" << endl;
    cout << "1:" << lengths.count(1) << " 2:" << lengths.count(2) << " 3:" << lengths.count(3) << " 4:" << lengths.count(4) << " 5:" << lengths.count(5) << std::endl;
    std::cout << "-- letters --" << std::endl;
    cout << "A:" << letters.count('A') << " B:" << letters.count('B') << " C:" << letters.count('C') << endl;
    std::cout << words.uniqueSize() << " out of " << words.count() << " are unique " << (double((words.uniqueSize())/double(words.count()))*100) << "%" << endl;
    
    for (int i=0; i < words.count(); i++) {
        std::string w;
        int n = words.item(i, w);
        if(n != 0)
        {
            cout << n << ":" << w << "\n";
        }
    }
    is.close();
}
Ejemplo n.º 5
0
Digraph::Digraph(const Digraph& G) : V_(G.V_), E_(G.E_) {
	printf("Copying Digraph\n");
	adj_ = new Bag<int>[G.V_];
	for (int v = 0; v < G.V_; v++) {
		// reverse so that adjacency list is in the same order as original
		Bag<int> reverse;
		for (int w : G.adj_[v])
			reverse.add(w);
		for (int w : reverse)
			adj_[v].add(w);
	}		
}