Пример #1
0
int main()
{
	//test creation of multiple types
	Bag<int> IntBag;
	Bag<double> DoubleBag;
	Bag<string> StringBag;
	bool passed;

	for (int j = 0; j < 50000; j++) {
		IntBag.AddItem(j);
		passed = true;
	}
	
	for (int j = 0; j < 50000; j++){
		if (!IntBag.IsPresent(j)){
			cout << "Error: Lost item " << j << endl;
			passed = false;
		}
	}

	if (passed) {
		cout << "Retained all items \n";
	}
	else
		cout << "Failed test\n";
		passed = true;

	for (int j = 50000; j < 50050; j++){
		if (IntBag.IsPresent(j) || IntBag.IsPresent(j*-1)){
			passed = false;
			cout << "Found extra item: " << j << '\n';
		}
	}

	if (passed) {
		cout << "No extra items \n";
	}
	else
		cout << "Failed extra-item test\n";
		passed = false;

	try{
		IntBag.Discard(-1);
	}
	
	catch (BagException){
		passed = true;
		cout << "Raised expected exception on discarding nonexistent item\n";
	}

	if (passed) {
		cout << "Handled discard of non-existent item \n";
	}
	else
		cout << "Failed discard test\n";
	
	IntBag.Discard(12);
	
	if (IntBag.IsPresent(12)) {
		cout << "Failed to discard item\n";
	}
	else
		cout << "Discard-item test passed.\n";
	
	IntBag.Clear();
	
	if (!IntBag.IsEmpty()) {
		cout << "Failed to clear Bag\n";
	}
	else
		cout << "Cleared Bag OK. \n";
	
	if (IntBag.IsEmpty() && IntBag.Size() != 0) {
		cout << "Error: Reports empty but nonzero count.\n";
	}

	system("pause");
	return 0;
}
Пример #2
0
int main()
{
    int n1;
    int ca = 1;
    while (cin >> n1)
    {
        Init();
        for (int i = 0; i < n1; ++i)
        {
            char name[50];
            int cost;
            scanf("%s%d", name, &cost);
            item_getter.insert(make_pair(name, Item(name, cost, NOR)));
        }
        int n2;
        cin >> n2;
        for (int i = 0; i < n2; ++i)
        {
            char name[50];
            int cost;
            char list[300];
            scanf("%s%d:%[^\n]", name, &cost, list);
            item_getter.insert(make_pair(name, Item(name, cost, MIX)));
            vector<pair<string, int> > & vList = mixture[name];
            string slist = list;
            int pos = 0;
           // printf("list='%s'\n", list);
            if(slist.size()<=1) continue;
            while (true)
            {
                int ed = slist.find(',', pos);
                string sub = slist.substr(pos, ed - pos);
                int num;
                sscanf(sub.c_str(), "%s%d", name, &num);
                vList.push_back(pair<string, int>(name, num));
                //printf("dep: %s %d!!\n", name, num);
                if (ed == string::npos) break;
                else pos = ed + 1;
            }
        }
        int n3;
        cin >> n3;
        for (int i = 0; i < n3; ++i)
        {
            char name[50];
            int cost;
            scanf("%s%d", name, &cost);
            item_getter.insert(make_pair(name, Item(name, cost, CUS)));
        }
        Bag bag;
        int cmd_cnt;
        cin>>cmd_cnt;
        while(cmd_cnt--)
        {
            char op;
            char str[1024];
            scanf("%s", str);
            op = str[0];
            string name(str+1);
            if(name[0]>='0'&&name[0]<='9')
            {
                bag.money += atoi(name.c_str());
            }
            else
            {
                if(op=='-') bag.RemoveItem(name);
                else bag.AddItem(name);
            }
            //printf("money = %d\n", bag.money);
            //printf("%c %s\n", op, name.c_str());
        }
        printf("Case %d:\n", ca++);
        printf("%d\n", bag.money);
        printf("%d\n", bag.items.size());
        sort(bag.items.begin(), bag.items.end());
        for(int i=0;i<bag.items.size();++i)
        {
            printf("%s: %d\n", bag.items[i].name.c_str(), bag.items[i].num);
        }
        puts("");
    }
    return 0;
}