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

  1.                       
  2. /***********************************************************
  3.  
  4.         Automatic Investment Management
  5.                  by
  6.                David McCourt
  7. ************************************************************/
  8.  
  9. #include bdscio.h
  10. #define CLEARS "\032"        /* clear screen code */
  11. #define MAX_ISS 15         /* number of stocks or mutual funds */
  12. #define MAX_LET 11        /* number of letters in name        */
  13. char name[MAX_ISS] [MAX_LET];    /* name of stock or fund */
  14. char shares[MAX_ISS] [5];    /* no of shares owned */
  15. char price[MAX_ISS] [5];    /* current price */
  16. char cost[MAX_ISS] [5];        /* total invested in stock/fund
  17.                    when shares are sold as directed
  18.                    by AIM, the total cost is reduced
  19.                    by the amount of the sale
  20.                 */
  21. char value[MAX_ISS] [5];    /* current total value */
  22. char control[MAX_ISS] [5];    /* used for buy/sell decisions */
  23. char aim_rec[MAX_ISS] [5];    /* the buy sell amount */
  24. char bs[MAX_ISS] [5];        /* 'buy' or 'sell' instruction */
  25. char str[10];            /* used for input to floating point */
  26. char m1[5];            /* used to compute control */
  27. char m2[5];
  28. char m4[5];
  29. char tot_inv[5];        /* used to compute various totals */
  30. int x, iss_num, w_o;        /* iss_num = count of issues in file
  31.                    w_o = issue currently being worked on
  32.                    x = transaction code
  33.                 */
  34.  
  35. main()
  36. {
  37.     iss_num = -1;
  38.     atof(m1, "1.1");
  39.     atof(m2, ".5");
  40.     atof(m4, ".1");
  41.     menu();
  42.  
  43. }
  44.  
  45. menu()
  46. {
  47.  
  48.     x = 0;
  49.     puts(CLEARS);            
  50.     puts("\n\t\tMenu for Automatic Investment Management\n");
  51.     puts("\t\t========================================\n\n");
  52.     puts("\t\t\t 0) End session\n");
  53.     puts("\t\t\t 1) Original investment\n");
  54.     puts("\t\t\t 2) Dividends received as stock\n");
  55.     puts("\t\t\t 3) Additional cash invested\n");
  56.     puts("\t\t\t 4) AIM Market order - SELL \n");
  57.     puts("\t\t\t 5) AIM Market order - BUY\n");
  58.     puts("\t\t\t 6) Review portfolio\n");
  59.     puts("\t\t\t 7) Save file\n");
  60.     puts("\t\t\t 8) Read file\n");
  61.     puts("\t\t\t 9) Delete issue\n");
  62.     puts("\t\t\t10) Update Prices\n");
  63.     puts("\t\t\t11) Money Market original investment\n");
  64.     puts("\t\t\t12) Money Market deposits, withdrawal\n");
  65.         puts("\t\t\t    and interest received\n");
  66.     printf("\n\n\t\tEnter your choice: ");
  67.     
  68.     scanf("%d",&x);
  69.  
  70.     switch(x){
  71.         case 0:
  72.             exit();
  73.             break;
  74.         case 1:
  75.             org_pur();
  76.             shs_pur();
  77.             break;
  78.         case 2:
  79.             list();
  80.             sel_iss();
  81.             shs_pur();
  82.             break;
  83.         case 3:
  84.             new_add();
  85.             break;
  86.         case 4:
  87.             list();
  88.             sel_iss();
  89.             shs_pur();
  90.             break;
  91.         case 5:
  92.             list();
  93.             sel_iss();
  94.             shs_pur();
  95.             break;
  96.         case 6:
  97.             ind_rep();
  98.             break;
  99.         case 7:
  100.             save();
  101.             break;
  102.         case 8:
  103.             load();
  104.             break;
  105.         case 9:
  106.             delete();
  107.             break;
  108.         case 10:
  109.             list();
  110.             sel_iss();
  111.             up_date();
  112.             break;
  113.         case 11:
  114.             org_pur();
  115.             dep_mon();
  116.             break;
  117.         case 12:
  118.             list();
  119.             sel_iss();
  120.             dep_mon();
  121.             break;
  122.         default:
  123.             menu();
  124.     }
  125.     menu();
  126. }
  127.  
  128. dep_mon()
  129. {
  130. char add_cash[5];
  131.  
  132.     puts(CLEARS);
  133.     printf("\n\t\tWorking on: %s",name[w_o]);
  134.     puts("\n\n\t\tEnter the amount of the deposit:  ");
  135.     atof(add_cash, gets(str));
  136.     atof(price[w_o], "1");    /* set price per share to $1 */
  137.     fpadd(cost[w_o],cost[w_o],add_cash);
  138.     fpadd(control[w_o],control[w_o],add_cash);
  139.     fpadd(shares[w_o],shares[w_o],add_cash);
  140.     fpadd(value[w_o],value[w_o],add_cash);
  141.  
  142. }
  143.  
  144. org_pur()
  145. {
  146. int i;
  147.  
  148.     ++iss_num;
  149.     w_o = iss_num;
  150.     puts(CLEARS);
  151.     puts("\n\n\tEqual amounts of money should be deposited in\n");
  152.     puts("\tstock or mutual funds and money market funds.\n");
  153.     puts("\n\tEnter name of stock or fund:\n");
  154.     puts("\tFill all blanks with spaces.\n");
  155.     puts("\tThen press space bar to continue..\n");
  156.     puts("\n\n\t[..........]\b\b\b\b\b\b\b\b\b\b\b");
  157.     for(i=0;(name[iss_num][i] = getchar())!='\n' && i<MAX_LET;++i);
  158.     name[iss_num] [10] ='\0';
  159. }
  160. shs_pur()
  161. {
  162. char add_shs[5];
  163. char new_con[5];
  164. char add_cash[5];
  165.     puts(CLEARS);
  166.     printf("\nWorking on: %s", name[w_o]);
  167.     puts("\n======================\n\n");
  168.     puts("(enter all negative figures with a minus)\n\n");
  169.     puts("\nEnter number of shares bought/sold....\t");
  170.     atof(add_shs, gets(str));
  171.     puts("Enter current price per share..........\t");
  172.     atof(price[w_o], gets(str));
  173.     fpmult(tot_inv,add_shs,price[w_o]);
  174.     printf("\nThis transaction = %10.3f", tot_inv);
  175.     fpadd(shares[w_o],shares[w_o],add_shs);
  176.     fpmult(value[w_o],price[w_o],shares[w_o]);
  177.     if(x==1){ 
  178.         fpmult(control[w_o],price[w_o],shares[w_o]);
  179.         fpmult(cost[w_o],price[w_o],shares[w_o]);
  180.     }
  181.     if(x==3){
  182.         fpmult(new_con,tot_inv,m1);
  183.         fpadd(control[w_o],control[w_o],new_con);
  184.         fpadd(cost[w_o],cost[w_o],tot_inv);
  185.     }
  186.     if(x==4){
  187.         fpadd(cost[w_o],cost[w_o],tot_inv);
  188.     }
  189.     if(x==5){
  190.         fpmult(new_con,tot_inv,m2);
  191.         fpadd(control[w_o],control[w_o],new_con);            fpadd(cost[w_o],cost[w_o],tot_inv);
  192.     }
  193.     hold();
  194. }
  195.  
  196. hold()
  197. {
  198.     puts("\n\nPress <RETURN> to continue...");
  199.     getchar();
  200. }
  201.  
  202. list()
  203. {
  204. int i;
  205.     puts(CLEARS);
  206.     i = 0;
  207.     printf("\n\n\t\tNumber\t\tName\n");
  208.     while(i != iss_num+1){
  209.         printf("\n\t\t%6d\t\t%s",i,name[i]);
  210.     ++i;
  211.     }
  212. }
  213. ind_rep()
  214. {
  215. char p_l[5];
  216. char ave_cost[5];
  217. char port_val[5];
  218.  
  219.     atof(port_val,"0");
  220.     atof(p_l, "0");
  221.     puts(CLEARS);
  222. puts("Name of    Shares    Cur       Cur       Ave       Control    Buy/Sell\n");
  223. puts("Issue      Owned     Price     Value     Cost      Amount     Advice\n");
  224. puts("========== ========= ========  ========= ========  ========== =============");
  225. puts("\n\n");
  226.     for(w_o=0; w_o < iss_num +1; ++w_o){
  227.         aim();
  228.         fpsub(p_l, value[w_o], cost[w_o]);
  229.         fpdiv(ave_cost,cost[w_o],shares[w_o]);
  230.         printf("%s",name[w_o]);
  231.         printf("%10.3f",shares[w_o]);
  232.         printf("%9.3f",price[w_o]);
  233.         printf("%11.3f",value[w_o]);
  234.         printf("%9.3f",ave_cost);
  235.         printf("%11.3f",control[w_o]);
  236.         printf("%10.2f %s\n",aim_rec[w_o],bs[w_o]);
  237.         fpadd(port_val,port_val,value[w_o]);
  238.     }
  239. puts("\n\n");
  240. puts("===========================================================================");
  241.     printf("\nPorfolio  Value: %11.3f",port_val);
  242.     hold();
  243. }
  244. sel_iss()
  245. {
  246.     puts("\n\nSelect issue number:  ");
  247.     scanf("%d", &w_o);
  248. }
  249.  
  250. new_add()
  251. {
  252. int z;
  253.     puts(CLEARS);
  254.     puts("Select 1) New issue or 2) Additional stock\n");
  255.     scanf("%d", &z);
  256.     switch(z){
  257.         case 1:
  258.             org_pur();
  259.             shs_pur();
  260.             break;
  261.         case 2:
  262.             list();
  263.             sel_iss();
  264.             shs_pur();
  265.             break;
  266.         default:
  267.             new_add();
  268.     }
  269. }
  270. up_date()
  271. {
  272.     puts(CLEARS);
  273.     printf("\nEnter price for:  %s: ", name[w_o]);
  274.     atof(price[w_o], gets(str));
  275.     fpmult(value[w_o],price[w_o],shares[w_o]);
  276. }
  277. save()
  278. {
  279. char iobuf[BUFSIZ];
  280. int a,b,c;
  281.     puts(CLEARS);
  282.     puts("Saving file to disk ...");
  283.     if(iss_num < 0){
  284.         puts("CAUTION !!! file is empty.");
  285.         hold();
  286.         menu();
  287.     }
  288.     if(fcreat("AIM.FIL",iobuf) == ERROR){
  289.         puts("\nCan't create AIM.FIL\n");
  290.         return(ERROR);
  291.     }
  292.     putw(iss_num,iobuf);
  293.     for(a=0; a < iss_num +1; ++a){
  294.         for(b=0; b < MAX_LET; ++b){
  295.             c = name[a] [b];
  296.             putw(c,iobuf);
  297.         }
  298.     }
  299.     for(a=0; a< iss_num + 1; ++a){
  300.         for(b=0; b < 5; ++b){
  301.             c = shares[a] [b];
  302.             putw(c,iobuf);
  303.             c = price[a] [b];
  304.             putw(c,iobuf);
  305.             c = cost[a] [b];
  306.             putw(c,iobuf);
  307.             c = value[a] [b];
  308.             putw(c,iobuf);
  309.             c = control[a] [b];
  310.             putw(c,iobuf);
  311.             c = aim_rec[a] [b];
  312.             putw(c,iobuf);
  313.         }
  314.     }
  315.     fflush(iobuf);
  316.     fclose(iobuf);
  317. }
  318. load()
  319. {
  320. char iobuf[BUFSIZ];
  321. int a,b,c;
  322.     puts(CLEARS);
  323.     puts("Loading file ...");
  324.     if(fopen("AIM.FIL",iobuf) == ERROR){
  325.         puts("\nCan't open AIM.FIL\n");
  326.         return(ERROR);
  327.     }
  328.     iss_num = getw(iobuf);
  329.     for(a=0; a < iss_num +1; ++a){
  330.         for(b=0; b < MAX_LET; ++b)
  331.             name[a] [b] = getw(iobuf);
  332.     }
  333.     for(a=0; a < iss_num +1; ++a){
  334.         for(b=0; b < 5; ++b){
  335.             shares[a] [b] = getw(iobuf);
  336.             price[a] [b] = getw(iobuf);
  337.             cost[a] [b] = getw(iobuf);
  338.             value[a] [b] = getw(iobuf);
  339.             control[a] [b] = getw(iobuf);
  340.             aim_rec[a] [b] = getw(iobuf);
  341.         }
  342.     }
  343.     fclose(iobuf);
  344. }
  345. aim()
  346. {
  347. char safe[5];
  348. char test[5];
  349. int y;
  350.     fpmult(safe,value[w_o],m4);
  351.     atof(test, "100");
  352.     y = fpcomp(value[w_o],control[w_o]);
  353.     if(y==1){
  354.         fpsub(aim_rec[w_o],value[w_o],control[w_o]);
  355.         fpsub(aim_rec[w_o],aim_rec[w_o],safe);
  356.         strcpy(bs[w_o], "Sell");
  357.     }
  358.     if(y==-1){
  359.         fpsub(aim_rec[w_o],control[w_o],value[w_o]);
  360.         fpsub(aim_rec[w_o],aim_rec[w_o],safe);
  361.         strcpy(bs[w_o], "Buy ");
  362.     }
  363.     if(y==0)
  364.         atof(aim_rec[w_o], "0");
  365.     y = fpcomp(aim_rec[w_o], test);
  366.     if(y==-1){
  367.         atof(aim_rec[w_o], "0");
  368.         strcpy(bs[w_o], "None");
  369.     }
  370. }
  371. delete()
  372. {
  373. int i,y;
  374.         puts(CLEARS);
  375.         list();
  376.         sel_iss();
  377.         while(w_o < iss_num+1){
  378.             for(i=0; i < 6; ++i){
  379.                 name[w_o] [i] = name[w_o +1] [i];
  380.                 shares[w_o] [i] = shares[w_o + 1] [i];
  381.                 price[w_o] [i] = price[w_o +1] [i];
  382.                 cost[w_o] [i] = cost [w_o +1] [i];
  383.                 value[w_o] [i] = value[w_o +1] [i];
  384.                 control[w_o] [i] = control[w_o +1] [i];
  385.                 aim_rec[w_o] [i] = aim_rec[w_o +1] [i];
  386.             }
  387.         ++w_o;
  388.         }
  389.         iss_num = iss_num -1;
  390. }
  391.  
  392. /* remark end of file aim.c */
  393.  test[