我正在尝试扩展TinyGP软件的功能集,以包括非线性函数,例如sin,cos和tan。 问题是,树的打印是通过递归,打印个人,然后是函数(+, - ,*,/)然后是另一个人来完成的。所以结果就像(X2 * 2.365789)。但是,sin只接受一个参数sin(x)。如何更新打印方法?
打印方法的源代码概述如下。
int print_indiv( char []buffer, int buffercounter ) {
int a1=0, a2;
if ( buffer[buffercounter] < FSET_START ) {
if ( buffer[buffercounter] < varnumber )
System.out.print( "X"+ (buffer[buffercounter] + 1 )+ " ");
else
System.out.print( x[buffer[buffercounter]]);
return( ++buffercounter );
}
switch(buffer[buffercounter]) {
case ADD: System.out.print( "(");
a1=print_indiv( buffer, ++buffercounter );
System.out.print( " + ");
break;
case SUB: System.out.print( "(");
a1=print_indiv( buffer, ++buffercounter );
System.out.print( " - ");
break;
case MUL: System.out.print( "(");
a1=print_indiv( buffer, ++buffercounter );
System.out.print( " * ");
break;
case DIV: System.out.print( "(");
a1=print_indiv( buffer, ++buffercounter );
System.out.print( " / ");
break;
}
a2=print_indiv( buffer, a1 );
System.out.print( ")");
return( a2);}
非常感谢你的帮助!
答案 0 :(得分:0)
未经测试,但沿着此行的某些内容应该有效:
/* ... */
switch(buffer[buffercounter]) {
/* .... */
case SIN: System.out.print( "sin(");
a1= ++buffercounter;
break;
/* ... */
}
a2=print_indiv( buffer, a1 );
System.out.print( ")");
return( a2);