//NumberLevel.txt

#include <code\programmer\questionsDLL.txt>



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



var testFormulForRow(sys,fn){
	for(i=0;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={
		name:(*sys)["имя программы"],
		text:sys->namesoutput[0]+"="+t+";",
		inputnames:sys->namesinput,
		outputnames:sys->namesoutput[0]
		};
	sys->functions.push(Xfunct);
	return 1;
}


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

	return 0;
}
