home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 186_01 / ltri.c < prev    next >
Text File  |  1985-08-21  |  5KB  |  257 lines

  1.            
  2. /********************************************************************
  3.  
  4.  
  5.                  LTRI.C
  6.                                By
  7.               David McCourt
  8.  
  9. *********************************************************************/
  10.  
  11. #include bdscio.h
  12. #define CLEARS "\032"            /* clear screen         */
  13. #define MON 480                /* max months in file           */
  14.  
  15. int date[MON];                /* date of entry:
  16.                         182 = January 1982
  17.                            1282 = December 1982    */
  18.  
  19. char djia [MON] [5];            /* closing Dow Jones Industrial
  20.                        average for the last day the
  21.                        market was open in the given
  22.                        month            */
  23.     
  24. char ind [MON] [5];            /* Long Term risk index        */
  25. char fac[5];                /* Used to multiply index     */
  26. char round[5];                /* Used to round index        */
  27. char next[5];                /* Used to add to weight factor    */
  28. int rec_num,w_o;
  29.  
  30. main()
  31. {
  32.     atof(round,"0.5");
  33.     atof(fac, "10");
  34.     atof(next, "1");
  35.     rec_num = 0;
  36.     w_o = 0;
  37.     menu();
  38. }
  39.  
  40. menu()
  41. {
  42. int x;
  43.     puts(CLEARS);
  44.     puts("\n\tMenu for Long Term Risk Index\n");
  45.     puts("\t==================================================\n\n");
  46.     puts("\t\t0) End session\n");
  47.     puts("\t\t1) Update file\n");
  48.     puts("\t\t2) Load file\n");
  49.     puts("\t\t3) Save file\n");
  50.     puts("\t\t4) Edit \n");
  51.     puts("\t\t5) Print file\n");
  52.     puts("\n\n\n\tEnter your selection:");
  53.     scanf("%d",&x);
  54.     switch(x){
  55.         case 0:
  56.             exit();
  57.         case 1:
  58.             up_date();
  59.             break;
  60.         case 2:
  61.             load();
  62.             break;
  63.         case 3:
  64.             save();
  65.             break;
  66.         case 4:
  67.             edit();
  68.             break;
  69.         case 5:
  70.             list();
  71.             break;
  72.         default:
  73.             menu();
  74.     }
  75.     menu();
  76. }
  77.  
  78.  
  79. /*
  80.     After 35 entries have been made, the program will branch
  81.     to this function to develope the Index
  82. */
  83.  
  84. index(a)
  85. int a;
  86. {
  87. char bld_1 [5];
  88. char bld_2 [5];
  89. char bld_3 [5];
  90. char count [5];
  91.  
  92.     atof(count, "0");
  93.     atof(ind[a],"0");
  94.  
  95.     w_o = a - 10;
  96.     while(w_o < a + 1){
  97.         /* add one to count for weighted average */
  98.         fpadd(count,count,next);
  99.         
  100.         /* subtract 14 month old djia from current djia */
  101.         fpsub(bld_1,djia [w_o], djia [w_o - 14]);
  102.  
  103.         /* divide by 14 month old djia */
  104.         fpdiv(bld_1,bld_1,djia [w_o - 14]);
  105.  
  106.         /* subtract 11 month old djia from current djia */
  107.         fpsub(bld_2,djia [w_o],djia [w_o -11]);
  108.  
  109.         /* divide by 11 month old djia */
  110.         fpdiv(bld_2,bld_2,djia [w_o -11]);
  111.  
  112.         /* add together */
  113.         fpadd(bld_3,bld_1,bld_2);
  114.  
  115.         /* weight the average */
  116.         fpmult(bld_3,bld_3,count);
  117.  
  118.         /* add weighted average to the index */
  119.         fpadd(ind [a],ind [a],bld_3);
  120.  
  121.         ++w_o;
  122.     }
  123.  
  124.     /* multiply index by 10 */    
  125.     fpmult(ind [a],ind[a],fac);
  126.     fpadd(ind[a],ind[a],round);
  127. }
  128.  
  129. hold()
  130. {
  131.     puts("\nPress <RETURN> to continue ...");
  132.     getchar();
  133. }
  134.     
  135.                                                     
  136. up_date()
  137. {
  138. char str[5];
  139. int a;
  140.  
  141.     ++rec_num; 
  142.     puts(CLEARS);
  143.     if(rec_num > 1)
  144.         printf("\t\tLast date entered: %d",date[rec_num -1]);
  145.     printf("\n\n\t\tWorking on record number: %3d",rec_num);
  146.     puts("\n\n\t\tEnter date: ");
  147.     scanf("%d",&date[rec_num]);
  148.     puts("\n\t\tEnter closing DJIA: ");
  149.     atof(djia[rec_num], gets(str));
  150.     if(rec_num > 35)
  151.         a = rec_num;
  152.         index(a);
  153.  
  154. }
  155.  
  156. load()
  157. {
  158. char iobuf[BUFSIZ];
  159. int a,b;
  160.     puts(CLEARS);
  161.     puts("Loading file ...");
  162.     if(fopen("LTRI.FIL",iobuf) ==ERROR){
  163.         puts("\nCan't open LTRI.FIL");
  164.         return(ERROR);
  165.     }
  166.     rec_num = getw(iobuf);
  167.     for(a=0; a < rec_num +1; ++a){
  168.         date[a] = getw(iobuf);
  169.         for(b=0; b < 5; ++b){
  170.             djia[a] [b] = getw(iobuf);
  171.             ind [a] [b] = getw(iobuf);
  172.         }
  173.     }
  174.     fclose(iobuf);
  175. }
  176.  
  177. save()
  178. {
  179. char iobuf[BUFSIZ];
  180. int a,b;
  181.     
  182.     puts(CLEARS);
  183.     puts("Saving file to disk ...");
  184.     if(rec_num == 0){
  185.         puts("\nCAUTION FILE IS EMPTY");
  186.         hold();
  187.     }
  188.     if(fcreat("LTRI.FIL",iobuf) == ERROR){
  189.         puts("\nCan't create LTRI.FIL");
  190.         return(ERROR);
  191.     }
  192.     putw(rec_num,iobuf);
  193.     for(a=0; a < rec_num +1; ++a){
  194.         putw(date[a],iobuf);
  195.         for(b=0; b < 5; ++b){
  196.             putw(djia[a] [b],iobuf);
  197.             putw(ind[a] [b], iobuf);
  198.         }
  199.     }
  200.     fflush(iobuf);
  201.     fclose(iobuf); 
  202. }
  203.  
  204. edit()
  205. {
  206. int a;
  207. char str[5];
  208.  
  209.     puts(CLEARS);
  210.     puts("\n\t\tEnter record number to change: ");
  211.     scanf("%d", &a);
  212.     puts("\n\t\tEnter date: ");
  213.     scanf("%d",date[a]);
  214.     puts("\n\t\tEnter closing DJIA: ");
  215.     atof(djia[a],gets(str));
  216.     if(a > 35){
  217.         puts("\n\n\t\tEditing record number:\n");
  218.         while(a < rec_num +1){
  219.             printf("%4d",a);
  220.             index(a);
  221.             ++ a;
  222.         }
  223.     }
  224. }
  225.  
  226. list()
  227. {
  228. int a,b;
  229.             
  230.     puts(CLEARS);
  231.     puts("\n\n\n\n\n\n\n\n");
  232.     puts("\t\tThe screen will display five years of history.\n");
  233.     puts("\t\tTo see the most current data,  enter a number\n");
  234.     puts("\t\t1 less than the number of records in the file.\n\n"); 
  235.     printf("\t\tThere are  %d  records in the file.\n",rec_num);
  236.     printf("\t\tStart from record number: [   ]\b\b\b\b");
  237.     scanf("%d", &a);
  238.     if(a + 60 > rec_num){
  239.         b = rec_num;
  240.          a = rec_num - 59;
  241.         }
  242.     else 
  243.         b = a + 60;
  244.     puts(CLEARS);
  245.     puts(" Date   DJIA    Index       Date   DJIA    Index");      
  246.     puts("       Date   DJIA    Index\n\n");
  247.     while(a < b){
  248.         printf("%5d  %7.2f   %5.0f     ",date[a],djia[a],ind[a]);
  249.         printf("%5d  %7.2f   %5.0f     ",date[a+1],djia[a+1],ind[a+1]);
  250.         printf("%5d  %7.2f   %5.0f\n",date[a+2],djia[a+2],ind[a+2]);
  251.         a = a + 3;
  252.     }
  253.     hold();
  254. }
  255.  
  256. /* eof ltri.c                            */
  257.