{%%%%%%%%% Copyright (c) 2019 Trevor Blight All rights reserved. %%%%%%%%%
#  // force cpp

   enter an expression, then call gnuplot to plot it

   - just about any pascal expression (or fortran or basic or c expression)
   - assumes gnuplot is installed
}

program plot(input, output);

const
   max =  200;

type
   string = packed array[1..max] of char;
   index  = 1 .. max;
var
   cmd : string;
   i   : index;
   e   : integer;
   c   : char;

#include "clib.inc.pas"

function initcmd( s: packed array[one..len :integer ] of char) : index;
var
   i : index;
begin
   i := 1;
   while i <= len do begin
      cmd[i] := s[i];
      i := i+1;
   end;
   initcmd := i;
end; { init }

begin

   repeat
      repeat
         writeln('enter an expression to plot (? for examples)');
         read(c);
         if c = '?' then begin
            readln;
            writeln('sin(x)          --> plot sin(x)');
            writeln('[-pi:pi] sin(x) --> set range from -pi..+pi');
            writeln('sin(x),cos(x)   --> plot sin(x) and cos(x)');
            writeln('exp(1/(1+x**2)) --> more complicated function');
            writeln('besj0(x)        --> j0 bessel function');
            writeln;
         end;
      until c <> '?';

      i := initcmd('gnuplot --persist -e "plot ');
      cmd[i] := c;
      i := i+1;
      while not eoln do begin
         read(cmd[i]);
         i := i+1;
      end;
      readln;
      cmd[i] := '"';
      cmd[i+1] := chr(0);

      writeln('cmd is ''', cmd, '''');

      e := system(cmd);

      if e <> 0 then begin
         writeln('gnuplot returned error ', e);
      end;
      writeln('plot another function? (y/n)');
      readln(c);
   until not (c in ['Y', 'y']);
end.

{%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end of plot.pas %%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
