home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 341.img / TCS161S.ZIP / VOTING.PAS < prev    next >
Pascal/Delphi Source File  |  1989-09-21  |  11KB  |  464 lines

  1. {$R-,S-,I-,V-,B-,N-,L- }
  2. {$O+}
  3.  
  4. unit voting;
  5.  
  6.  
  7. {/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\}
  8.  
  9. interface
  10.  
  11. {/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\}
  12.  
  13.  
  14. uses gentypes,gensubs,subs1,subs2,userret,overret1;
  15.  
  16. Procedure votingbooth (getmandatory:boolean);
  17.  
  18.  
  19. {/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\}
  20.  
  21. implementation
  22.  
  23. {/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\}
  24.  
  25.  
  26. Procedure votingbooth (getmandatory:boolean);
  27. VAR curtopic:topicrec;
  28.     curtopicnum:integer;
  29.  
  30.   Function votefn (n:integer):sstr;
  31.   begin
  32.     votefn:='Votefile.'+strr(n)
  33.   end;
  34.  
  35.   Procedure opentopicdir;
  36.   VAR n:integer;
  37.   begin
  38.     assign (tofile,'VOTEDIR');
  39.     reset (tofile);
  40.     if ioresult<>0 then begin
  41.       close (tofile);
  42.       n:=ioresult;
  43.       rewrite (tofile)
  44.     end
  45.   end;
  46.  
  47.   Function numtopics:integer;
  48.   begin
  49.     numtopics:=filesize (tofile)
  50.   end;
  51.  
  52.   Procedure opentopic (n:integer);
  53.   VAR q:integer;
  54.   begin
  55.     curtopicnum:=n;
  56.     close (chfile);
  57.     assign (chfile,votefn(n));
  58.     reset (chfile);
  59.     if ioresult<>0 then begin
  60.       close (chfile);
  61.       q:=ioresult;
  62.       rewrite (chfile)
  63.     end;
  64.     seek (tofile,n-1);
  65.     read (tofile,curtopic)
  66.   end;
  67.  
  68.   Function numchoices:integer;
  69.   begin
  70.     numchoices:=filesize (chfile)
  71.   end;
  72.  
  73.   Procedure writecurtopic;
  74.   begin
  75.     seek (tofile,curtopicnum-1);
  76.     write (tofile,curtopic)
  77.   end;
  78.  
  79.   Procedure listchoices;
  80.   VAR ch:choicerec;
  81.       cnt:integer;
  82.   begin
  83.     writehdr ('Your Choices');
  84.     seek (chfile,0);
  85.     for cnt:=1 to numchoices do
  86.       begin
  87.         read (chfile,ch);
  88.         writeln (cnt:2,'.  ',ch.choice);
  89.         if break then exit
  90.       end
  91.   end;
  92.  
  93.   Function addchoice:integer;
  94.   VAR ch:choicerec;
  95.   begin
  96.     addchoice:=0;
  97.     buflen:=70;
  98.     writestr (^M'Enter new choice: &');
  99.     if length(input)<2 then exit;
  100.     addchoice:=numchoices+1;
  101.     ch.numvoted:=0;
  102.     ch.choice:=input;
  103.     seek (chfile,numchoices);
  104.     write (chfile,ch);
  105.     writelog (20,2,ch.choice)
  106.   end;
  107.  
  108.   Procedure getvote (mandatory:boolean);
  109.   VAR cnt,chn:integer;
  110.       k:char;
  111.       ch:choicerec;
  112.       tmp:lstr;
  113.       a:boolean;
  114.   begin
  115.     if urec.voted[curtopicnum]<>0 then begin
  116.       writeln ('Sorry, can''t vote twice!!');
  117.       exit
  118.     end;
  119.     a:=ulvl>=curtopic.addlevel;
  120.     tmp:='Your choice [?=List';
  121.     if a then tmp:=tmp+', A to add';
  122.     tmp:=tmp+']:';
  123.     repeat
  124.       writestr (tmp);
  125.       if (length(input)=0) or hungupon then exit;
  126.       chn:=valu(input);
  127.       if chn<>0 then begin
  128.         k:=upcase(input[1]);
  129.         if k='?'
  130.           then listchoices
  131.           else if k='A'
  132.             then if A
  133.               then chn:=addchoice
  134.               else writestr ('You may not add choices to this topic!')
  135.       end
  136.     until chn<>0;
  137.     if (chn>numchoices) or (chn<0) then begin
  138.       writeln ('Choice number out of range!');
  139.       exit
  140.     end;
  141.     curtopic.numvoted:=curtopic.numvoted+1;
  142.     writecurtopic;
  143.     seek (chfile,chn-1);
  144.     read (chfile,ch);
  145.     ch.numvoted:=ch.numvoted+1;
  146.     seek (chfile,chn-1);
  147.     write (chfile,ch);
  148.     urec.voted[curtopicnum]:=chn;
  149.     writeurec;
  150.     writeln ('Thanks for voting!')
  151.   end;
  152.  
  153.   Procedure showresults;
  154.   VAR cnt,tpos,n:integer;
  155.       ch:choicerec;
  156.       percent:real;
  157.   begin
  158.     if urec.voted[curtopicnum]=0 then begin
  159.       writeln ('Sorry, you must vote first!');
  160.       exit
  161.     end;
  162.     seek (chfile,0);
  163.     tpos:=1;
  164.     for cnt:=1 to filesize (chfile) do begin
  165.       read (chfile,ch);
  166.       n:=length(ch.choice)+2;
  167.       if n>tpos then tpos:=n
  168.     end;
  169.     writehdr ('The results so far');
  170.     seek (chfile,0);
  171.     for cnt:=1 to numchoices do if not break then begin
  172.       read (chfile,ch);
  173.       tab (ch.choice,tpos);
  174.       writeln (ch.numvoted)
  175.     end;
  176.     if numusers>0
  177.       then percent:=100.0*curtopic.numvoted/numusers
  178.       else percent:=0;
  179.     writeln (^M,percent:0:0,'% of ',numusers,' have voted.')
  180.   end;
  181.  
  182.   Procedure listtopics;
  183.   VAR t:topicrec;
  184.       cnt:integer;
  185.   begin
  186.     writehdr ('Voting Topics');
  187.     seek (tofile,0);
  188.     for cnt:=1 to numtopics do
  189.       if not break then begin
  190.         read (tofile,t);
  191.         writeln (cnt:2,'.  ',t.topicname)
  192.       end
  193.   end;
  194.  
  195.   Procedure addtopic;
  196.   VAR t:topicrec;
  197.       ch:choicerec;
  198.       u:userrec;
  199.       cnt,tpn:integer;
  200.   begin
  201.     if numtopics>=maxtopics then
  202.       begin
  203.         writeln ('No more room to add a topic!');
  204.         exit
  205.       end;
  206.     tpn:=numtopics+1;
  207.     writestr ('Topic name:');
  208.     if length(input)=0 then exit;
  209.     t.topicname:=input;
  210.     t.numvoted:=0;
  211.     writeurec;
  212.     for cnt:=1 to numusers do begin
  213.       seek (ufile,cnt);
  214.       read (ufile,u);
  215.       if u.voted[tpn]<>0
  216.         then
  217.           begin
  218.             u.voted[tpn]:=0;
  219.             seek (ufile,cnt);
  220.             write (ufile,u)
  221.           end
  222.     end;
  223.     readurec;
  224.     writestr (^M'Make all users vote on this topic? *');
  225.     t.mandatory:=yes;
  226.     writestr ('Allow users to add their own choices? *');
  227.     if yes then begin
  228.       writestr ('Level required to add choices? *');
  229.       t.addlevel:=valu(input)
  230.     end else t.addlevel:=maxint;
  231.     seek (tofile,tpn-1);
  232.     write (tofile,t);
  233.     opentopic (tpn);
  234.     writeln (^M^B'Enter choices, blank line to end.');
  235.     cnt:=1;
  236.     repeat
  237.       buflen:=70;
  238.       writestr ('Choice number '+strr(cnt)+': &');
  239.       if length(input)>0 then begin
  240.         cnt:=cnt+1;
  241.         ch.numvoted:=0;
  242.         ch.choice:=input;
  243.         write (chfile,ch)
  244.       end
  245.     until (length(input)=0) or hungupon;
  246.     writeln ('Topic created!');
  247.     writelog (20,3,strr(tpn)+' ('+t.topicname+')')
  248.   end;
  249.  
  250.   Procedure maybeaddtopic;
  251.   begin
  252.     writestr ('Create new topic? *');
  253.     if yes then addtopic
  254.   end;
  255.  
  256.   Procedure selecttopic;
  257.   VAR ch:integer;
  258.   begin
  259.     input:=copy(input,2,255);
  260.     if input='' then input:=' ';
  261.     repeat
  262.       if length(input)=0 then exit;
  263.       ch:=valu(input);
  264.       if ch>numtopics then begin
  265.         ch:=numtopics+1;
  266.         if issysop then maybeaddtopic;
  267.         if numtopics<>ch then exit
  268.       end;
  269.       if (ch<1) or (ch>numtopics) then begin
  270.         if input='?' then listtopics;
  271.         writestr ('Topic number [?=list]:');
  272.         ch:=0
  273.       end
  274.     until (ch>0) or hungupon;
  275.     opentopic (ch)
  276.   end;
  277.  
  278.   Procedure deltopic;
  279.   VAR un,cnt:integer;
  280.       u:userrec;
  281.       f:file;
  282.       t:topicrec;
  283.       tn:lstr;
  284.   begin
  285.     tn:=' topic '+strr(curtopicnum)+' ('+curtopic.topicname+')';
  286.     writestr ('Delete topic '+tn+'? *');
  287.     if not yes then exit;
  288.     writelog (20,1,tn);
  289.     close (chfile);
  290.     erase (chfile);
  291.     cnt:=ioresult;
  292.     for cnt:=curtopicnum to numtopics-1 do begin
  293.       assign (f,votefn(cnt+1));
  294.       rename (f,votefn(cnt));
  295.       un:=ioresult;
  296.       seek (tofile,cnt);
  297.       read (tofile,t);
  298.       seek (tofile,cnt-1);
  299.       write (tofile,t)
  300.     end;
  301.     seek (tofile,numtopics-1);
  302.     truncate (tofile);
  303.     if curtopicnum<numtopics then begin
  304.       writeln ('Adjusting user voting record...');
  305.       writeurec;
  306.       for un:=1 to numusers do begin
  307.         seek (ufile,un);
  308.         read (ufile,u);
  309.         for cnt:=curtopicnum to numtopics do
  310.           u.voted[cnt]:=u.voted[cnt+1];
  311.         seek (ufile,un);
  312.         write (ufile,u)
  313.       end;
  314.       readurec
  315.     end;
  316.     if numtopics>0 then opentopic (1)
  317.   end;
  318.  
  319.   Procedure removechoice;
  320.   VAR n:integer;
  321.       delled,c:choicerec;
  322.       cnt:integer;
  323.       u:userrec;
  324.   begin
  325.     n:=valu(copy(input,2,255));
  326.     if (n<1) or (n>numchoices) then n:=0;
  327.     while n=0 do begin
  328.       writestr (^M'Choice to delete [?=list]:');
  329.       n:=valu(input);
  330.       if n=0
  331.         then if input='?'
  332.           then listchoices
  333.           else exit
  334.     end;
  335.     if (n<1) or (n>numchoices) then exit;
  336.     seek (chfile,n-1);
  337.     read (chfile,delled);
  338.     for cnt:=n to numchoices-1 do begin
  339.       seek (chfile,cnt);
  340.       read (chfile,c);
  341.       seek (chfile,cnt-1);
  342.       write (chfile,c)
  343.     end;
  344.     seek (chfile,numchoices-1);
  345.     truncate (chfile);
  346.     curtopic.numvoted:=curtopic.numvoted-delled.numvoted;
  347.     writecurtopic;
  348.     write (^B^M'Choice deleted; updating user voting records...');
  349.     writeurec;
  350.     for cnt:=1 to numusers do begin
  351.       seek (ufile,cnt);
  352.       read (ufile,u);
  353.       u.voted[curtopicnum]:=0;
  354.       seek (ufile,cnt);
  355.       write (ufile,u)
  356.     end;
  357.     readurec;
  358.     writeln (^B'Done.')
  359.   end;
  360.  
  361.   Procedure nexttopic;
  362.   begin
  363.     if curtopicnum=numtopics
  364.       then writeln ('No more topics!')
  365.       else opentopic (curtopicnum+1)
  366.   end;
  367.  
  368.   Procedure voteonmandatory;
  369.   VAR n:integer;
  370.       t:topicrec;
  371.   begin
  372.     for n:=1 to numtopics do
  373.       if urec.voted[n]=0 then begin
  374.         seek (tofile,n-1);
  375.         read (tofile,t);
  376.         if t.mandatory then begin
  377.           opentopic (n);
  378.           clearbreak;
  379.           nobreak:=true;
  380.           writeln (^M'Mandatory voting topic: ',t.topicname,^M);
  381.           listchoices;
  382.           getvote (true);
  383.           if urec.voted[curtopicnum]<>0 then begin
  384.             writestr (^M'See results? *');
  385.             if yes then showresults
  386.           end
  387.         end
  388.       end
  389.   end;
  390.  
  391.   Procedure sysopvoting;
  392.   VAR q,dum:integer;
  393.   begin
  394.     writelog (19,1,curtopic.topicname);
  395.     repeat
  396.       q:=menu ('Voting sysop','VSYSOP','QACDR');
  397.       if hungupon then exit;
  398.       case q of
  399.         2:addtopic;
  400.         3:dum:=addchoice;
  401.         4:deltopic;
  402.         5:removechoice;
  403.       end
  404.     until (q=1) or hungupon or (numtopics=0)
  405.   end;
  406.  
  407. VAR q:integer;
  408. label exit;
  409. begin
  410.   cursection:=votingsysop;
  411.   opentopicdir;
  412.   repeat
  413.     if numtopics=0 then begin
  414.       if getmandatory then goto exit;
  415.       writeln ('No voting topics right now!');
  416.       if not issysop
  417.         then goto exit
  418.         else
  419.           begin
  420.             writestr ('Make topic #1? *');
  421.             if yes
  422.               then addtopic
  423.               else goto exit
  424.           end
  425.     end
  426.   until (numtopics>0) or hungupon;
  427.   if hungupon then goto exit;
  428.   if getmandatory then begin
  429.     voteonmandatory;
  430.     goto exit
  431.   end;
  432.   opentopic (1);
  433.   writehdr ('The Voting Booths');
  434.   writeln ('Number of topics: ',numtopics);
  435.   repeat
  436.     writeln (^M'Active topic (',curtopicnum,'): ',curtopic.topicname);
  437.     q:=menu ('Voting','VOTING','QS_VLR#*H%@');
  438.     if hungupon then goto exit;
  439.     if q<0
  440.       then
  441.         begin
  442.           q:=-q;
  443.           if q<=numtopics then opentopic (q);
  444.           q:=0
  445.         end
  446.       else
  447.         case q of
  448.           2,8:selecttopic;
  449.           3:nexttopic;
  450.           4:getvote (false);
  451.           5:listchoices;
  452.           6:showresults;
  453.           9:help ('Voting.hlp');
  454.           10:sysopvoting
  455.         end
  456.   until (q=1) or hungupon or (numtopics=0);
  457.   if numtopics=0 then writeln (^B'No voting topics right now!');
  458.   exit:
  459.   close (tofile);
  460.   close (chfile)
  461. end;
  462.  
  463. end.
  464.