// NumberLevel.code

#include <code\programmer\questionsDLL.txt>



bool scaneRowOfIsNumber(var row){
	i=0;
	for(;i<row->size();++i)if({"int","float","double","digit"}.find(typeof((*row)[i]))<0)return 0;
	return 1;
}



var testFormulForRow(var sys,var fn){
	i=0;
	for(;i<sys->inputs.size();++i){
		a=(sys->outputs)[i];
		b=(sys->inputs)[i];
		if(fn==0)x=a-b;
		if(fn==1)x=a+b;
		if(fn==2){if(b)x=a/b;else x=# ;}
		if(fn==3)x=a*b;
		if(!isset(y))y=x;
		if(y!=x)return 0;
		}
	if(!isset(sys->namesinput))sys->namesinput={"input"};
	if(!isset(sys->namesoutput))sys->namesoutput={"output"};
	if(fn==0)t=(sys->namesinput)[0]+"+"+y;
	if(fn==1)t=((string)y)+"-"+(sys->namesinput)[0];
	if(fn==2)t=((string)y)+"*"+(sys->namesinput)[0];
	if(fn==3)t=((string)y)+"/"+(sys->namesinput)[0];
	Xfunct={inputnames:sys->namesinput,name:(*sys)["имя программы"],outputnames:(sys->namesoutput)[0],text:(sys->namesoutput)[0]+"="+t+";"};
	sys->functions.push(Xfunct);
	return 1;
}



bool NumberLevel(var sys){
	if(!scaneRowOfIsNumber(&sys->inputs))return 0;
	if(!scaneRowOfIsNumber(&sys->outputs))return 0;
	i=0;
	for(;i<4;++i)if(testFormulForRow(sys,i))return 1;
	if(!isset(sys->namesinput))sys->namesinput={"input"};
	if(!isset(sys->namesoutput))sys->namesoutput={"output"};
	R=question("function",{sys->outputs,sys->inputs},{(sys->namesoutput)[0],(sys->namesinput)[0]});
	Progs={};
	i=0;
	for(;i<R.size();++i){
		t=PROGRAM(R[i]);
		if(typeof(t)=="program")Progs.push(t);
		}
	if(Progs.size()){
		Powers={};
		i=0;
		for(;i<Progs.size();++i)Powers.push(Progs[i].getPower(1));
		P=Progs[Powers.find(Powers.min())];
		Xfunct={inputnames:sys->namesinput,name:(*sys)["имя программы"],outputnames:(sys->namesoutput)[0],text:((string)P)};
		sys->functions.push(Xfunct);
		return 1;
		}
	return 0;
}


// NumberLevel.code	:-|