home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / graphdem.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  165 lines

  1. ############################################################################
  2. #
  3. #    File:     graphdem.icn
  4. #
  5. #    Subject:  Program to demonstrate simple bar graphics
  6. #
  7. #    Author:   Matthias Heesch
  8. #
  9. #    Date:     November 19, 1997
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  graph.icn: simple bar graphics package with two demo applications:
  18. #    1. display the 4 most frequently used characters in a string.
  19. #    2. display the fibonacci numbers
  20. #
  21. ############################################################################
  22. #
  23. #  Requires: ANSI terminal support
  24. #
  25. ############################################################################
  26.  
  27. procedure main()
  28.     local option
  29.  
  30.     write("graph: simple bar graphics package for icon")
  31.     write("(b)yte frequency count or (f)ibonacci's numbers?")
  32.     option := read()
  33.     case option of {
  34. "b"     : countdemo()
  35. "f"     : fibodemo()
  36. default : write("erroneous option")
  37.     }
  38. end
  39. #
  40. procedure countdemo()
  41.     local numlist, line, a, ms, b
  42.  
  43.     numlist := list(0)
  44.     write("type strings or quit using end-of-file")
  45.     while line := read() do {
  46.     a := frequ_count(line,4)
  47.     ms := a
  48.     a ? {
  49.         while b := tab(upto(";")) do {
  50.         b ? {
  51.             tab(upto(","))
  52.             move(1)
  53.             b := tab(0)
  54.         }
  55.         move(1)
  56.         put(numlist,b)
  57.         }
  58.     }
  59.     graph(numlist,("the most frequently used characters: " || ms))
  60.     }
  61. end
  62. #
  63. procedure frequ_count(lin,item_number)
  64.     local result, n, byte_frequency_1, byte_frequency_2, byte, entry
  65.  
  66.     result := ""
  67.     n := 1
  68.     byte_frequency_1 := table(0)
  69.     every byte := !lin do {
  70.         byte_frequency_1[byte] +:= 1
  71.     }
  72.     byte_frequency_2 := sort(byte_frequency_1,2)
  73.     while n <= item_number do {
  74.     entry := pull(byte_frequency_2)
  75.     result := result || pop(entry) || "," || pull(entry) || ";"
  76.     n +:= 1
  77.     }
  78. return result
  79. end
  80. #
  81. # fibodemo(): calls user defined function fibo(n,m): fibodemo() will
  82. # use an ansi escape code to clear the screen after every call to
  83. # graph. therefore when using ms/dr dos the config.sys file should
  84. # contain: device=ansi.sys. using other operating systems, the line
  85. # containing the esc-code should be deleted.
  86. procedure fibodemo()
  87.     local a, l, b, fb
  88.  
  89.     while every a := fibo(0,1) & a < 10000 do {
  90.     l := list(4,0)
  91. # delete the following line if you don't use ms/dr dos
  92.     write(char(27),"[2J")
  93.     l[1] := a
  94.     graph(l,("fibo: " || a || ".   <enter> to continue"))
  95.     b := read()
  96.     }
  97. end
  98. #
  99. procedure fibo(m,n)
  100.     local fb
  101.  
  102.     while n < 30000 do {
  103.     fb := m + n
  104.     m := n
  105.     n := fb
  106.     suspend fb
  107.     }
  108. end
  109. #
  110. # graph(numbers,comment): bar graphics function which accepts a list
  111. # of 4 integers 10000 and a commentary message. it will display 4
  112. # bar graphic diagrams which each contains a diagram of one of the
  113. # argument values. in the order of the decimal system, the left bar
  114. # shows the 1000s, the following the 100s etc. Therefore the values
  115. # have to be <10000. When the diagram has been displayed argument
  116. # comment will be written to the screen.
  117. procedure graph(numbers,comment)
  118.     local item, itm, value, bar, graph_line, l, m, n, nn
  119.  
  120. # item2 is a list which contains lists of each 4 strings. these strings
  121. # correspond to the numerical values in the lists contained in list
  122. # numbers. each of these strings contains repl(" ",(10-numerical_value))
  123. # || repl("\334",numerical_value).
  124. #
  125. # create item2 with its string contents
  126.     item := list(0)
  127.      while itm := pop(numbers) do {
  128. # write every place of itm if there are less then 4 places.
  129.         if *itm < 4 then itm := repl("0",(4 - *itm)) || itm
  130. # convert every place of itm to a "\334 "-string and assign it
  131. # to list item
  132.         while every value := !itm do {
  133.           bar := repl(" ",(10 - value)) || repl("\334",value)
  134.           put(item,bar)
  135.         }
  136.     }
  137. # display bar graphic
  138.     graph_line := ""
  139.     l := 1
  140.     m := 1
  141.     n := 1
  142.     nn := 10
  143.     while n <= 10 do {
  144.       while m <= 16 do {
  145.       while l <= 4 do {
  146.         graph_line := graph_line || " " || !item[m]
  147.         item[m][1] := ""
  148.         l +:= 1
  149.         m +:= 1
  150.       }
  151.       graph_line := graph_line || "  \272 "
  152.       l := 1
  153.     }
  154.       write(graph_line,"    ",nn)
  155.       graph_line := ""
  156.       l := 1
  157.       m := 1
  158.       n +:= 1
  159.       nn -:= 1
  160.     }
  161.     write(" a b c d")
  162.     write("a: 1000, b: 100, c: 10, d: 1")
  163.     write(comment)
  164. end
  165.