map更新但又没更新,浮点错误求调
查看原帖
map更新但又没更新,浮点错误求调
992529
upto_300楼主2024/8/14 16:59

代码略见题解,具体错误见未注释debug代码 若有人能解决,我愿做其粉丝

#include<bits/stdc++.h>
using namespace std;
int64_t addr_to,def_type_n,def_ele_n;
struct Type{
	string name;
	int64_t size,align;
	vector<Type*>_type;
	vector<string>_name;
	Type(){name="";size=align=0;}
	Type(string _1,int64_t _2,int64_t _3){name=_1,size=_2,align=_3;}
};
map<string,Type*>types;
Type def_type[10005];
struct Element{
	Type type;
	string name;
	int64_t align,size,addr;
	Element(){name="",addr=0;}
};
map<string,Element*>eles;
map<pair<int64_t,int64_t>,Element*>eles_in;
Element def_ele[10005];
//Oddest thing happened!
void define(){
	Type newtype;
	int64_t k,at=0;
	cin>>newtype.name>>k;
	if(newtype.name!="a")printf("}%s's size=%d,align=%d,memsize=%d;\n",types["a"]->name.c_str(),types["a"]->size,types["a"]->align,types["a"]->_name.size());
// 	printf("struct %s{\n",newtype.name.c_str());
	while(k--){
		string s,name;
		cin>>s>>name;
		Type type=*types[s];
// 		printf("  %s{size=%d,align=%d,memsize=%d} %s;\n",type.name.c_str(),type.size,type.align,type._name.size(),name.c_str());
		newtype._type.push_back(&type);
		newtype._name.push_back(name);
		newtype.align=max(newtype.align,type.align);
		if(at%type.align!=0)at=(at/type.align+1)*type.align;
		at+=type.size;
	}
	if(at%newtype.align!=0)at=(at/newtype.align+1)*newtype.align;
	newtype.size=at;
	def_type[++def_type_n]=newtype;
	types[newtype.name]=&newtype;
	Type type=*types[newtype.name];
	if(newtype.name=="a")printf("%s's size=%d,align=%d,memsize=%d;\n",types["a"]->name.c_str(),types["a"]->size,types["a"]->align,types["a"]->_name.size());
//	cout<<newtype.size<<' '<<newtype.align<<'\n';
}
void make(){
	string itstype;
	Element newele;
	cin>>itstype>>newele.name; 
	if(addr_to%newele.align!=0)addr_to=(addr_to/newele.align+1)*newele.align;
	newele.type=*types[itstype];
	newele.align=newele.type.align;
	newele.size=newele.type.size;
	newele.addr=addr_to;
	addr_to+=newele.size;
	def_ele[++def_ele_n]=newele;
	eles[newele.name]=&newele;
	eles_in[{newele.addr,addr_to-1}]=&newele;
	cout<<newele.addr<<'\n';
}
void visit(){
	vector<string>names;
	string s,s_="";
	cin>>s;
	for(char c:s)
		if(c=='.')names.push_back(s_),s_="";
		else s_+=c;
	names.push_back(s_);
	int64_t at=eles[names[0]]->addr;
	for(int i=0;i<names.size();i++){
		string name=names[i];
		Element ele=*eles[name];
		for(int i=0;i<ele.type._name.size();i++){
			Type _type=*(ele.type._type[i]);
			string _name=ele.type._name[i];
			if(at%_type.align!=0)at=(at/_type.align+1)*_type.align;
			if(name==_name)break;
			else at+=_type.size;
		}
	}
	cout<<at<<'\n';
}
void seek(){
	int64_t addr;
	Element ele;
	string find="";
	cin>>addr;
	if(addr>=addr_to){cout<<"ERR\n";return;}
	for(auto area:eles_in)
		if(area.first.first<=addr&&addr<=area.first.second)
			{ele=*(area.second);break;}
	find+=ele.name;                                                                                                                                                  
	int64_t goal=addr-ele.addr,start=0,end;
	Type type=ele.type;
	while(!type._name.empty())
		for(int i=0;i<type._name.size();i++){
			Type _type=*(type._type[i]);
			string _name=type._name[i];
			end=start+_type.size;
			if(start<=goal&&goal<end){
				find+='.'+_name;
				type=_type;
				break;
			}else{
				if(i+1==type._name.size()){cout<<"ERR";return;}
				if(end%_type.align!=0)end=(end/_type.align+1)*_type.align;
				if(goal<end){cout<<"ERR";return;}
				start=end;
			}
		}
	cout<<find<<'\n';
}
int main(){
	def_type[1]=Type("byte",1,1);
	def_type[2]=Type("short",2,2);
	def_type[3]=Type("int",4,4);
	def_type[4]=Type("long",8,8);
	types["byte"]=&def_type[1];
	types["short"]=&def_type[2];
	types["int"]=&def_type[3];
	types["long"]=&def_type[4];
	def_type_n=4;
	int n;
	cin>>n;
	while(n--){
		int type;
		cin>>type;
		if(type==1)define();
		if(type==2)make();
		if(type==3)visit();
		if(type==4)seek();
	}
}
2024/8/14 16:59
加载中...