I wrote a tool for polar functions. It lists values from an input range like that:
0 Grad: (0 RAD|1 RES) 20 Grad: (0.349065850398866 RAD|1.3639702342662 RES) 40 Grad: (0.698131700797732 RAD|1.83909963117728 RES) 60 Grad: (1.0471975511966 RAD|2.73205080756888 RES) 80 Grad: (1.39626340159546 RAD|6.67128181961771 RES) 100 Grad: (1.74532925199433 RAD|4.67128181961771 RES) 120 Grad: (2.0943951023932 RAD|0.732050807568878 RES) 140 Grad: (2.44346095279206 RAD|0.16090036882272 RES) 160 Grad: (2.79252680319093 RAD|0.636029765733797 RES) 180 Grad: (3.14159265358979 RAD|1 RES)
It is based of the function
abs(1 + tan($_[0]));
How can I parse such a function from UserInput (Perl syntax) and assign it to a variable?
I want to avoid changing the Perl script; making the function dynamic instead of static.
Greets and thanks for reading.
EDIT: sorry for quadrupelpost....
Thanks for the help, but the following snippet gives wrong values:
print("Eingabe: Funktion (phi = $t); PERL syntax!: > ");
$iFunktion = <STDIN>;
chop($iFunktion);
print("Eingabe: Grad Start: > ");
$iGradStart = <STDIN>;
chop($iGradStart);
print("Eingabe: Grad End: > ");
$iGradEnd = <STDIN>;
chop($iGradEnd);
print("Eingabe: Schrittweite: > ");
$iSchrittweite = <STDIN>;
chop($iSchrittweite);
print("\nBerechne Funktion von $iGradStart bis $iGradEnd Grad mit einer Schrittweite von $iSchrittweite\n");
for ($i = $iGradStart; $i < $iGradEnd; $i = $i + $iSchrittweite)
{
$flRad = °2rad($i);
#$flResult = &Compute($flRad);
$t = $i;
$flResult = eval($iFunktion);
print("$i Grad: ($flRad RAD|$flResult RES) \n");
}
Input was abs(1 + tan($t));
(additional info, merged from follow-up)
print("Eingabe: Grad Start: > ");
$iGradStart = <STDIN>;
chop($iGradStart);
print("Eingabe: Grad End: > ");
$iGradEnd = <STDIN>;
chop($iGradEnd);
print("Eingabe: Schrittweite: > ");
$iSchrittweite = <STDIN>; chop($iSchrittweite);
print("\nfrom $iGradStart to $iGradEnd Grad with $iSchrittweite\n");
for ($i = $iGradStart; $i <= $iGradEnd; $i = $i + $iSchrittweite)
{
$flRad = °2rad($i);
$flResult = &Compute($flRad);
print("$i Grad: ($flRad RAD|$flResult RES) \n");
}
sub Compute { return abs(1 + tan($_[0])); }