1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#include "bag.h"
void add_item (Bag<string>& bag);
void init_bag (Bag<string>& bag, const string& fname);
void help ();
void remove_item (Bag<string>& bag);
void save_bag (const Bag<string>& bag, const string& fname);
void show_bag_detail (const Bag<string>& bag);
void show_bag_summary (const Bag<string>& bag);
int main () {
// Declare and initialize bag.
Bag<string> bag1, bag2;
init_bag(bag1, "bag1.txt");
init_bag(bag2, "bag2.txt");
// Command loop...
cout << endl;
help();
string command;
do {
cout << endl;
cout << "bag1: "; show_bag_summary(bag1);
cout << "bag2: "; show_bag_summary(bag2);
cout << endl;
cout << "Command: ";
getline(cin, command);
if (command == "add1") add_item(bag1);
else if (command == "add2") add_item(bag2);
else if (command == "remove1") remove_item(bag1);
else if (command == "remove2") remove_item(bag2);
else if (command == "detail1") show_bag_detail(bag1);
else if (command == "detail2") show_bag_detail(bag2);
else if (command == "diff12") ;// show_detail(bag1.difference(bag2))
else if (command == "diff21") ;// show_detail(bag1.difference(bag2))
else if (command == "intersect") ;// show_detail(bag1.intersection(bag2))
else if (command == "union") ;// show_detail(bag1.union(bag2))
else if (command != "exit")
help();
} while (command != "exit");
// Save bag contents
save_bag(bag1, "bag1.txt");
save_bag(bag2, "bag2.txt");
// system("pause");
return 0;
}
void add_item (Bag<string>& bag){
string item;
cout << "Item to add: ";
getline(cin, item);
if (bag.add(item))
cout << "Item added." << endl;
else
cout << "Failed to add item." << endl;
}
void help () {
cout << "Valid commands: addN removeN detailN (N = 1 or 2)" << endl;
cout << " diff12 diff21 intersect union" << endl;
}
void init_bag (Bag<string>& bag, const string& fname) {
// Open file of items. Exit if file not found.
ifstream f(fname.c_str());
if (!f) return;
// Get all items from file and add to bag.
string item;
while (getline(f, item))
bag.add(item);
// Close file
f.close();
}
void remove_item (Bag<string>& bag){
string item;
cout << "Item to remove: ";
getline(cin, item);
// if (bag.remove(item))
// cout << "Item was removed." << endl;
// else
// cout << "Item was not found." << endl;
cout << (bag.remove(item) ? "Item was removed." : "Item was not found.") << endl;
}
void save_bag (const Bag<string>& bag, const string& fname) {
// Open file for saving bag contents.
ofstream f(fname.c_str());
if (!f) {
cerr << "ERROR: Could not open mybag.txt for output." << endl;
return;
}
// Save bag items
for (int i = 0; i < bag.getCurrentSize(); i++)
f << bag.getItem(i) << endl;
// Close file
f.close();
}
void show_bag_detail (const Bag<string>& bag) {
show_bag_summary(bag);
for (int i = 0; i < bag.getCurrentSize(); i++)
cout << " - " << bag.getItem(i) << endl;
}
void show_bag_summary (const Bag<string>& bag) {
if (bag.isEmpty())
cout << "The bag is empty." << endl;
else {
int size = bag.getCurrentSize();
if (size > 1)
cout << "There are " << size << " items in the bag." << endl;
else
cout << "There is 1 item in the bag." << endl;
}
}
|