home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / pac / amort.c next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  7.9 KB  |  304 lines

  1. /* amort.c */
  2. /**********************************************************************
  3. *    File Name     : amort.c
  4. *    Function      : amortization (loan) routines of pac
  5. *    Author        : Istvan Mohos, 1987
  6. ***********************************************************************/
  7.  
  8. #include "defs.h"
  9. #define AMORTMAP
  10. #include "maps.h"
  11. #undef AMORTMAP
  12.  
  13. show_loan(play)
  14. int play;
  15. {
  16.     int thisline = AMTY;
  17.     int ck, did_input, Too_big;
  18.     char floatbuf[LOANSIZ + 1];
  19.     int pyp, pxp, ryr, rxr;
  20.     static char *fid = "show_loan";
  21.  
  22.     _TR
  23.     CYX;
  24.     show_float(Amt, AMTY, 2);
  25.     show_float(Rate, PRCY, 3);
  26.     show_float(Years, YRY, 2);
  27.     mvaddstr(DATY, LOANX, "begin mo/year");
  28.     Too_big = show_pymt();
  29.  
  30.     if (!play)
  31.         year();
  32.     else {
  33.         while(TRUE) {
  34.  
  35.             switch(thisline) {
  36.             case AMTY:
  37.                 if (Amt == 0.)
  38.                     clear_wline(thisline, LOANX, LOANR, 0, 1);
  39.                 break;
  40.             case PRCY:
  41.                 if (Rate == 0.)
  42.                     clear_wline(thisline, LOANX, LOANR, 0, 1);
  43.                 break;
  44.             case YRY:
  45.                 if (Years == 0.)
  46.                     clear_wline(thisline, LOANX, LOANR, 0, 1);
  47.                 break;
  48.             }
  49.  
  50.             if ((ck = ledit(floatbuf, loan_map, thisline, LOANX,
  51.                 LOANR, 0, 1, 1)) == 1)
  52.                 break;
  53.             did_input = strlen(floatbuf);
  54.  
  55.             if (ck == 2 && Pymt > 0. && !Too_big) {
  56.                 ryr=CY, rxr=CX;
  57.                 ck = printamort();
  58.                 move(CY=ryr, CX=rxr);
  59.                 pfresh();
  60.                 if (ck == 2)
  61.                     break;
  62.                 continue;
  63.             }
  64.  
  65.             if (did_input) {
  66.                 switch(thisline) {
  67.                     case AMTY:
  68.                         Amt = atof(floatbuf);
  69.                         break;
  70.                     case PRCY:
  71.                         Rate = atof(floatbuf);
  72.                         break;
  73.                     case YRY:
  74.                         Years = atof(floatbuf);
  75.                         break;
  76.                 }
  77.                 Too_big = show_pymt();
  78.             }
  79.  
  80.             if (++thisline > YRY)
  81.                 thisline = AMTY;
  82.         }
  83.         PYX;
  84.         pfresh();
  85.     }
  86.     TR_
  87. }
  88.  
  89. show_pymt()
  90. {
  91.     double nonzero;
  92.     char reformat[32];
  93.     static char *fid = "show_pymt";
  94.  
  95.     _TR
  96.     Months = Years * 12.;
  97.     I_mo = (Rate / 100.) / 12.;
  98.     nonzero = 1. - pow(1. + I_mo, - Months);
  99.     if (nonzero == 0. || I_mo == 0.)
  100.         Pymt = Showpymt = 0.;
  101.     else {
  102.         Pymt = Amt * (I_mo / nonzero);
  103.         Showpymt = Pymt + .00999999;
  104.     }
  105.  
  106.     sprintf(reformat, "%.2f", Showpymt);
  107.     Showpymt = atof(reformat);
  108.     if (show_float(Showpymt, PAYY, 2)) {
  109.         TR_
  110.         return(-1);
  111.     }
  112.     TR_
  113.     return(0);
  114. }
  115.  
  116. show_float(value, where, decimals)
  117. double value;
  118. int where, decimals;
  119. {
  120.     char loanbuf[32];
  121.     char *lp = loanbuf;
  122.     register char *cp;
  123.     static char *fid = "show_float";
  124.  
  125.     _TR
  126.     strcpy (lp, Sp13);
  127.     sprintf(lp,  "%.*f",decimals , value);
  128.     (strlen(lp) < 13) ? (*(lp + strlen(lp)) = ' ') : (*(lp + 13) = 0);
  129.  
  130.     cp = lp + 13;
  131.     if (where > YRY)
  132.         for (; --cp >= lp;)
  133.             if (*cp == '.')
  134.                 break;
  135.  
  136.     if (cp >= lp) {
  137.         mvaddstr(where, LOANX, lp);
  138.         TR_
  139.         return(0);
  140.     }
  141.  
  142.     mvaddstr(where, LOANX, "overflow     ");
  143.     TR_
  144.     return(-1);
  145. }
  146.  
  147. printamort()
  148. {
  149.     double a_amt, ppay, ipay, yppay, yipay;
  150.     FILE *fp;
  151.     char *token, *np;
  152.     int ck, startmonth, startyear, yr;
  153.     int pyp, pxp;
  154.     register int ri;
  155.     int lastmonth;
  156.     char round[32];
  157.     char datebuf[LOANSIZ + 1];
  158.     static char savedate[LOANSIZ + 1];
  159.     char namebuf[TITSIZ + 1];
  160.     char spaceless[TITSIZ + 1];
  161.     int did_input;
  162.     static char *fid = "printamort";
  163.  
  164.     _TR
  165.     if (strlen(savedate))
  166.         mvaddstr(DATY, LOANX, savedate);
  167.  
  168.     if ((ck = ledit(&datebuf[0], date_map, DATY,
  169.         LOANX, LOANR, 0, 1, 0)) == 1) {
  170.         TR_
  171.         return(1);
  172.     }
  173.     if (ck == 2) {  /* types ^B, I have no idea what this does */
  174.         TR_
  175.         return(2);
  176.     }
  177.     did_input = datebuf[0];
  178.  
  179.     if ((token = pactok(&datebuf[0], " .,/:")) == ZERO)
  180.         startmonth = 1, startyear = atoi(Thisyear);
  181.     if ((startmonth = whichmonth(token)) == 0)
  182.         startmonth = 1;
  183.     if ((token = pactok(ZERO, " .,/:")) == ZERO)
  184.         startyear = atoi(Thisyear);
  185.     else if ((startyear = atoi(token)) < 1)
  186.         startyear = atoi(Thisyear);
  187.     else if (startyear < 1900)
  188.         startyear += 1900;
  189.  
  190.     move(DATY, LOANX);
  191.     printw("%.2d/%.4d      ", startmonth, startyear);
  192.     if (did_input)
  193.         sprintf(savedate, "%.2d/%.4d      ", startmonth, startyear);
  194.     pfresh();
  195.  
  196.     Basq[EDITREQ] = Bb[LOANREQ];
  197.     CYX;  /* to save the caller'r coordinates */
  198.     update();  /* this returns to the original coordinates,
  199.                   but does'nt pfresh */
  200. retry:
  201.  
  202.     ledit(namebuf, amort_map, BOT, FBOUND, RBOUND, 1, 1, 0);
  203.  
  204.     if (strlen(namebuf) == 0) {
  205.         strcpy(spaceless, Amortname);
  206.         for (np = spaceless; *np > 32; np++);
  207.         *np = '\0';
  208.         standout();
  209.         mvaddstr(BOT, FBOUND, Amortname);
  210.         standend();
  211.         pfresh();
  212.     }
  213.     else {
  214.         strcpy(Amortname, namebuf);
  215.         strcpy(spaceless, namebuf);
  216.     }
  217.  
  218.     if ((fp = fopen(spaceless, "w")) == NULL) {
  219.         standout();
  220.         mvaddstr(BOT, ULEFT, "can't access:");
  221.         pfresh();
  222.         goto retry;
  223.     }
  224.     
  225.     fprintf(fp, "\
  226. AMORTIZATION SCHEDULE\n\n");
  227.     fprintf(fp, "\
  228.         PREPARED FOR    %s\n", Amortname);
  229.     fprintf(fp, "\
  230.         LOAN AMOUNT     %.2f\n", Amt);
  231.     fprintf(fp, "\
  232.         MONTHLY PAYMENT %.2f\n", Showpymt);
  233.     fprintf(fp, "\
  234.         LENDING RATE    %.3f%c\n", Rate, '\045');
  235.     fprintf(fp, "\
  236.         TERM YEARS      %.2f\n\n", Years);
  237.  
  238. /* format field widths
  239. 12345678901 1234567890123 12345678901234567890 12345678901234567890 XX
  240. */
  241.     fprintf(fp, "\
  242. PAYMENT         DUE               PRINCIPAL          INTEREST           NEW\n");
  243.     fprintf(fp, "\
  244. NUMBER          DATE              PAYMENT            PAYMENT            BALANCE\n\n");
  245.  
  246.     a_amt = Amt;
  247.     ck = startmonth;
  248.     Intrst = yppay = yipay = 0.;
  249.     yr = 1;
  250.     for (ri = 1, lastmonth = (int)Months; ri < lastmonth; ri++) {
  251.         ipay = I_mo * a_amt;
  252.         sprintf(round, "%.2f", ipay);
  253.         ipay = atof(round);
  254.         yipay += ipay;
  255.         ppay = Pymt - ipay + .009999999;
  256.         sprintf(round, "%.2f", ppay);
  257.         ppay = atof(round);
  258.         yppay += ppay;
  259.         a_amt -= ppay;
  260.         fprintf(fp, "\
  261.   %4d      %.2d/01/%.4d    %17.2f %17.2f %17.2f\n",
  262.             ri, ck, startyear, ppay, ipay, a_amt);
  263.         if (++ck > 12)
  264.             ck = 1;
  265.         if (((ri - 1) % 12) == (12 - startmonth)) {
  266.             fprintf(fp, "\
  267. YEAR %.2d TOTAL:            %17.2f %17.2f\n\n", yr++, yppay, yipay);
  268.             Intrst += yipay;
  269.             ++startyear;
  270.             yppay = yipay = 0.;
  271.         }
  272.     }
  273.     ipay = I_mo * a_amt;
  274.     sprintf(round, "%.2f", ipay);
  275.     ipay = atof(round);
  276.     yipay += ipay;
  277.     ppay = Pymt - ipay + .009999999;
  278.     sprintf(round, "%.2f", ppay);
  279.     ppay = atof(round);
  280.     a_amt -= ppay;
  281.     ppay += a_amt;
  282.     yppay += ppay;
  283.     a_amt = 0.;
  284.     Intrst += yipay;
  285.     fprintf(fp, "\
  286.   %4d      %.2d/01/%.4d    %17.2f %17.2f %17.2f\n",
  287.         ri, ck, startyear, ppay, ipay, a_amt);
  288.     fprintf(fp, "\
  289. YEAR %.2d TOTAL:            %17.2f %17.2f\n\n", yr, yppay, yipay);
  290.     fprintf(fp, "\
  291. GRAND TOTAL:              %17.2f %17.2f\n", Amt, Intrst);
  292.     fclose(fp);
  293.  
  294.     strcpy(Bb[LOANREQ] + BUFSTOP, Amortname);
  295.     rev_clear(Bb[LOANREQ] + TITSIZ);
  296.     Basq[EDITREQ] = ZERO;
  297.  
  298.     PYX;
  299.     update();
  300.     TR_
  301.     return(0);
  302. }
  303.  
  304.