home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / corel / Scripts / finance.csc < prev    next >
Text File  |  1998-02-06  |  9KB  |  249 lines

  1. rem VypoΦφtß p∙jΦky.
  2. rem Finance.csc  20.srpna 1996
  3. rem Copyright 1998 Corel Corporation. VÜechna prßva vyhrazena.
  4.  
  5. REM ***************************************************************
  6. REM * Global Data                                                 *
  7. REM ***************************************************************
  8.  
  9. 'Standard constants
  10. #include "ScpConst.CSI"
  11.  
  12. REM ***************************************************************
  13. REM * Main Dialog                                                 *
  14. REM ***************************************************************
  15. BEGIN DIALOG OBJECT Calculator 511, 217, "Financial Calculator", SUB CalculatorSub
  16.     PUSHBUTTON  16, 200, 46, 14, .Calculate, "Calculate"
  17.     PUSHBUTTON  69, 200, 46, 14, .ExitButton, "Exit"
  18.     TEXT  8, 16, 44, 8, .Text1, "Initial Capital"
  19.     SPINCONTROL  67, 15, 67, 12, .Capital
  20.     TEXT  8, 30, 44, 8, .Text2, "Interest Rate"
  21.     SPINCONTROL  67, 29, 67, 12, .Interest
  22.     TEXT  8, 44, 58, 8, .Text3, "Number of Periods"
  23.     SPINCONTROL  67, 43, 67, 12, .Periods
  24.     TEXT  8, 58, 44, 8, .Text5, "Payment"
  25.     SPINCONTROL  67, 57, 67, 12, .Payment
  26.     GROUPBOX  8, 97, 130, 42, .GroupBox1, "Interest compounded"
  27.     OPTIONGROUP .CompoundWhen
  28.         OPTIONBUTTON  17, 109, 110, 10, .StartPeriod, "at start of period"
  29.         OPTIONBUTTON  17, 121, 110, 10, .EndPeriod, "at end of period"
  30.     GROUPBOX  8, 151, 130, 42, .GroupBox2, "Obtain"
  31.     OPTIONGROUP .ObtainWhat
  32.         OPTIONBUTTON  17, 163, 114, 10, .ObtainPayment, "payment from number of periods"
  33.         OPTIONBUTTON  17, 175, 116, 10, .ObtainPeriod, "number of periods from payment"
  34.     TEXT  174, 16, 29, 8, .Text7, "Period"
  35.     LISTBOX  174, 26, 31, 191, .PeriodList
  36.     TEXT  209, 16, 50, 8, .Text8, "Intial Capital"
  37.     LISTBOX  209, 26, 71, 191, .CapitalBeforeList
  38.     TEXT  285, 16, 50, 8, .InterestText, "Interest"
  39.     LISTBOX  285, 26, 71, 191, .InterestList
  40.     TEXT  361, 16, 50, 8, .PaymentText, "Payment"
  41.     LISTBOX  361, 26, 71, 191, .PaymentList
  42.     TEXT  437, 16, 62, 8, .Text11, "Remaining Capital"
  43.     LISTBOX  437, 26, 71, 191, .CapitalAfterList
  44. END DIALOG
  45.  
  46. REM ***************************************************************
  47. REM * Main Program                                                *
  48. REM ***************************************************************
  49. 'Initialize data
  50. WITH Calculator
  51.     .Capital.SetMinRange 0
  52.     .Capital.SetMaxRange 1000000000
  53.     .Capital.SetDoubleMode TRUE
  54.     .Capital.SetPrecision 2
  55.     .Capital.SetIncrement 1000
  56.     .Capital.SetValue 25000
  57.     .Interest.SetMinRange 0.5
  58.     .Interest.SetMaxRange 50.0
  59.     .Interest.SetDoubleMode TRUE
  60.     .Interest.SetPrecision 2
  61.     .Interest.SetIncrement .5
  62.     .Interest.SetValue 8
  63.     .Periods.SetMinRange 3
  64.     .Periods.SetMaxRange 200
  65.     .Periods.SetDoubleMode FALSE
  66.     .Periods.SetIncrement 1
  67.     .Periods.SetValue 20
  68.     .Payment.SetMinRange 0
  69.     .Payment.SetMaxRange 100000000000
  70.     .Payment.SetDoubleMode TRUE
  71.     .Payment.SetPrecision 2
  72.     .Payment.SetIncrement 1
  73.     .Payment.SetValue 0
  74.     
  75.     .SetStyle STYLE_MINIMIZEBOX
  76. END WITH
  77.  
  78. 'Call the dialog (it does all the work)
  79. DIALOG Calculator
  80.  
  81. REM ***************************************************************
  82. REM * MakeString: Makes a string with two decimal places out of a *
  83. REM * currency value.                                             *
  84. REM ***************************************************************
  85. FUNCTION MakeString (Value AS CURRENCY) AS STRING
  86.     DIM ReturnVal AS STRING
  87.     ReturnVal$ = CSTR(Value)
  88.     IF INT(Value) = Value THEN
  89.         ReturnVal$ = ReturnVal$ + ".00"
  90.     ELSEIF INT(Value * 10) = Value * 10 THEN
  91.         ReturnVal$ = ReturnVal$ + "0"
  92.     ENDIF
  93.     MakeString = ReturnVal$
  94. END FUNCTION
  95.  
  96. REM ***************************************************************
  97. REM * CalculatorSub: Event handler for the main dialog. This SUB  *
  98. REM * handles all of the user commands; it is, in effect, the     *
  99. REM * heart of the program.                                       *
  100. REM ***************************************************************
  101. SUB CalculatorSub (BYVAL ControlID%, BYVAL Event%)
  102.     DIM PaymentValue AS DOUBLE
  103.     DIM DistanceValue AS CURRENCY
  104.     DIM CurrentCapital AS CURRENCY
  105.     DIM CurrentInterest AS CURRENCY
  106.     DIM CurrentPayment AS CURRENCY
  107.     DIM Period AS INTEGER
  108.     DIM ToAdd AS STRING
  109.     WITH Calculator
  110.         IF Event% = EVENT_MOUSE_CLICK THEN
  111.             'Exit
  112.             IF ControlID% = .ExitButton.GetID() THEN .CloseDialog MSG_CANCEL
  113.             
  114.             'Calculate
  115.             IF ControlID% = .Calculate.GetID() THEN
  116.                 'Check for validity
  117.                 IF .ObtainWhat.GetValue() = 1 AND .Payment.GetValue() = 0 THEN
  118.                     MESSAGE "Must have a payment greater than 0"
  119.                     EXIT SUB
  120.                 ENDIF
  121.                 IF .Capital.GetValue() = 0 THEN
  122.                     MESSAGE "Must have a capital greater than 0"
  123.                     EXIT SUB
  124.                 ENDIF
  125.                 'Calculate payment from periods if necessary
  126.                 IF .ObtainWhat.GetValue() = 0 THEN
  127.                     DistanceValue = .Capital.GetValue()
  128.                     IF .CompoundWhen.GetValue() = 0 THEN
  129.                         'Compound at start of period (before payment)
  130.                         PaymentValue = (DistanceValue * (.Interest.GetValue() / 100.0))
  131.                         PaymentValue = PaymentValue / (1.0 - ((1.0 + (.Interest.GetValue() / 100.0))^(-(.Periods.GetValue()))))
  132.                     ELSE
  133.                         'Compound at end of period (after payment)
  134.                         PaymentValue = ((DistanceValue) * (.Interest.GetValue() / 100.0))
  135.                         PaymentValue = PaymentValue / (1.0 - ((1.0 + (.Interest.GetValue() / 100.0))^(-(.Periods.GetValue() - 1.0)))+ (.Interest.GetValue() / 100.0))
  136.                     ENDIF
  137.                     'Adjust payment for rounding errors
  138.                     IF PaymentValue * 100 - INT(PaymentValue * 100) > 0 THEN
  139.                         PaymentValue = INT(PaymentValue * 100) / 100 + .01
  140.                     ENDIF
  141.                     'Set it in the dialog
  142.                     .Payment.SetValue PaymentValue
  143.                 ENDIF
  144.                 'Empty all list boxes
  145.                 .PeriodList.Reset
  146.                 .CapitalBeforeList.Reset
  147.                 .InterestList.Reset
  148.                 .PaymentList.Reset
  149.                 .CapitalAfterList.Reset
  150.                 'Move the boxes around corresponding to the compund time
  151.                 IF .CompoundWhen.GetValue() = 0 THEN
  152.                     'Move the list so the interest is before the payment
  153.                     .InterestText.Move 285,16
  154.                     .InterestList.Move 285,26
  155.                     .PaymentText.Move 361, 16
  156.                     .PaymentList.Move 361, 26
  157.                     CurrentCapital = .Capital.GetValue()
  158.                     Period% = 1
  159.                     DO WHILE CurrentCapital > 0
  160.                         'Calculate each payment and result
  161.                         
  162.                         'Period number
  163.                         .PeriodList.AddItem Period%
  164.                         
  165.                         'Current capital
  166.                         ToAdd$ = MakeString (CurrentCapital)
  167.                         .CapitalBeforeList.AddItem ToAdd$
  168.                         
  169.                         'Interest
  170.                         CurrentInterest = CurrentCapital * (.Interest.GetValue() / 100.0)
  171.                         'Adjust for rounding errors
  172.                         IF CurrentInterest * 100 - INT(CurrentInterest * 100) > 0 THEN
  173.                             CurrentInterest = INT(CurrentInterest * 100.0) / 100.0
  174.                         ENDIF
  175.                         CurrentCapital = CurrentCapital + CurrentInterest
  176.                         ToAdd$ = MakeString (CurrentInterest)
  177.                         .InterestList.AddItem ToAdd$
  178.                         
  179.                         'Payment
  180.                         CurrentPayment = .Payment.GetValue()
  181.                         'Adjust for rounding errors
  182.                         IF CurrentPayment * 100 - INT(CurrentPayment * 100) > 0 THEN
  183.                             CurrentPayment = INT(CurrentPayment * 100) / 100 + .01
  184.                         ENDIF
  185.                         'Make sure we never pay too much
  186.                         IF CurrentPayment > CurrentCapital THEN CurrentPayment = CurrentCapital
  187.                         ToAdd$ = MakeString (CurrentPayment)
  188.                         .PaymentList.AddItem ToAdd$
  189.                         
  190.                         'Resulting capital
  191.                         CurrentCapital = CurrentCapital - CurrentPayment
  192.                         ToAdd$ = MakeString (CurrentCapital)
  193.                         .CapitalAfterList.AddItem ToAdd$
  194.                         Period% = Period% + 1
  195.                     LOOP                
  196.                 ELSE
  197.                     'Move the list so the payment is before the interest
  198.                     .PaymentText.Move 285,16
  199.                     .PaymentList.Move 285,26
  200.                     .InterestText.Move 361, 16
  201.                     .InterestList.Move 361, 26
  202.                     CurrentCapital = .Capital.GetValue()
  203.                     Period% = 1
  204.                     DO WHILE CurrentCapital > 0
  205.                         'Calculate each payment and result
  206.                         
  207.                         'Period number
  208.                         .PeriodList.AddItem Period%
  209.                         
  210.                         'Current capital
  211.                         ToAdd$ = MakeString (CurrentCapital)
  212.                         .CapitalBeforeList.AddItem ToAdd$
  213.                         
  214.                         'Payment
  215.                         CurrentPayment = .Payment.GetValue()
  216.                         'Adjust for rounding errors
  217.                         IF CurrentPayment * 100 - INT(CurrentPayment * 100) > 0 THEN
  218.                             CurrentPayment = INT(CurrentPayment * 100) / 100 + .01
  219.                         ENDIF
  220.                         'Make sure we never pay too much
  221.                         IF CurrentPayment > CurrentCapital THEN CurrentPayment = CurrentCapital
  222.                         ToAdd$ = MakeString (CurrentPayment)
  223.                         .PaymentList.AddItem ToAdd$
  224.                         CurrentCapital = CurrentCapital - CurrentPayment
  225.                         
  226.                         'Interest
  227.                         CurrentInterest = CurrentCapital * (.Interest.GetValue() / 100)
  228.                         'Adjust for rounding errors
  229.                         IF CurrentInterest * 100 - INT(CurrentInterest * 100) > 0 THEN
  230.                             CurrentInterest = INT(CurrentInterest * 100) / 100
  231.                         ENDIF
  232.                         CurrentCapital = CurrentCapital + CurrentInterest
  233.                         ToAdd$ = MakeString (CurrentInterest)
  234.                         .InterestList.AddItem ToAdd$
  235.                         
  236.                         'Resulting capital
  237.                         ToAdd$ = MakeString (CurrentCapital)
  238.                         .CapitalAfterList.AddItem ToAdd$
  239.                         Period% = Period% + 1
  240.                     LOOP                
  241.                 ENDIF
  242.                 'Reset the number of periods
  243.                 .Periods.SetValue Period% - 1
  244.             ENDIF
  245.         ENDIF
  246.     END WITH
  247. END SUB
  248.  
  249.