home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 244.lha / TinyLoan / Loan.c < prev    next >
C/C++ Source or Header  |  1989-05-01  |  13KB  |  433 lines

  1. /*
  2.  *  SCREEN/WINDOW Demo  in Manx C   Brian Jackson
  3.  *
  4.  *  Butchered By Allen - No custom screen now, just a window & gadgets
  5.  *
  6.  *   compile with:
  7.  *        cc Loan
  8.  *   link with:
  9.  *        ln +Cdb +Q Loan -lm -lc
  10.  *
  11.  *   Exit by clicking on the window's CLOSE gadget.
  12.  *
  13.  **********************************************************************/
  14.  
  15.  #include <stdio.h>
  16.  #include <ctype.h>
  17.  #include <intuition/intuition.h>
  18.  #include <functions.h>
  19.  
  20.  #define  RN          33L             /* the current library revision # */
  21.  #define  RECALC        1
  22.  #define  PRINTIT       2
  23.  #define  YES           1
  24.  #define  NO            2
  25.  
  26.  struct IntuitionBase *IntuitionBase = NULL ;
  27.  struct GfxBase       *GfxBase       = NULL ;
  28.  struct Window        *MW            = NULL ;
  29.  struct IntuiMessage  *message ;
  30.  
  31.  
  32. UBYTE UNDOBUFFER[8];
  33.  
  34. SHORT GadBorderVector1[] = {0,0,83,0,83,13,0,13,0,0};
  35.  
  36. struct Border GadBorder1 = {
  37.     -2,-1,    /* Right & Down offset from top left */
  38.     3,0,JAM1,    /* Front & Back pen & drawmode */
  39.     5,    /* vector count */
  40.     GadBorderVector1,    /* pointer to XY vectors */
  41.     NULL    /* next border in list */
  42. };
  43.  
  44. struct IntuiText IntuiText1 = {
  45.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  46.     17,2,    /* Right & Down offset from top left */
  47.     NULL,    /* font pointer or NULL for default */
  48.     (UBYTE *)"RECALC",    /* pointer to text */
  49.     NULL    /* next IntuiText structure */
  50. };
  51.  
  52. struct Gadget Gadget5 = {
  53.     NULL,    /* next gadget */
  54.     115,64,    /* origin XY of hit box relative to window TopLeft */
  55.     80,12,    /* hit box width and height */
  56.     GADGHCOMP,    /* gadget flags */
  57.     RELVERIFY,    /* activation flags */
  58.     BOOLGADGET,    /* gadget type flags */
  59.     (APTR)&GadBorder1,    /* gadget border or image to be rendered */
  60.     NULL,    /* alternate imagery for selection */
  61.     &IntuiText1,    /* first IntuiText structure */
  62.     NULL,    /* gadget mutual-exclude long word */
  63.     NULL,    /* SpecialInfo structure */
  64.     RECALC,    /* user-definable data */
  65.     NULL    /* pointer to user-definable data */
  66. };
  67.  
  68.  
  69. SHORT GadBorderVector2[] = {0,0,83,0,83,13,0,13,0,0};
  70.  
  71. struct Border GadBorder2 = {
  72.     -2,-1,    /* Right & Down offset from top left */
  73.     3,0,JAM1,    /* Front & Back pen & drawmode */
  74.     5,    /* vector count */
  75.     GadBorderVector2,    /* pointer to XY vectors */
  76.     NULL    /* next member of linked list */
  77. };
  78.  
  79. struct IntuiText IntuiText2 = {
  80.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  81.     20,2,    /* Right & Down offset from top left */
  82.     NULL,    /* font pointer or NULL for default */
  83.     (UBYTE *)"PRINT",    /* pointer to text */
  84.     NULL    /* next IntuiText structure */
  85. };
  86.  
  87. struct Gadget Gadget4 = {
  88.     &Gadget5,    /* next gadget */
  89.     21,64,    /* origin XY of hit box relative to window TopLeft */
  90.     80,12,    /* hit box width and height */
  91.     GADGHCOMP,    /* gadget flags */
  92.     RELVERIFY,    /* activation flags */
  93.     BOOLGADGET,    /* gadget type flags */
  94.     (APTR)&GadBorder2,    /* gadget border or image to be rendered */
  95.     NULL,    /* alternate imagery for selection */
  96.     &IntuiText2,    /* first IntuiText structure */
  97.     NULL,    /* gadget mutual-exclude long word */
  98.     NULL,    /* SpecialInfo structure */
  99.     PRINTIT,    /* user-definable data */
  100.     NULL    /* pointer to user-definable data */
  101. };
  102.  
  103. UBYTE Gadget3SIBuff[3] = "4";
  104.  
  105. struct StringInfo Gadget3SInfo = {
  106.     Gadget3SIBuff,    /* buffer where text will be edited */
  107.     UNDOBUFFER,    /* optional undo buffer */
  108.     0,    /* character position in buffer */
  109.     3,    /* maximum number of characters to allow */
  110.     0,    /* first displayed character buffer position */
  111.     0,0,0,0,0,    /* Intuition initialized and maintained variables */
  112.     0,    /* Rastport of gadget */
  113.     0,    /* initial value for integer gadgets */
  114.     NULL    /* alternate keymap (fill in if you set the flag) */
  115. };
  116.  
  117. SHORT GadBorderVector3[] = {0,0,71,0,71,9,0,9,0,0};
  118.  
  119. struct Border GadBorder3 = {
  120.     -2,-1,    /* Right & Down offset from top left */
  121.     3,0,JAM1,    /* Front & Back pen & drawmode */
  122.     5,    /* vector count */
  123.     GadBorderVector3,    /* pointer to XY vectors */
  124.     NULL    /* next member of linked list */
  125. };
  126.  
  127. struct Gadget Gadget3 = {
  128.     &Gadget4,    /* next gadget */
  129.     127,49,    /* origin XY of hit box relative to window TopLeft */
  130.     68,8,    /* hit box width and height */
  131.     NULL,    /* gadget flags */
  132.     RELVERIFY+STRINGCENTER,    /* activation flags */
  133.     STRGADGET,    /* gadget type flags */
  134.     (APTR)&GadBorder3,    /* gadget border or image to be rendered */
  135.     NULL,    /* alternate imagery for selection */
  136.     NULL,    /* first IntuiText structure */
  137.     NULL,    /* gadget mutual-exclude long word */
  138.     (APTR)&Gadget3SInfo,    /* SpecialInfo structure */
  139.     NULL,    /* user-definable data */
  140.     NULL    /* pointer to user-definable data */
  141. };
  142.  
  143. UBYTE Gadget2SIBuff[8] = "13000";
  144.  
  145. struct StringInfo Gadget2SInfo = {
  146.     Gadget2SIBuff,    /* buffer where text will be edited */
  147.     UNDOBUFFER,    /* optional undo buffer */
  148.     0,    /* character position in buffer */
  149.     8,    /* maximum number of characters to allow */
  150.     0,    /* first displayed character buffer position */
  151.     0,0,0,0,0,    /* Intuition initialized and maintained variables */
  152.     0,    /* Rastport of gadget */
  153.     0,    /* initial value for integer gadgets */
  154.     NULL    /* alternate keymap (fill in if you set the flag) */
  155. };
  156.  
  157. SHORT GadBorderVector4[] = {0,0,71,0,71,9,0,9,0,0};
  158.  
  159. struct Border GadBorder4 = {
  160.     -2,-1,    /* Right & Down offset from top left */
  161.     3,0,JAM1,    /* Front & Back pen & drawmode */
  162.     5,    /* vector count */
  163.     GadBorderVector4,    /* pointer to XY vectors */
  164.     NULL    /* next member of linked list */
  165. };
  166.  
  167. struct Gadget Gadget2 = {
  168.     &Gadget3,    /* next gadget */
  169.     127,35,    /* origin XY of hit box relative to window TopLeft */
  170.     68,8,    /* hit box width and height */
  171.     NULL,    /* gadget flags */
  172.     RELVERIFY+STRINGCENTER,    /* activation flags */
  173.     STRGADGET,    /* gadget type flags */
  174.     (APTR)&GadBorder4,    /* gadget border or image to be rendered */
  175.     NULL,    /* alternate imagery for selection */
  176.     NULL,    /* first IntuiText structure */
  177.     NULL,    /* gadget mutual-exclude long word */
  178.     (APTR)&Gadget2SInfo,    /* SpecialInfo structure */
  179.     NULL,    /* user-definable data */
  180.     NULL    /* pointer to user-definable data */
  181. };
  182.  
  183. UBYTE Gadget1SIBuff[6] ="13.5";
  184.  
  185. struct StringInfo Gadget1SInfo = {
  186.     Gadget1SIBuff,    /* buffer where text will be edited */
  187.     UNDOBUFFER,    /* optional undo buffer */
  188.     0,    /* character position in buffer */
  189.     6,    /* maximum number of characters to allow */
  190.     0,    /* first displayed character buffer position */
  191.     0,0,0,0,0,    /* Intuition initialized and maintained variables */
  192.     0,    /* Rastport of gadget */
  193.     0,    /* initial value for integer gadgets */
  194.     NULL    /* alternate keymap (fill in if you set the flag) */
  195. };
  196.  
  197. SHORT GadBorderVector5[] = {0,0,71,0,71,9,0,9,0,0};
  198.  
  199. struct Border GadBorder5 = {
  200.     -2,-1,    /* Right & Down offset from top left */
  201.     3,0,JAM1,    /* Front & Back pen & drawmode */
  202.     5,    /* vector count */
  203.     GadBorderVector5,    /* pointer to XY vectors */
  204.     NULL    /* next member of linked list */
  205. };
  206.  
  207. struct Gadget Gadget1 = {
  208.     &Gadget2,    /* next gadget */
  209.     127,21,    /* origin XY of hit box relative to window TopLeft */
  210.     68,8,    /* hit box width and height */
  211.     NULL,    /* gadget flags */
  212.     RELVERIFY+STRINGCENTER,    /* activation flags */
  213.     STRGADGET,    /* flags */
  214.     (APTR)&GadBorder5,    /* border */
  215.     NULL,    /* alternate imagery for selection */
  216.     NULL,    /* first IntuiText structure */
  217.     NULL,    /* gadget mutual-exclude long word */
  218.     (APTR)&Gadget1SInfo,    /* SpecialInfo structure */
  219.     NULL,    /* user-definable data */
  220.     NULL    /* pointer to user-definable data */
  221. };
  222.  
  223. UBYTE UpdateBuff[30];
  224.  
  225. struct IntuiText IntuiText8 = {
  226.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  227.     25,92,    /* Right & Down offset from top left */
  228.     NULL,    /* font pointer or NULL for default */
  229.     UpdateBuff,    /* pointer to text */
  230.     NULL    /* next IntuiText structure */
  231. };
  232.  
  233. struct IntuiText IntuiText7 = {
  234.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  235.     25,82,    /* Right & Down offset from top left */
  236.     NULL,    /* font pointer or NULL for default */
  237.     (UBYTE *)"Your Payments Will Be",    /* pointer to text */
  238.     &IntuiText8    /* next IntuiText structure */
  239. };
  240.  
  241. struct IntuiText IntuiText6 = {
  242.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  243.     75,49,    /* Right & Down offset from top left */
  244.     NULL,    /* font pointer or NULL for default */
  245.     (UBYTE *)"Years",    /* pointer to text */
  246.     &IntuiText7    /* next IntuiText structure */
  247. };
  248.  
  249. struct IntuiText IntuiText5 = {
  250.     3,0,
  251. JAM2,    /* front and back text pens, drawmode and fill byte */
  252.     35,35,    /* Right & Down offset from top left */
  253.     NULL,    /* font pointer or NULL for default */
  254.     (UBYTE *)"Loan Value",    /* pointer to text */
  255.     &IntuiText6    /* next IntuiText structure */
  256. };
  257.  
  258. struct IntuiText IntuiText4 = {
  259.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  260.     186,21,    /* Right & Down offset from top left */
  261.     NULL,    /* font pointer or NULL for default */
  262.     (UBYTE *)"%",    /* pointer to text */
  263.     &IntuiText5    /* next IntuiText structure */
  264. };
  265.  
  266. struct IntuiText IntuiText3 = {
  267.     3,0,JAM2,    /* front and back text pens, drawmode and fill byte */
  268.     10,21,    /* Right & Down offset from top left */
  269.     NULL,    /* font pointer or NULL for default */
  270.     (UBYTE *)"Interest Rate",    /* pointer to text */
  271.     &IntuiText4    /* next IntuiText structure */
  272. };
  273.  
  274.  struct NewWindow newwindow = {
  275.     19,19,        /* left edge, top edge */
  276.     223,107,       /* width, height       */
  277.     0,1,          /* detail pen, blk pen */
  278.     CLOSEWINDOW|GADGETUP,  /* IDCMP flags         */
  279.       WINDOWDEPTH | WINDOWDRAG      /* flags */
  280.       | SMART_REFRESH | ACTIVATE 
  281.       | NOCAREREFRESH | WINDOWCLOSE,
  282.     &Gadget1,    /* firstgadget         */
  283.     NULL,        /* checkmark           */
  284.     (UBYTE *)" Loan Calculator ",
  285.     NULL,        /* Screen Pointer . See Below! */
  286.     NULL,        /* bitmap pointer      */
  287.     5,5,         /* minwidth, minheight */
  288.     640,200,     /* maxwidth, maxheight */
  289.     WBENCHSCREEN /* screentype          */
  290.     };
  291.  
  292.   
  293.  main()
  294.  {
  295.  
  296.  ULONG class;
  297.  float atof();
  298.  int loan();
  299.    
  300.    if (( IntuitionBase = 
  301.       (struct IntuitionBase *)OpenLibrary("intuition.library",RN)) == NULL)
  302.           GetOut ("Error:  No Intuition\n");
  303.  
  304.    if ((GfxBase =
  305.        (struct GfxBase *)OpenLibrary("graphics.library",RN)) == NULL)
  306.           GetOut ("Error:  No Graphics.lib\n");
  307.  
  308.    if ((MW = (struct Window *)OpenWindow(&newwindow)) == NULL)
  309.           GetOut ("Error:  No Window\n");
  310.  
  311.        loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
  312.                 atof(Gadget2SIBuff),UpdateBuff);
  313.  
  314.        PrintIntuiText(MW->RPort,&IntuiText3,0L,0L);
  315.  
  316.    FOREVER  {
  317.  
  318.        Wait( 1L << MW->UserPort->mp_SigBit) ;
  319.  
  320.        while(message = (struct IntuiMessage *)GetMsg(MW->UserPort)) {
  321.            class = message->Class;
  322.            ReplyMsg(message);
  323.  
  324.            if (class == GADGETUP)
  325.         {
  326.             switch (((struct Gadget *)(message->IAddress))->GadgetID)
  327.             {
  328.             case RECALC: loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
  329.                            atof(Gadget2SIBuff),UpdateBuff,NO);
  330.                       PrintIntuiText(MW->RPort,&IntuiText8,0L,0L);
  331.                       break;
  332.  
  333.             case PRINTIT: loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
  334.                             atof(Gadget2SIBuff),UpdateBuff,NO);
  335.                       PrintIntuiText(MW->RPort,&IntuiText8,0L,0L);
  336.                       loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
  337.                             atof(Gadget2SIBuff),UpdateBuff,YES);
  338.                       break;
  339.             }
  340.             break;
  341.         }
  342.  
  343.            if (class == CLOSEWINDOW)
  344.         {
  345.         GetOut(UpdateBuff);
  346.         }
  347.  
  348.            }        /* end while message     */
  349.        }        /* end forever           */
  350.   }            /* end main              */
  351.  
  352.  GetOut( lastword )
  353.  TEXT *lastword ;             /* Close things in REVERSE order!! */
  354.  {                  
  355.      if(MW)               CloseWindow(MW);
  356.      if(GfxBase)          CloseLibrary(GfxBase);
  357.      if(IntuitionBase)    CloseLibrary(IntuitionBase);
  358.      if( lastword )       puts(lastword) ;
  359.      exit(0) ;
  360.  }      /* end function */
  361.  
  362.  
  363.  
  364. /********* LOAN FUNCTION *******/
  365.  
  366. int
  367. loan(length, rate, value, spointer, ToPrinter)
  368. float length, rate, value;
  369. char *spointer;
  370. int ToPrinter;
  371.  
  372. {
  373.     float pow();
  374.     float bal, i, m, intr, pmt, rate2;
  375.     float w1, w2, w3, w4;
  376.     FILE *OutPut;        /* file pointer printer */
  377.  
  378.     /* length in years */
  379.     if(length < .1){
  380.         strcpy(spointer," *** ENTER YEARS ***  ");
  381.         return(-1);
  382.         }
  383.     length = length * 12.0;
  384.  
  385.     /* interest rate */
  386.     if(rate < .1){
  387.         strcpy(spointer,"*** ENTER RATE ***    ");
  388.         return(-1);
  389.         }
  390.     rate2=rate;
  391.     rate = (rate/12.0) / 100.0;
  392.  
  393.     /* loan value */
  394.     if(value < .1){
  395.         strcpy(spointer,"*** ENTER VALUE ***   ");
  396.         return(-1);
  397.         }
  398.     w1 = (1 + rate); w2 = -length; w3 = pow(w1,w2);
  399.     pmt = value * rate/(1 - w3);
  400.  
  401.     sprintf(spointer,"$%4.2f Per Month.    ",pmt);
  402.  
  403.  
  404.     if(ToPrinter==YES)
  405.         {
  406.  
  407.         /* try to open the printer */
  408.         if ((OutPut = fopen ("PRT:", "w")) == NULL)
  409.             {
  410.             DisplayBeep(NULL);
  411.             return(-1);
  412.             }
  413.  
  414.         fprintf(OutPut,"The interest rate is  %2.2f.\n", rate2);
  415.         fprintf(OutPut,"The loan value is $%.2f.\n", value);
  416.         fprintf(OutPut,"The loan is for %f years (%.0f months).\n\n", length/12.0, length);
  417.         fprintf(OutPut,"The monthly payment amount is %7.2f.\n\n", pmt);
  418.         fprintf(OutPut,"Month   Payment      Balance     Interest\n");
  419.         fprintf(OutPut,"-----------------------------------------\n");
  420.         for (m = 1; m < length + 1; m++)
  421.             {
  422.             w2 = -m; w4 = pow(w1,w2);
  423.             bal = (value / w4) * (w4-w3)/(1-w3);
  424.             intr = m * pmt - value + bal;
  425.             fprintf(OutPut,"%4.0f   %8.2f    %9.2f    %9.2f\n", m, pmt, bal, intr);
  426.             }
  427.         fprintf(OutPut,"\n");
  428.         }
  429.  
  430.     return(0);
  431. }
  432.  
  433.