[ create a new paste ] login | about

Link: http://codepad.org/soCIfu0e    [ raw code | fork ]

C++, pasted on Nov 3:
#include<iostream>
#include<fstream>
#include<stack>
#include<map>
#include<sstream>
#include<cstdlib>
#include<vector>
using namespace std;

class order{
public:
    void insert(string product, int number){
        if(item.find(product)==item.end()){
            item.insert(pair<string,int>(product,number));
        }else{
			if(item[product]<number){
				item[product]=number;
			}
		}
    }

	void display(){
		cout<<"Order has "<<item.size()<<" items:\n";
		for(map<string,int>::iterator i=item.begin();i!=item.end();++i){
			cout<<i->first<<" : "<<i->second<<'\n';
		}
	}

	bool exist(const string stra,const string strb){
		if(item.empty()) return false;
		map<string,int>::iterator it = item.find(stra);
		if(it!=item.end()){
			if(it->second > 0){
				it = item.find(strb);
				if(it!=item.end()){
					if(it->second > 0)return true;
				}
			}
        }
		return false;
	}

	bool empty(){
		return item.empty();
	}
private:
    map<string, int> item;
};

int main(int argc, char* argv[]){
	ifstream fin;
	//read json
	fin.open(argv[1],ios::in|ios::binary);
	char c;
	stack<char> orders;
	string buffer="";
	string prebuffer="";
	string product="";
	order* newOrder;
	int number;
	vector<order*> cart;
	while(fin.get(c)){
		if(c==' ' || c=='\t' || c=='\n' || c=='\r') continue;
		if(c=='{' || c=='['){// start with { or [
			orders.push(c);
		}else if((orders.top()=='\"' && c=='\"') || c==':' || c==','){//end with " or : or ,
			if(c=='\"') orders.pop();
			if(buffer.size()!=0 && prebuffer.compare("order")==0){
				if(newOrder) cart.push_back(newOrder);
                newOrder = new order();
			}else if(buffer.size()!=0 && prebuffer.compare("product")==0){
                product=buffer;
			}else if(buffer.size()!=0 && prebuffer.compare("Number")==0){
                number=atoi(buffer.c_str());
				if(newOrder) newOrder->insert(product,number);
			}
			if(buffer.size()!=0) prebuffer=buffer;
			buffer="";
		}else if(c=='\"'){//start with "
			orders.push(c);
		}else if(c=='}' || c==']'){//end with } or ]
			orders.pop();
		}else{
			buffer+=c;
		}
	}
	if(buffer.size()!=0 && prebuffer.compare("Number")==0){
		number=atoi(buffer.c_str());
		if(newOrder) newOrder->insert(product,number);
	}
	if(newOrder) cart.push_back(newOrder);
	fin.close();
	//read .txt
	fin.open(argv[2]);
	string line;
	getline(fin,line);
	int n=atoi(line.c_str());
	int m=0;
	string a,b;
	stringstream ss;
	int res=0;
	while(n--){
		getline(fin,line);
		ss.clear();
		ss.str(line);
		while(getline(ss,line,',')){
			m++;
			if(line[line.length()-1]=='\n' || line[line.length()-1]=='\r') line=line.substr(0,line.length()-1);
			if(m%2==1){
				a=line;
			}else{
				b=line;
				res=0;
				for(vector<order*>::iterator i=cart.begin();i!=cart.end();++i){
					if((*i)->exist(a,b)){
						res++;
					}
				}
				cout<<res;
				if(n!=0) cout<<'\n';
			}
		}
	}
	fin.close();
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: