Created
August 11, 2018 15:29
-
-
Save Ansarina/c887ac2f95c8073d48ef72a618a281fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program main | |
use function_parser | |
use iso_fortran_env, only: wp => real64, output_unit | |
implicit none | |
character(len=50):: func ='sin(x)' | |
integer,parameter :: nvar = 1 !! number of variables | |
character(len=*),dimension(nvar),parameter :: var = [ 'x' ] | |
real(wp),dimension(nvar) :: val = [ 2.0_wp ] | |
type(fparser) :: parser | |
real(wp) :: res | |
real(wp) :: x | |
real:: A | |
write(*,*) 'Please enter your function f(x):' | |
read(*,*) func | |
write(*,*) 'Please enter value for x:' | |
read(*,*) A | |
val = [A] | |
call parser%parse(func, var, .false.) ! parse and bytecompile function string | |
if (parser%error()) then | |
call parser%print_errors(output_unit) | |
else | |
call parser%evaluate(val,res) ! interprete bytecode representation of function | |
if (parser%error()) then | |
call parser%print_errors(output_unit) | |
else | |
write(*,*) 'f(x)=',func | |
write(*,'(/X,A2,F5.2,A2,F15.8/)') 'f(',A,')=',res | |
end if | |
end if | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment