这是一个简单的代码。必须稍微修改以考虑操作的优先级,但我不知道要修复什么。
`
print "\nEnter the expression: ";
chomp($_ = <>);
print "\n";
sub mul {return $1*$2;}
sub div {return $1/$2;}
sub sum {return $1+$2;}
sub dif {return $1-$2;}
while(
s/([+-]?\d+)\s*[*]\s*([+-]?\d+)/mul($1,$2)/e ||
s/([+-]?\d+)\s*[\/]\s*([+-]?\d+)/div($1,$2)/e ||
s/([+-]?\d+)\s*[+]\s*([+-]?\d+)/sum($1,$2)/e ||
s/([+-]?\d+)\s*[-]\s*([+-]?\d+)/dif($1,$2)/e ||
s/\(\s*([+-]?\d+)\s*\)/$1/e
)
{print "$_\n";}
print "\nThe ansver = $_\n";
`
我很高兴能帮上忙,谢谢。