home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP14.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  14.4 KB  |  323 lines

  1.                      Chapter 14 - Example Programs
  2.  
  3.  
  4.                              WHY THIS CHAPTER?
  5.  
  6.              Although  every  program  in this tutorial has  been  a 
  7.  
  8.         complete  program,  each  one  has also been  a  very  small 
  9.  
  10.         program intended to teach you some principle of  programming 
  11.  
  12.         in  C.   It  would do you a disservice to leave you at  that 
  13.  
  14.         point  without introducing you to a few larger  programs  to 
  15.  
  16.         illustrate  how  to  put together the  constructs  you  have 
  17.  
  18.         learned  to create a major program.   This chapter  contains 
  19.  
  20.         four  programs  of increasing complexity,  each designed  to 
  21.  
  22.         take  you  into a higher plateau of  programming,  and  each 
  23.  
  24.         designed to be useful to you in some way.
  25.  
  26.              DOSEX will illustrate how to make DOS system calls  and 
  27.  
  28.         will teach you,  through self-study, how the system responds 
  29.  
  30.         to  the  keyboard.   WHATNEXT  reads commands input  on  the 
  31.  
  32.         command line and will aid you in setting up a variable batch 
  33.  
  34.         file,  one  that requests an operator input and responds  to 
  35.  
  36.         the  input  by branching to a different part  of  the  batch 
  37.  
  38.         file.
  39.  
  40.              LIST  is  the source code for the program you  used  to 
  41.  
  42.         print  out the C source files when you began studying C with 
  43.  
  44.         the aid of this tutorial.  Finally we come to VC, the Visual 
  45.  
  46.         Calculator,  which  you should find to be a  useful  program 
  47.  
  48.         even  if you don't study its source code.   VC uses most  of 
  49.  
  50.         the  programming  techniques we have studied in this  course 
  51.  
  52.         and  a few that we never even mentioned such  as  separately 
  53.  
  54.         compiled subroutines.
  55.  
  56.              We  will  take a look at the example programs one at  a 
  57.  
  58.         time  but  without  a complete explanation of  any  of  them 
  59.  
  60.         because  you  have  been studying C for some  time  now  and 
  61.  
  62.         should be able to read and understand most of these programs 
  63.  
  64.         on  your  own.   One other thing must  be  mentioned,  these 
  65.  
  66.         programs  use  lots of nonstandard constructs and  you  will 
  67.  
  68.         probably need to modify some of them to get them to  compile 
  69.  
  70.         with  your  particular  compiler.  That will be left  as  an 
  71.  
  72.         exercise for you.
  73.  
  74.                      DOSEX.C - The DOS Example Program
  75.  
  76.              The  copy of DOS that you received with your IBM-PC  or 
  77.  
  78.         compatible has about 50 internal DOS calls that you can  use 
  79.  
  80.         as  a programmer to control your peripheral devices and read 
  81.  
  82.         information  or status from them.   Some of the earlier  IBM 
  83.  
  84.         DOS manuals, DOS 2.0 and earlier, have these calls listed in 
  85.  
  86.         the back of the manual along with how to use them.   Most of 
  87.  
  88.         the  manuals  supplied  with compatible  computers  make  no 
  89.  
  90.         mention  of  these  calls even  though  they  are  extremely 
  91.  
  92.         useful.   These  calls  can  be  accessed  from  nearly  any 
  93.  
  94.  
  95.  
  96.                                   Page 94
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.                      Chapter 14 - Example Programs
  107.  
  108.  
  109.         programming  language but they do require some initial study 
  110.  
  111.         to learn how to use them.   This program is intended to  aid 
  112.  
  113.         you in this study.
  114.  
  115.              Display the program on your monitor or print it out for 
  116.  
  117.         reference.   It  is  merely a loop watching for  a  keyboard 
  118.  
  119.         input or a change in the time.  If either happens, it reacts 
  120.  
  121.         accordingly.   In line 23,  the function "kbhit()" returns a 
  122.  
  123.         value  of 1 if a key has been hit but not yet read from  the 
  124.  
  125.         input buffer by the program.  This is a nonstandard function 
  126.  
  127.         and  may require a name change for your particular compiler.  
  128.  
  129.         There will probably be several similar calls that will  need 
  130.  
  131.         changed  for  your compiler in order to compile and run  the 
  132.  
  133.         programs in chapter 14.
  134.  
  135.              Look at the function named "get_time" for an example of 
  136.  
  137.         a  DOS call.   An interrupt 21(hex) is called after  setting 
  138.  
  139.         the  AH  register to 2C(hex) =  44(decimal).   The  time  is 
  140.  
  141.         returned in the CH,  CL, and DH registers.  Refer to the DOS 
  142.  
  143.         call  definitions in your copy of DOS.   If the  definitions 
  144.  
  145.         are  not included there,  Peter Nortons  book,  "Programmers 
  146.  
  147.         Guide  to  the  IBM PC" is recommended as a  good  reference 
  148.  
  149.         manual   for   these  calls  and  many   other   programming 
  150.  
  151.         techniques.
  152.  
  153.              Another useful function is the "pos_cursor()"  function 
  154.  
  155.         that  positions the cursor anywhere on the monitor that  you 
  156.  
  157.         desire  by  using  a  DOS  interrupt.   In  this  case,  the 
  158.  
  159.         interrupt  used  is  10(hex) which is  the  general  monitor 
  160.  
  161.         interrupt.   This particular service is number 2 of about 10 
  162.  
  163.         different  monitor  services  available.    This  particular 
  164.  
  165.         function  may  not be needed by your compiler  because  some 
  166.  
  167.         compilers  have a cursor positioning function predefined for 
  168.  
  169.         your use.  This function is included here as another example 
  170.  
  171.         to you.
  172.  
  173.              The  next  function,  service  number  6  of  interrupt 
  174.  
  175.         10(hex)  is the window scroll service.   It should  be  self 
  176.  
  177.         explanatory.
  178.  
  179.              In this program, the cursor is positioned and some data 
  180.  
  181.         is  output  to the monitor,  then the cursor is "hidden"  by 
  182.  
  183.         moving  it  to line 26 which is not  displayed.   After  you 
  184.  
  185.         compile and run the program, you will notice that the cursor 
  186.  
  187.         is  not  visible on the monitor.   This is possible  in  any 
  188.  
  189.         program,  but  be  sure  to put the cursor  in  view  before 
  190.  
  191.         returning  to  DOS  because  DOS does not  like  to  have  a 
  192.  
  193.         "hidden" cursor and may do some strange things. 
  194.  
  195.              Some time spent studying this program will be  valuable 
  196.  
  197.         to  you as it will reveal how the keyboard data is input  to 
  198.  
  199.  
  200.  
  201.                                   Page 95
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.                      Chapter 14 - Example Programs
  212.  
  213.  
  214.         the  computer.   Especially of importance is how the special 
  215.  
  216.         keys such as function keys, arrows, etc. are handled.
  217.  
  218.                   WHATNEXT.C - The Batch File Interrogator
  219.  
  220.              This  is  an  example of how to read the  data  on  the 
  221.  
  222.         command line following the function call.  Notice that there 
  223.  
  224.         are  two variables listed within the  parentheses  following 
  225.  
  226.         the main() call.   The first variable is a count of words in 
  227.  
  228.         the entire command line including the command itself and the 
  229.  
  230.         second  variable  is  a  pointer to  an  array  of  pointers 
  231.  
  232.         defining the actual words on the command line.
  233.  
  234.              First the question on the command line, made up of some 
  235.  
  236.         number of words, is displayed on the monitor and the program 
  237.  
  238.         waits for the operator to hit a key.   If the key hit is one 
  239.  
  240.         of  those  in the last "word" of the group of words  on  the 
  241.  
  242.         command  line,  the number of the character within the group 
  243.  
  244.         is  returned to the program where it can be tested with  the 
  245.  
  246.         "errorlevel" command in the batch file.   You could use this 
  247.  
  248.         technique  to  create a variable AUTOEXEC.BAT  file  or  any 
  249.  
  250.         other  batch  file  can  use this for  a  many  way  branch.  
  251.  
  252.         Compile  and  run this file with TEST.BAT for an example  of 
  253.  
  254.         how  it  works in practice.   You may  find  this  technique 
  255.  
  256.         useful  in  one  of  your batch files and  you  will  almost 
  257.  
  258.         certainly  need  to  read in  the  command  line  parameters 
  259.  
  260.         someday.
  261.  
  262.              An  interesting alternative would be for you to write a 
  263.  
  264.         program  named "WOULD.C" that would return a 1 if a  "Y"  or 
  265.  
  266.         "y"  were typed and a zero if any other key were hit.   Then 
  267.  
  268.         your batch file could have a line such as;
  269.  
  270.         WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  271.  
  272.              Dos would use "WOULD" as the program name,  ignore  the 
  273.  
  274.         rest  of  the  statement  except for displaying  it  on  the 
  275.  
  276.         screen.   You  would  then respond to the  question  on  the 
  277.  
  278.         monitor  with a single keyhit.   Your batch file would  then 
  279.  
  280.         respond   to  the  1  or  0  returned  and  either  run  the 
  281.  
  282.         alternative  part  of  the batch file or  the  primary  part 
  283.  
  284.         whatever each part was.
  285.  
  286.         WOULD YOU LIKE PRIMARY (Y/N)
  287.         IF ERRORLEVEL 1 GOTO PRIMARY
  288.         (secondary commands)
  289.         GOTO DONE
  290.         :PRIMARY
  291.         (primary commands)
  292.         :DONE
  293.  
  294.  
  295.  
  296.  
  297.                                   Page 96
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.                      Chapter 14 - Example Programs
  308.  
  309.  
  310.                         LIST.C - The Program Lister
  311.  
  312.              This program is actually composed of two files,  LIST.C 
  313.  
  314.         and  LISTF.C  that must be separately  compiled  and  linked 
  315.  
  316.         together  with your linker.   There is nothing new here  and 
  317.  
  318.         you  should  have  no  trouble compiling  and  linking  this 
  319.  
  320.         program  by  reading the documentation  supplied  with  your 
  321.  
  322.         compiler.
  323.  
  324.              The  only  thing  that is new in this  program  is  the 
  325.  
  326.         inclusion   of  three  "extern"  variables  in  the  LISTF.C 
  327.  
  328.         listing.   The only purpose for this is to tie these  global 
  329.  
  330.         variables  to  the main program and tell the  compiler  that 
  331.  
  332.         these  are not new variables.   The compiler will  therefore 
  333.  
  334.         not  generate any new storage space for them but simply  use 
  335.  
  336.         their names during the compile process.   At link time,  the 
  337.  
  338.         linker  will  get  their actual storage locations  from  the 
  339.  
  340.         LIST.OBJ  file and use those locations for the variables  in 
  341.  
  342.         the  LISTF part of the memory map also.   The  variables  of 
  343.  
  344.         those  names in both files are therefore the same  identical 
  345.  
  346.         variables and can be used just as any other global variables 
  347.  
  348.         could be used if both parts of the program were in one file.
  349.  
  350.              There is no reason why the variables couldn't have been 
  351.  
  352.         defined  in the LISTF.C part of the program and declared  as 
  353.  
  354.         "extern"  in the LIST.C part.   Some of the variables  could 
  355.  
  356.         have  been  defined  in one and some in the  other.   It  is 
  357.  
  358.         merely a matter of personal taste.   Carried to an  extreme, 
  359.  
  360.         all of the variables could have been defined in a third file 
  361.  
  362.         and  named "extern" in both of these files.   The third file 
  363.  
  364.         would then be compiled and included in the linking process.
  365.  
  366.              It would be to your advantage to compile, link, and run 
  367.  
  368.         this  program to prepare you for the next program  which  is 
  369.  
  370.         composed of 5 separate files which must all work together.
  371.  
  372.                         VC.C - The Visual Calculator
  373.  
  374.              This  program  finally ties nearly everything  together 
  375.  
  376.         because  it uses nearly every concept covered in the  entire 
  377.  
  378.         tutorial.   It  is so big that I will not even try to  cover 
  379.  
  380.         the finer points of its operation.   Only a few of the  more 
  381.  
  382.         important points will be discussed.
  383.  
  384.              The  first  thing  you  should do  is  go  through  the 
  385.  
  386.         tutorial  for  VC included in the file  VC.DOC.   There  are 
  387.  
  388.         several  dozen  steps  for you to execute,  with  each  step 
  389.  
  390.         illustrating some aspect of the Visual Calculator.  You will 
  391.  
  392.         get  a  good feel for what it is capable of doing  and  make 
  393.  
  394.         your study of the source code very profitable.  In addition, 
  395.  
  396.         you  will  probably  find  many  ways  to  use  the   Visual 
  397.  
  398.  
  399.  
  400.                                   Page 97
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.                      Chapter 14 - Example Programs
  411.  
  412.  
  413.         Calculator  to  solve problems involving calculations  where 
  414.  
  415.         the  simplicity  of  the problem at hand  does  not  warrant 
  416.  
  417.         writing a program.
  418.  
  419.              Notice that the structure definitions,  used in all  of 
  420.  
  421.         the  separate parts of the program,  are defined in the file 
  422.  
  423.         STRUCT.DEF.   During  program development,  when  it  became 
  424.  
  425.         necessary  to change one of the structures slightly,  it was 
  426.  
  427.         not  necessary to change it in all of the  files,  only  one 
  428.  
  429.         file  required modification which was then "included" in the 
  430.  
  431.         source files.   Notice that the transcript data is stored in 
  432.  
  433.         a doubly linked list with the data itself being stored in  a 
  434.  
  435.         separate  dynamically allocated char string.   This line  is 
  436.  
  437.         pointed to by the pointer "lineloc".
  438.  
  439.              For  ease  of development,  the similar functions  were 
  440.  
  441.         grouped together and compiled separately.   Thus, all of the 
  442.  
  443.         functions  involving the monitor were included in  the  file 
  444.  
  445.         named  VIDEO.C,  and all of the functions involving the data 
  446.  
  447.         storage were grouped into the FILE.C  collection.   Dividing 
  448.  
  449.         your  program  in  a  way similar to  this  should  simplify 
  450.  
  451.         debugging and future modifications. 
  452.  
  453.              Of special interest is the "monitor()" function.   This 
  454.  
  455.         function  examines  the  video mode through  use  of  a  DOS 
  456.  
  457.         command  and  if it is a 7,  it assumes it is  a  monochrome 
  458.  
  459.         monitor,  otherwise it assumes a color monitor.   The colors 
  460.  
  461.         of  the various fields are established at this time and used 
  462.  
  463.         throughout  the  program.   Most  of  the  data  is  written 
  464.  
  465.         directly  to the video memory,  but some is written  through 
  466.  
  467.         the standard BIOS routines.
  468.  
  469.              The file DEFIN.C is simply a catalogue of the functions 
  470.  
  471.         to aid in finding the functions.  This file was generated as 
  472.  
  473.         one  of the first files and was maintained and  updated  for 
  474.  
  475.         use during the entire design and coding lifetime.
  476.  
  477.              Feel free,  after understanding this code, to modify it 
  478.  
  479.         in any way you desire for your own use.
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.                                   Page 98
  495.  
  496.