home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 600-699 / ff661.lha / Citadel / ctdlaux.lzh / loandoor.c < prev    next >
C/C++ Source or Header  |  1991-07-13  |  10KB  |  385 lines

  1.  
  2. /* Left off translating at j910 */
  3.  
  4. #include <stdio.h>
  5. #include <math.h>
  6. void delay();
  7.  
  8. /* Calculate the interger of x limited to the precision of long interger data types */
  9. long INT(x)
  10. double x;
  11. {
  12. long x1;
  13. x1=x;
  14. return x1;
  15. }
  16.  
  17. double ABS(c)   /* Calculate the absolute value of C. */
  18. double c;
  19. {
  20. if (c<0.0) c=c * -1.0;
  21. return c;
  22. }
  23.  
  24. void crsrup(x,y)
  25. int x,y;
  26. {
  27. int count=x;
  28.  
  29. while (count-->0)
  30.    {
  31.     scr_cursup();
  32.    }
  33.     scr_cr();
  34.     scr_eol();
  35. count=y-1;
  36. while(count-->=1)
  37.    {
  38.     scr_cursrt();
  39.    }
  40. }
  41.  
  42. void main()
  43. {
  44.  
  45. FILE *file, *crt, *out, *fopen(), *freopen();
  46.  
  47. double py,y,in,i,co,pd,x,c8,ra,s8,np,c,
  48.        pa,x9,b,am,ppy,te,mo,ye,y1,k1,k,kf,ki;
  49.  
  50. int testflag=0; /* variable used during debugging some loops */
  51. int flag=0;
  52. int f2=0;
  53. int exF=0;
  54. int fl=0;  /* Multiple unknowns flag */
  55. int br=0;  /* Branch to case: #br */
  56. int delay=150; /* Aproximately 2 seconds delay factor */
  57. long tint=0; /*Temporary interger value for conversions.*/
  58. char aS,exS[20];
  59. char filename[160]; strcpy(filename,"PRT:");
  60. py=y=in=i=co=pd=x=c8=ra=s8=np=c
  61. =pa=x9=b=am=ppy=te=mo=ye=y1=k1=k=kf=ki=0.0;
  62.       crt=fopen("ctdlaux:","w");
  63. /*    crt=fopen("*","w");          */
  64. /*    crt=freopen("*","w",stdout); */
  65. /*        ^^^^^^^^ modified for use as a door program under Cidadel*/
  66.       out=crt;
  67.  
  68. /*
  69.  
  70.     Loan Amortization Program.  Modified to be used remotely with the
  71.     ctdlaux: device. Also commented out stuff at j430: 4/18/91
  72.  
  73.  
  74.     Computes schedule of intrest and payments on a loan.
  75.     Origional program source was in HP 9836 Basic.  Author Unknown.
  76.  
  77.     Translated to Commodore 128 basic 7.0 and Modified from HP 9836 basic
  78.     several years ago (Aproximately 1986) by Garen L. Erdoisa
  79.  
  80.     Translated to AMIGABASIC and modified from Commodore 128 Basic 7.0
  81.     ON 9/18/90 by Garen L. Erdoisa
  82.  
  83.     Translated to Aztec-C 10/9/90 by Garen L. Erdoisa as an excersize in
  84.     C programming.
  85. */
  86.  
  87. strcpy(exS,"Will be Computed!");
  88.  
  89. j50:
  90.  
  91. if (f2=0) out=crt; /* f2=0 then output to screen. 1 then output to file */
  92. scr_clear();
  93. scr_curs(1,0);
  94. printf("You will be asked to enter values for three (out of four)\n");
  95. printf("variables.  The four variables are:\n\n");
  96.  
  97. printf("          Amount of the Loan\n");
  98. printf("          Annual Rate of Interest\n");
  99. printf("          Duration of the Loan\n");
  100. printf("          Amount of Each Payment\n\n");
  101.  
  102. printf("One of the categories (and ONLY one) must be left undefined.\n");
  103. printf("This category will be solved for by the program.  When the program\n");
  104. printf("asks for the category you wish to solve for, Enter   0\n");
  105. printf("and press 'RETURN'\n");
  106. printf("Press   ^C CR   to return to Citadel at anytime.\n");
  107.  
  108. te=6.3179664438;   /* Test Value */
  109. scr_curs(15,0);
  110.  
  111. j180: pa=am=mo=ye=ra=ppy=te;  /* Initialize all values */ 
  112.       fl=0;
  113.       printf("Number of Payments in a year (Required): ");
  114.       scanf("%lf",&ppy);
  115.       if ((ppy != te) && (ppy > 0) && (ppy<=365)) goto j240;
  116.       scr_beep();
  117.       printf("INVALID INPUT\n");crsrup(2,1);goto j180;
  118. j240: crsrup(1,1); printf("Number of Payments in a Year = %4.0lf\n",ppy);
  119.       scr_eol();printf("\n");
  120.  
  121.  
  122. j250: am=0;
  123.       printf("Amount of the loan ? ");scanf("%lf",&am);
  124.       if (am==0) am=te;
  125.       if (am>0) goto j320;
  126.       scr_beep();printf("INVALID INPUT\n");crsrup(2,1);goto j250;
  127. j320: crsrup(1,1);
  128.       if (am==te)
  129.         {
  130.          printf("Amount of Loan               = %s\n",exS);
  131.          if (++fl>1) goto j790;
  132.          br=2;
  133.         }
  134.     else printf("Amount of Loan               = $%.2f\n",am);
  135.  
  136. j380: ra=0;
  137.       printf("Annual Interest Rate ? ");scanf("%lf",&ra);
  138.       if (ra==0) ra=te;
  139.       if (ra>0) goto j450;
  140.       scr_beep();printf("INVALID INPUT\n");crsrup(2,1);goto j380;
  141. j450: crsrup(1,1);
  142.       if (ra==te)
  143.         {
  144.          printf("Annual Interest Rate         = %s\n",exS);
  145.          if (++fl>1) goto j790;
  146.          br=4;
  147.         }
  148.     else printf("Annual Interest Rate         = %.2lf%%\n",ra);
  149.  
  150. j520: ye=mo=0.0;
  151.   scr_eol();printf("Duration of loan        Years: ");scanf("%lf",&ye);
  152.   scr_eol();printf("                       Months: ");scanf("%lf",&mo);
  153.       if ((ye==0)&&(mo==0)) ye=mo=te;
  154.       if ((ye> 0)||(mo> 0)) goto j550;
  155.       scr_beep();crsrup(1,1);crsrup(1,1);
  156.       printf("INVALID INPUT\n");crsrup(2,1);goto j520;
  157. j550: crsrup(1,1);crsrup(1,1);
  158.       if ((ye==te)&&(mo==te))
  159.         {
  160.          printf("Duration of the Loan         :%s\n",exS);
  161.          if (++fl>1) goto j790;
  162.          br=3;
  163.         }
  164.     else
  165.         {
  166.     /*     np=ye*ppy + mo/12.0*ppy;
  167.          y1=INT(np/ppy);
  168.          mo=np-y1*12.0;ye=y1;  */
  169.          printf("Duration of the Loan is      :%.0lf Years, %.2lf Months\n",ye,mo);
  170.         }
  171. j710: pa=0.0;
  172.       printf("Amount of Each Payment       = ");scanf("%lf",&pa);
  173.       if (pa==0.0) pa=te;
  174.       if (pa>0) goto j780;
  175.       scr_beep();printf("INVALID INPUT\n");crsrup(2,1);goto j710;
  176. j780: crsrup(1,1);
  177.       if (pa==te)
  178.         {
  179.          printf("Amount of Each Payment       = %s\n",exS);
  180.          if (++fl>1) goto j790;
  181.          br=1;
  182.         }
  183.       else printf("Amount of each Payment      : $%.2f\n",pa);
  184.  if (fl==1) goto j870;
  185.  
  186.    scr_beep();
  187.    printf("ERROR: You must leave one variable unspecified.\n");
  188.    Delay(delay);goto j50; /* wait 2 seconds then restart the program. */
  189.  
  190. j790:
  191.    printf("ERROR: You must leave just one variable unspecified.\n");
  192.    Delay(delay);goto j50; /* wait 2 seconds then restart the program  */
  193.  
  194. j870:
  195.      if (ra>1) ra=ra/100.0;
  196.      ra=ra/ppy;
  197.      if (br==3) goto j910;
  198.      k=mo/12.0*ppy;
  199.      ki=INT(k);
  200.      kf=k-ki;
  201.      np= ye*ppy + ki - (kf > 0.0); /* This logic test might be a problem */
  202.  
  203. j910:
  204. /*
  205. printf("ppy=%lf, am=%lf, ra=%lf, ye=%lf, mo=%lf, pa=%lf, np=%lf"
  206.         ,ppy, am, ra, ye, mo, pa, np);
  207. goto end;
  208. */
  209. switch(br)
  210.    {
  211.     case 1:
  212.       pa=am*ra*pow((1.0 + ra),np) /( pow((1.0 + ra),np) -1.0);
  213.       pa=INT(pa*100.0+0.5)/100.0;
  214.       break;
  215.  
  216.     case 2:
  217.       am=pa *( pow((1.0+ra),np) -1.0) / pow((1.0+ra),np) /ra;
  218.       tint=(am*100.0+0.5);am=tint/100.0; /* Might be able to get rid of this */
  219.       break;
  220.  
  221.     case 3:
  222.       if ((am*ra/pa)<1.0)
  223.         {
  224.          tint=( -log( 1.0 - am*ra/pa)  / log(1.0 + ra));
  225.          np=tint + 1.0;
  226.         }
  227.       else
  228.         {
  229.          printf("ERROR: Log less than or equal to zero");
  230.          Delay(delay);goto j50;
  231.         }
  232.       y1=np/ppy;
  233.       ye=tint=y1;
  234.       mo=tint=((y1-ye)*12.0+0.5);
  235.       break;
  236.  
  237.     case 4:
  238.       ra=( pa*np/am -1.0) / 12.0;
  239.       if (ra > 0.0) goto j1030;
  240.       printf("ERROR:Rate is negative or zero");
  241.       Delay(delay);goto j50;
  242.  
  243. j1030:
  244.       ra=1.001;flag=0;
  245. j1040:
  246. /* printf("s8=%lf c=%lf x9=%lf b=%lf ra=%lf\n",s8,c,x9,b,ra); used for dubugging */
  247.       s8=(1.0 - pow(ra,(-np)) ) / (ra - 1.0);
  248.  
  249.       c=pa * s8;
  250.       x9=( (ra-1.0) * np * pow(ra,(-np)) / ra -(1.0 - pow(ra,(-np)) )) / pow((ra -1.0),2.0);
  251.       b=pa*x9;
  252.       c=c-am;
  253.       ra=ra-c/b;
  254.  
  255.       if (c8!=c) c8=c; else flag++;
  256.       if ( (ABS(c)>0.01) && (flag<3) ) goto j1040;
  257.  
  258.       ra=ra-1.0;
  259.       if ((ppy*pa) > (ra*am*ppy+1)) break;
  260.       printf("Your first year's payments are  %.2lf\n",ppy*pa);
  261.       printf("The first year's intrest is    %.2lf \n",ra*am*ppy);
  262.       printf("Therefore the life of the mortgage is undefined\n");
  263.       Delay(delay);goto j50;
  264.    }
  265.  
  266.  
  267.     out=crt;Delay(delay);scr_clear();printf("\n\n");
  268.  
  269. j1200:
  270. fprintf(out,"Amount of Loan         : $%.2lf\n",am);
  271. fprintf(out,"Total Cost of Loan     : $%.2lf\n",pa*np-am);
  272. fprintf(out,"Annual Interest Rate   :         %3.2lf%%\n",ra*ppy*100.0);
  273. fprintf(out,"Total Payments         : $%.2lf\n",pa*np);
  274. fprintf(out,"Duration of the Loan   :  %.0lf ",np);
  275. fprintf(out,"Payment Periods (%.0lf",ye);
  276.  if (mo==te) fprintf(out,") Years\n");
  277.     else fprintf(out,") Years, (%.2lf) Months\n",mo);
  278. fprintf(out,"Amount of Each Payment : $%.2lf\n",pa);
  279. fprintf(out,"Payments per year      : %.0lf\n\n\n",ppy);
  280.  
  281. /* this stuff was used during debugging of source code.
  282. printf("\n\nbr=%d ppy=%lf, am=%lf, ra=%lf, ye=%lf, mo=%lf, pa=%lf, np=%lf\n"
  283.         ,br,ppy, am, ra, ye, mo, pa, np);
  284. goto end;
  285. */
  286. if (f2==1) goto j1465;
  287.  
  288. printf("P - View Payment Schedule\n");
  289. printf("R - Restart program\n");
  290. printf("Q - Quit Program.\n");
  291. loop:
  292.   aS='\0';
  293.   scanf("%c",&aS);
  294.   if ((aS=='\0')||(aS=='\12')||(aS=='\14')) goto loop;
  295.  
  296.   if ((aS=='R')||(aS=='r')) goto j50;
  297.   if ((aS=='Q')||(aS=='q')) goto end;
  298.   if ((aS=='P')||(aS=='p')) goto schedule;
  299.   printf("\nINVALID INPUT\n");
  300.   crsrup(3,1);
  301.   goto loop;
  302.  
  303. schedule:
  304.  
  305. f2=0;out=crt;
  306. scr_clear();
  307.  
  308. /*
  309.    This stuff is not needed for remote operation.
  310.  
  311. printf("\n\nOutput to (S)reen or (F)ile?\n");
  312.  
  313. j1430:
  314.   scanf("%c",&aS);
  315.   if ((aS=='F')||(aS=='f'))
  316.      {
  317.        printf("Enter PRT: for filename if you wish to send output to the line printer.\n");
  318.        printf("\nFilename : ");scanf("%s",&filename);
  319.        file=fopen(filename,"w");
  320.        out=file;
  321.        f2=1;
  322.        goto j1460;
  323.      }
  324.  
  325.   if ((aS=='S')||(aS=='s')) goto j1460;
  326.  
  327.   crsrup(1,1);goto schedule;
  328. */
  329.  
  330. j1460: if (f2==1) goto j1200;
  331.        fprintf(out,"\n");
  332. j1465:
  333.  
  334.        if (f2==1) fprintf(out,"%c\n\n",'\14'); /* send form feed */
  335.  
  336. j1490:
  337.   fprintf(out,
  338. "Payment #       Payment       Principle        Interest          Ballance\n");
  339.  
  340.      x=am;
  341.      pd=0;
  342.      co=0;
  343.      for (i=1;i<=np;i++)
  344.          {
  345.  
  346.    j1560:
  347.           in=ra*x;
  348.           y=pa-in;
  349.           if (y<=x) goto j1610;
  350.           y=x;
  351.           pa=y+in;
  352.    j1610:
  353.           x=x-y;
  354.           fprintf(out,"   %.0lf\t\t",i)    ;
  355.           fprintf(out,"$%.2lf\t\t",pa)  ;
  356.           fprintf(out,"$%.2lf\t\t",y)   ;
  357.           fprintf(out,"$%.2lf\t\t",in)  ;
  358.           fprintf(out,"$%.2lf\n",x) ;
  359.           pd=pd+py;
  360.           co=co+1;
  361.           if ( ((co<60) && (i<=np-1)) || (f2==0) || (i==np)) goto j1710;
  362.             co=0;
  363.             fprintf(out,"%c\n\n",'\14');
  364.             fprintf(out,
  365. "Payment #       Payment       Principle        Interest          Ballance\n");
  366.  
  367.    j1710: ;
  368.  
  369.  
  370.          }
  371.          if (f2==1) {f2=0;fprintf(out,"\n");out=crt;}
  372.          printf("\n\n");
  373.    j1730:
  374.          printf("(Q)uit or (R)estart?\n");
  375.  
  376.    j1740:
  377.          scanf("%c",&aS);
  378.  
  379.          if ((aS=='Q')||(aS=='q')) goto end;
  380.          if ((aS=='R')||(aS=='r')) goto j50;
  381.          crsrup(1,1);
  382.          goto j1730;
  383. end: ;
  384. }
  385.