home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / LEARN.EX < prev    next >
Text File  |  1993-05-18  |  7KB  |  234 lines

  1.     -------------------------------------------------------
  2.     -- An Interactive Program to Help You Learn Euphoria --
  3.     -------------------------------------------------------
  4. include get.e
  5. include graphics.e
  6.  
  7. constant NTRYS = 3
  8.  
  9. procedure get_answer(object correct)
  10.     sequence answer
  11.     atom t
  12.  
  13.     for i = 1 to NTRYS do
  14.         answer = get(0)
  15.         if answer[1] = GET_SUCCESS then
  16.         if compare(answer[2], correct) = 0 then
  17.             puts(1, "Correct!\n\n")
  18.         sound(2000)
  19.         t = time()
  20.         while time() < t+0.1 do
  21.         end while
  22.          sound(0)
  23.             return
  24.          elsif i < NTRYS then
  25.         puts(1, "Try again\n")
  26.         sound(200)
  27.         t = time()
  28.         while time() < t+0.4 do
  29.         end while
  30.         sound(0)
  31.         end if
  32.     else
  33.         puts(1, "syntax error - a Euphoria object is expected\n")
  34.         while getc(0) != '\n' do
  35.         end while
  36.         end if    
  37.     end for
  38.     puts(1, "The correct answer was: ")
  39.     print(1, correct)
  40.     puts(1, '\n')
  41. end procedure
  42.  
  43. procedure part1()
  44. -- evaluating simple expressions
  45.     object x, y
  46.  
  47.     puts(1, "Please evaluate the following Euphoria expressions\n")
  48.     puts(1, "You have 3 guesses - control-c to quit\n\n")
  49.  
  50.     x = rand(10)
  51.     y = rand(10)
  52.     printf(1, "%d + %d\n", {x, y})
  53.     get_answer(x + y)
  54.  
  55.     x = rand(repeat(10, 3))
  56.     y = rand(10)
  57.     print(1, x)
  58.     puts(1, " * ")
  59.     print(1, y)
  60.     puts(1, '\n')
  61.     get_answer(x * y)
  62.  
  63.     x = rand(repeat(10, 4)) - 5
  64.     y = rand(repeat(10, 4)) - 5
  65.     print(1, x)
  66.     puts(1, " > ")
  67.     print(1, y)
  68.     puts(1, '\n')
  69.     get_answer(x > y)    
  70.  
  71.     x = rand(20)
  72.     y = rand(5)
  73.     puts(1, "repeat(")
  74.     print(1, x)
  75.     puts(1, ", ")
  76.     print(1, y)
  77.     puts(1, ")\n")
  78.     get_answer(repeat(x, y))
  79.     
  80.     x = rand(repeat(25, 3)) + 'a'
  81.     y = rand(repeat(25, 2)) + 'a'
  82.     printf(1, "\"%s\" & \"%s\"\n", {x, y})
  83.     get_answer(x & y)
  84.  
  85.     puts(1, "append(")
  86.     print(1, x)
  87.     puts(1, ", ")
  88.     print(1, y)
  89.     puts(1, ")\n")
  90.     get_answer(append(x, y))
  91.     
  92.     puts(1, "what will the value of x be\n")
  93.     puts(1, "after executing the following statements?\n")
  94.     puts(1, "x = ")
  95.     x = rand({10, 10, {10, 10, 10, 10}, 20})
  96.     print(1, x)
  97.     y = rand({20, 20, 20, 20, 20})
  98.     puts(1, "\ny = ")
  99.     print(1, y)
  100.     puts(1, "\nx[3][2..3] = y[4..5]\n")    
  101.     x[3][2..3] = y[4..5]
  102.     get_answer(x)
  103. end procedure
  104.  
  105. procedure test_program(sequence file_name, object correct)
  106. -- test a program
  107.     integer lout
  108.     sequence answer
  109.  
  110.     for i = 1 to NTRYS do
  111.         system("ed " & file_name, 0)
  112.         while 1 do
  113.             system("del ex.err > NUL", 0)
  114.             system("ex " & file_name & " > learn.out", 0)
  115.             lout = open("ex.err", "r")
  116.             if lout = -1 then
  117.             exit
  118.         else
  119.             close(lout)
  120.             system("ed", 0)    
  121.            end if
  122.         end while
  123.         lout = open("learn.out", "r")
  124.         answer = get(lout)
  125.         if answer[1] = GET_SUCCESS then
  126.         if compare(correct, answer[2]) = 0 then
  127.             puts(1, "Congratulations, your program worked!\n")
  128.             return
  129.          end if
  130.         end if
  131.         puts(1, "Sorry, your program is not correct - \n")
  132.         puts(1, "your output is:\n")
  133.         system("type learn.out", 1)
  134.     if i < NTRYS then
  135.         puts(1, "\nTry again ...\n")
  136.     end if
  137.     end for
  138. end procedure
  139.  
  140. integer user_prog
  141.  
  142. procedure comment(sequence line)
  143.     puts(user_prog, "-- " & line & '\n')
  144. end procedure
  145.  
  146. procedure prog_line(sequence line)
  147.     puts(user_prog, line & '\n')
  148. end procedure
  149.  
  150. procedure part2a()
  151. -- programming
  152.     sequence correct
  153.  
  154.     user_prog = open("work1.ex", "w")
  155.     comment("Program #1")
  156.     comment("You are now in the Euphoria editor.")
  157.     comment("Write a program that will print a sequence")
  158.     comment("containing the integers from 1 to 100.")
  159.     comment("Your output should look like:")
  160.     comment("{1, 2, 3, 4,  ..., 99, 100}")
  161.     comment("When you are finished, save your program with Esc s Enter\n")
  162.     close(user_prog)
  163.  
  164.     correct = {}
  165.     for i = 1 to 100 do
  166.     correct = append(correct, i)
  167.     end for
  168.     test_program("work1.ex", correct)
  169. end procedure
  170.  
  171. procedure part2b()
  172.     user_prog = open("work2.ex", "w")
  173.     comment("Program #2")
  174.     comment("You are now in the Euphoria editor.")
  175.     comment("Write a type-function called special that will only allow")
  176.     comment("a variable to be 3, 17, 52 or 99.")
  177.     comment("Remember that type-functions should return non-zero (TRUE) when")
  178.     comment("an object belongs to the type, and 0 (FALSE) when it does not.")
  179.     comment("The loop at the bottom will call this type function to")
  180.     comment("test it.")
  181.     comment("When you are finished, save your program with Esc s Enter\n")
  182.  
  183.     prog_line("type special(integer x)")
  184.     comment("now complete it ...")
  185.     prog_line("\n\n\n")
  186.     comment("code to test your type ...")
  187.     prog_line("sequence good")
  188.     prog_line("good = {}")
  189.     prog_line("for i = -10000 to 10000 do")
  190.     prog_line("    if special(i) then")
  191.     prog_line("         good = append(good, i)")
  192.     prog_line("    end if")
  193.     prog_line("end for")
  194.     prog_line("? good")
  195.     close(user_prog)
  196.     test_program("work2.ex", {3, 17, 52, 99})
  197. end procedure
  198.  
  199. procedure part2c()
  200.     user_prog = open("work3.ex", "w")
  201.     comment("Program #3")
  202.     comment("You are in the Euphoria editor.")
  203.     comment("Write a program that will get() a sequence from")
  204.     comment("a file called \"learn.dat\" in this directory.")
  205.     comment("It will then print the company name with the highest score.")
  206.     comment("The input data is a sequence of 2-element sequences like:")
  207.     comment("{")
  208.     comment(" {\"IBM\", 50},")
  209.     comment(" {\"Microsoft\", 62},")
  210.     comment("    ... etc ...")
  211.     comment("}")
  212.     comment("As you can see the first element is the name and the second is")
  213.     comment("the score.") 
  214.     comment("Print the name using: printf(1, \"\\\"%s\\\"\", {name})")
  215.     comment("Type Esc s Enter to save your file, when you are ready.")
  216.     comment("Hints: Remember to include get.e")
  217.     comment("       Remember that get returns a 2-element sequence:")
  218.     comment("                 {status, object-read}")
  219.     comment("       A single call to get will read in the whole list.")
  220.     comment("       The scores are from 0 to 100.")
  221.     comment("       If you put your code inside a procedure,") 
  222.     comment("           remember to call it!")
  223.     comment("       You shouldn't need more than about 25 lines of code.\n")
  224.     close(user_prog)
  225.     test_program("work3.ex < learn.dat", "Rapid Deployment Software")
  226. end procedure
  227.  
  228. -- comment out any of these to skip them:
  229. part1()   -- quick questions
  230. part2a()  -- program 1
  231. part2b()  -- program 2
  232. part2c()  -- program 3
  233.  
  234.