// gcd.j prompts for two numbers and returns the greatest common divisor. // It is written in the core join calculus and compiles under v0.4 of cjcc. // To compile and run: cjcc -o gcd.c gcd.j // gcc -o gcd gcd.c // gcd // Author: Peter Selinger, University of Pennsylvania // Copyright (C) 1996 Peter Selinger // This is free software under the terms of the GNU General Public License. def /* main */ main<> --> { def /* exception_handling */ e --> write<"An exception was raised: ", l1, e2> | store and l1<> | store --> write and l2<> --> write<"\n", main, e2> and e2 --> {} in def /* input_two_numbers */ input2 --> write<"Please input two numbers: ",l1,e> | store0 and l1<> --> readint and a --> store1 | readint and b | store1 | store0 --> respond in def /* are_the_numbers_positive */ positive --> less<0,a,pos_a,e> | less<0,b,pos_b,e> | store and pos_a --> if and pos_b --> if and good<> | good<> | store --> yes<> and bad<> | bad<> | store --> no<> and good<> | bad<> | store --> no<> in def /* compute_gcd */ gcd --> store | store0 | equal and eq --> if and case0<> | store | store0 --> result // if a=b, done and case1<> | store --> store | less and ls --> if and case2<> | store | store0 --> gcd // if a | store --> minus | store1 and diff | store1 | store0 --> gcd // do gcd(a-b,b) in def /* start */ start<> --> input2 and two_numbers --> store | positive and yes<> | store --> gcd and result --> write<"The gcd is: ", l1, e> | store1 and l1<> | store1 --> write and l2<> --> write<"\n", stop, e> and stop<> --> {} and no<> | store --> write<"Positive numbers, please.\n", l3, e> and l3<> --> start<> in start<> } in main<>