⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parts.h

📁 ssd5 op6的答案
💻 H
字号:
// parts.h
//this is Optional Exercise 6
//the author is liqinwei
//2007/12/6
#ifndef _PARTS_H_
#define _PARTS_H_

#include <vector>
#include <map>
#include <string>

using namespace std;

//**************** Part ****************
class Part {
	
public:
	string name;

	// TODO: Finish declaration
	//create a object
	map<Part*,int> subparts;
	
	Part(string const &n) : name(n) {};
	void describe(void);
	int count_howmany(Part const *p);
};


//**************** NameContainer ****************
class NameContainer  {

private:
	map<string,Part*> name_map;

public:

	NameContainer(void) {};

	Part* lookup(string const &name) {
    
        // TODO: Finish implementation

		//automatically create an instance of class Part with the 
		//given name if one does not already exist in the map.
		map<string,Part*>::iterator it;
		for(it = name_map.begin();it != name_map.end();it++)
		{
			if((*it).first == name)
				return (*it).second;
		}
		Part *newpart = new Part(name);
		name_map.insert(map<string,Part*>::value_type(name,newpart));
		return newpart;
	}
};

NameContainer partContainer;  

//**************** Part Member Functions ****************
void Part::describe(void) {
	
    // TODO: Finish implementation
	//this function putout the part and it's subparts
	/*
		look as:
		Part floor subparts are:
		4 wing
		1 central_lobby
	*/
	cout << "Part "<<this->name << " subparts are:"<<endl;
	map<Part*,int>::iterator it;
	if(subparts.empty())
		cout << "there is no such subparts."<<endl;
	else
		for(it = subparts.begin();it != subparts.end();it++)
		{		
			cout << (*it).second <<" "<<  (*it).first->name << endl;
		}
}

int Part::count_howmany(Part const *p) {
		
    // TODO: Finish implementation

	int total = 1;
	map<Part*,int>::iterator it;
	
	//this is the function of recursion 
	if(!subparts.empty()){
		for(it = subparts.begin();it != subparts.end();it++)
		{
			if(this == p)//when find *p return  
				return 1;
			//if not, find it's subparts
			int t = (*it).first->count_howmany(p);
			if(this != p&&t != 0)
				return total = (*it).second * t;
		}
	}
	//if the subparts is empty and not find it
	//return 0
	else if(this != p)
		return 0;
}

//**************** Miscellaneous Functions ****************
void add_part(string const &x, int q, string const &y) {

    // TODO: Finish implementation
	//this function adds the part named 
	//y as a new subpart of the Part named x
	Part *fatherPart = partContainer.lookup(x);
	Part *subPart = partContainer.lookup(y);
	fatherPart->subparts.insert(map<Part*,int>::value_type(subPart,q));
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -