home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / ANSWERS.ARC / ANS_148B.PRO < prev    next >
Encoding:
Prolog Source  |  1988-06-21  |  9.4 KB  |  349 lines

  1. /*
  2.    Turbo Prolog 2.0, Answer to second Exercise on page 148.
  3.    
  4.    Copyright (c) 1986, 88 by Borland International, Inc
  5. */
  6.  
  7. /*
  8.  * This program sets up a database of US Senators.  Compound
  9.  *  objects are used in the Senators record whenever logically 
  10.  *  possible.  Data entry of the records is left to the user.
  11.  *
  12.  *  (Note: This program was apparently written by a student who 
  13.  *         peaked ahead in the manual.  However, the important 
  14.  *         thing to look at is how the user-defined domains 
  15.  *         are used in the predicate declarations.)
  16.  */
  17.  
  18. Domains
  19.   fname, lname, 
  20.   state, bill   =  string
  21.   mon           =  symbol
  22.   day, year     =  integer 
  23.   party         =  r ; d
  24.   vote          =  y ; n ; a 
  25.   name          =  n ( lname, fname )
  26.   affil         =  a ( state, party )
  27.   constituency  =  real
  28.   elec_date     =  d ( mon, day, year )
  29.   record        =  r ( bill, vote )
  30.   vote_record   =  record*    % Vote Record is a list of records
  31.   
  32. Database
  33.   senator ( name, affil, constituency, elec_date, vote_record )  
  34.   
  35. /*
  36.  * Example senator data record:
  37.  *   
  38.  *   senator( n("Hows", "Sammy R.") ,
  39.  *            a( "ca", D ) ,
  40.  *            17484 ,
  41.  *            d( "aug", 10, 1982 ) ,
  42.  *            [ r( "SB 98-103", N) ,
  43.  *              r( "SJ 98-104", N) ,
  44.  *              r( "SB 98-105", A) ,
  45.  *              r( "SB 98-106", Y) ,
  46.  *              r( "SB 98-107", N) ,
  47.  *              r( "SJ 98-108", A) ,
  48.  *              r( "SB 98-109", A) ,
  49.  *              r( "SB 98-110", Y) ,
  50.  *              r( "SB 98-111", N) ,
  51.  *              r( "SB 98-112", N)]).
  52.  */             
  53.   
  54. Predicates
  55.   add_a_senator
  56.   add_a_vote
  57.   append ( vote_record, vote_record, vote_record )
  58.   consult_file
  59.   convert_party ( char, party )
  60.   convert_vote ( char, vote )
  61.   get_con ( constituency )
  62.   get_date ( elec_date )
  63.   get_day ( day )
  64.   get_new_vote ( bill, vote )
  65.   get_party ( party )
  66.   get_senator ( name, affil, constituency, elec_date, vote_record )
  67.   get_vote ( vote )
  68.   get_vote_record ( integer, vote_record )
  69.   get_year ( year )
  70.   list_senators
  71.   menu
  72.   number_of_vote_records ( vote_record, integer )
  73.   process_choice ( char )
  74.   process_vote ( vote_record, integer, vote_record )
  75.   read_file
  76.   repeat
  77.   save_file
  78.   write_votes ( vote_record )
  79.   
  80. Clauses
  81. /*
  82.  *  Main Loop
  83.  */
  84.   menu :- 
  85.     repeat ,
  86.         makewindow(2,2,3," Menu ",5,5,12,30), nl ,
  87.         write("Add a new senator\n") ,
  88.         write("Update a senator's record\n") ,
  89.         write("List senators\n") ,
  90.         write("Save new database\n") ,
  91.         write("Consult senator database\n") ,
  92.         write("eXit\n\n") ,
  93.         write("Enter a Choice: ") ,
  94.         readchar(Choice) ,
  95.         upper_lower(Ch, Choice) ,
  96.         removewindow , 
  97.         process_choice(Ch) ,
  98.       Ch = 'X'.
  99.  
  100.   process_choice('A') :- !, add_a_senator.
  101.   process_choice('U') :- !, add_a_vote.
  102.   process_choice('L') :- !, list_senators.
  103.   process_choice('S') :- !, save_file.
  104.   process_choice('C') :- !, consult_file.
  105.   process_choice('X').
  106.  
  107. /*
  108.  * Add a New Senator 
  109.  */
  110.   add_a_senator :-
  111.       makewindow(3,3,4,"New Senator",0,0,25,80) ,
  112.       repeat ,
  113.              clearwindow ,
  114.         field_str(0,2,14,"First Name   :") ,
  115.         field_str(1,2,14,"Last Name    :") ,
  116.         field_str(2,2,14,"State        :") ,
  117.         field_str(3,2,14,"Party        :") ,
  118.         field_str(4,2,14,"Constituency :") ,
  119.         field_str(5,2,14,"Date Elected  ") ,
  120.         field_str(6,5,11,   "Month     :") ,
  121.         field_str(7,5,11,   "Day       :") ,
  122.         field_str(8,5,11,   "Year      :") ,
  123.         field_str(10,2,35,
  124.              "Voting Record (10 bills Maximum):") ,
  125.         cursor(0, 17), readln(Fname) ,
  126.         cursor(1, 17), readln(Lname) ,
  127.         Name = n(Lname, Fname) ,
  128.         cursor(2, 17), readln(State) ,
  129.         get_party(Party) ,
  130.         Afil = a(State, Party) ,
  131.         get_con(Con) ,
  132.         get_date(Date) ,
  133.         cursor(11,4) ,
  134.         write("(Maximum number of 10 votes or <RETURN> to stop." , 
  135.               "  Vote: Y, N or A)") ,
  136.         get_vote_record(10,Record) ,
  137.         assertz( senator( Name, Afil, Con, Date, Record) ) ,
  138.       write("\nDo you wish to add another Senator? (Y/N): ") ,
  139.       readchar(Ans), write(Ans) ,
  140.     upper_lower(Ans, 'n'), ! ,
  141.     removewindow.
  142.  
  143.   get_party(Party) :-  
  144.         repeat ,
  145.         cursor(3, 17), readchar(C) ,
  146.         upper_lower(C1, C), write(C1) ,
  147.       convert_party (C1,Party), !.
  148.       
  149.   convert_party('D', d) :- !.
  150.   convert_party('R', r) :- !.      
  151.   convert_party(_,_) :- beep, fail.      
  152.  
  153.   get_con(Con) :-    
  154.       cursor(4, 17), 
  155.       readreal(Con), !.
  156.   get_con(Con) :-
  157.       beep, get_con(Con).      
  158.       
  159.   get_date(Date) :-
  160.         cursor(6,17) ,
  161.       readln(Month) ,
  162.       get_day(Day) ,
  163.       get_year(Year) ,
  164.       Date = d(Month, Day, Year).
  165.       
  166.   get_day(D) :-      
  167.       cursor(7, 17) , 
  168.       readint(D), !.
  169.   get_day(D) :-
  170.       beep, get_day(D).
  171.         
  172.   get_year(Y) :-            
  173.       cursor(8,17) , 
  174.       readint(Y), !.
  175.   get_year(Y) :-
  176.       beep, get_year(Y).      
  177.       
  178.   get_vote_record(0,[]) :- !.   
  179.   get_vote_record(Num,[H|T]) :- 
  180.        Num1 = Num - 1 ,
  181.        Num_in = abs( Num - 11 ) ,
  182.        cursor(R,_) ,
  183.        R1 = R+1 ,
  184.       cursor(R1,8) ,
  185.        writef("%)  Bill Number:", Num_in) ,
  186.        cursor(R1,25) ,
  187.        readln(Bill) ,
  188.        Bill <> "", ! ,        % Check for a <CR>
  189.     get_vote(Vote) ,
  190.        H = r(Bill, Vote) ,
  191.        get_vote_record(Num1, T).
  192.   get_vote_record(_,[]).       %  This will succeed when a <CR> is hit
  193.  
  194.   get_vote(Vote) :-
  195.         cursor(R,_) ,
  196.         R1 = R - 1 ,
  197.         repeat ,
  198.        cursor(R1,40) ,
  199.        write("Senator Vote: ") ,
  200.        readchar(V) ,
  201.        upper_lower(V1, V) ,
  202.        write(V1) ,
  203.        convert_vote(V1, Vote), !.
  204.  
  205.   convert_vote('Y', y) :- !.
  206.   convert_vote('N', n) :- !.
  207.   convert_vote('A', a) :- !.
  208.   convert_vote(_,_) :- beep, fail.
  209.  
  210. /*
  211.  *  Update a Senator's Vote Record
  212.  */
  213.   add_a_vote :-
  214.         makewindow(3,6,5," Add a Senators Vote ",2,2,21,76) ,
  215.     cursor(2,2) ,
  216.     write("First Name : ") ,
  217.     cursor(3,2) ,
  218.     write("Last Name  : ") ,
  219.     cursor(2,16) ,
  220.     readln(Fname) ,
  221.     cursor(3,16) ,
  222.     readln(Lname) ,
  223.     get_senator(n(Lname, Fname),_,_,_,_) ,    % see if name exists
  224.     repeat ,
  225.           get_senator(n(Lname, Fname),_,_,_,Votes) ,
  226.       number_of_vote_records(Votes, Num) ,
  227.       process_vote(Votes, Num, New_votes) ,
  228.       retract( senator(n(Lname, Fname),A,B,C,Votes) ) ,
  229.       assertz( senator(n(Lname, Fname),A,B,C,New_votes) ) ,
  230.       write("\n\nWould you like to add another " ,
  231.             "vote to this Senator? (Y/N): ") ,
  232.       readchar(Ans), write(Ans), nl ,
  233.       clearwindow ,
  234.     upper_lower('N', Ans) ,
  235.     removewindow, !.
  236.   add_a_vote :- removewindow.    
  237.     
  238.   process_vote([_|T], 10, New_votes) :-
  239.     !, get_new_vote(Bill, Vote) ,
  240.     append(T, [r(Bill,Vote)], New_votes).
  241.   process_vote(Votes, _, New_votes) :-
  242.       get_new_vote(Bill, Vote) ,    
  243.     append(Votes, [r(Bill,Vote)], New_votes).
  244.  
  245.   get_new_vote(Bill, Vote) :-
  246.       cursor(R,_) ,
  247.       R1 = R + 1 ,
  248.       cursor(R1,2) ,
  249.       write("Bill Number: ") ,
  250.       readln(Bill) ,
  251.       R2 = R1 + 1 ,
  252.       repeat ,
  253.         cursor(R2,2) ,
  254.         write("Senator's Vote: ") ,
  255.         readchar(V) ,
  256.         upper_lower(V1, V) ,
  257.         write(V1) ,
  258.       convert_vote(V1, Vote), !.
  259.  
  260.   get_senator(Name, Affil, Constituency, Elec_date, Vote_record) :-
  261.     senator(Name, Affil, Constituency, Elec_date, Vote_record) ,
  262.     ! ; write("Senator Name not found!\n" ,
  263.               "Press a key to continue...") ,
  264.     readchar(_), clearwindow, fail.
  265.  
  266. /*
  267.  *  List Senators
  268.  */
  269.   list_senators :-
  270.       write("Senators Name\tState  Party  Constituency  Date Elected  Voting Record\n" ,
  271.             "=============\t=====  =====  ============  ============  =============\n") ,
  272.         senator(n(L,F), a(S,P), C, d(M,D,Y), R ) ,
  273.         convert_party(P1,P) ,
  274.         str_int(Day, D) ,
  275.         str_int(Year, Y) ,
  276.         concat(M,"/",D1) ,
  277.         concat(D1,Day,D2) ,
  278.         concat(D2,"/",D3) ,
  279.         concat(D3,Year,Date) ,
  280.         concat(F," ",F1),
  281.         concat(F1,L,FL),
  282.       writef("\n%-10 \t %-2      %-1       %-9.0     %-8    ", 
  283.               FL,S,P1,C,Date),
  284.       write_votes(R) ,
  285.       fail.
  286.   list_senators :-      
  287.       write("\nPRESS a key...") ,
  288.       readchar(_) ,
  289.       clearwindow.
  290.  
  291.   write_votes([]) :- !.
  292.   write_votes([r(Bill, Vote)|T]) :-
  293.     convert_vote(V, Vote) ,
  294.       writef("%s-%\n", Bill, V) ,
  295.       cursor(Row,_),
  296.       cursor(Row,59),
  297.       write_votes(T).
  298. /*
  299.  *  Save Senator's Database to Disk
  300.  */
  301.   save_file :-
  302.       write("\nSaving Database...\n") ,
  303.       save("SENATORS.dba"), clearwindow.
  304.  
  305. /*
  306.  * Consult Senator's Databse File
  307.  */
  308.   consult_file :-
  309.       senator(_,_,_,_,_), ! ,
  310.       write("Database already in memory.\n" ,
  311.             " Press any key to continue...") ,
  312.       readchar(_), clearwindow.
  313.   consult_file :-
  314.       existfile("SENATORS.dba"), ! ,
  315.       read_file.
  316.   consult_file :-
  317.       write("Senators Database file not found!\n" ,
  318.             " (You must create and save a database\n" ,
  319.             "   before one can be consulted.)\n\n" ,
  320.             "Press any key to continue...") ,
  321.       readchar(_) ,
  322.       clearwindow.
  323.       
  324.   read_file :-               
  325.     write("\nConsulting Database...\n") ,
  326.       consult("SENATORS.dba"), ! ,
  327.       clearwindow ; beep ,
  328.         write("ERROR in database!\n" ,
  329.               "Press any key to continue...") ,
  330.         readchar(_), clearwindow.
  331.  
  332.  
  333. /*
  334.  *  System Predicates
  335.  */
  336.   repeat :- true;repeat.    
  337.   
  338.   append([],L,L).
  339.   append([H|L1],L2,[H|L3]) :- append(L1,L2,L3).
  340.   
  341.   number_of_vote_records([],0) :- !.
  342.   number_of_vote_records([_|T],N) :-
  343.       number_of_vote_records(T, N1) ,
  344.       N = N1 + 1.
  345.     
  346. GOAL
  347.   makewindow(1,2,3," Senators Database ",0,0,25,80) ,
  348.   menu.  
  349.