# L-10 MCS 507 Wed 13 Sep 2023 : polyrootscall.jl # This script illustrates calling an external program, # via input and output text files. # In this simulation, the external program is julia polyroots.jl. import Printf """ function make_input(dim::Int, name::String="input.txt") Generates a random coefficient vector, of dimension dim, writes the coefficients to file. By default, the name of the file is input.txt. """ function make_input(dim::Int, name::String="input.txt") c = rand(dim, 1) infile = open(name, "w") strn = Printf.@sprintf("%d", length(c)-1) write(infile, strn) write(infile, "\n") for i=1:length(c) strc = Printf.@sprintf("%.16e", c[i]) write(infile, strc) write(infile, "\n") end close(infile) end """ function get_output(name::String="output.txt") Opens the file with the given name and returns the numbers on the file. """ function get_output(name::String="output.txt") result = [] infile = open(name, "r") lines = readlines(infile) for i=1:length(lines) nbr = parse(Complex{Float64}, lines[i]) append!(result, nbr) end return result end """ function main() Generates a random coefficient vector, writes the coefficients to file, calls the root finder, and then reads the roots from file. """ function main() make_input(5) infile = "input.txt" run(pipeline(`cat input.txt`, pipeline(`julia polyroots.jl`, stdout="output.txt"))) #callcmd = `julia polyroots.jl <' input.txt '>' output.txt` #run(callcmd) r = get_output() println("the roots : ") for i=1:length(r) println(r[i]) end end main()