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