home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
chkbook.seq
< prev
next >
Wrap
Text File
|
1990-04-16
|
21KB
|
485 lines
\ File Name : CHKBOOK.SEQ
\ Program Name : Check Book
\ Author : Jack W. Brown
\ Original Date: July 25, 1988 for PF Forth
\ Last Modified: April 16, 1990 for F-PC 3.5
\ Function : Balance your check book
\ Required :
\ Support Files: DMATH.SEQ from provided with Lesson 5
\ : JBINPUT.SEQ from JBINPUT.ZIP
\ Usage : Fload file and type MAIN, follow instructions
\ : it won't break.
\ Overview : A simple menu driven program that illustrates how
\ : you can program dollars and cents using single
\ : integers. You must also have your ANSI.SYS driver
\ : installed in your CONFIG.SYS file.
\ : BIG BOX COMMENTS JUST LIKE " C " PROGRAMMERS USE!!
\ Revision History
\ JWB 25-07-88 Original PF-Forth version created.
\ JWB 07-03-89 Modified for F-PC 2.25 and Tutorial
\ JWB 16-04-90 Modified for F-PC 3.5
\ Re define comment to end of line so we can use "C" type comments.
: /* [COMPILE] \ ; IMMEDIATE
/* ************************************************************ */
/* */
/* For F-PC 2.25 with ANSI.SYS installed in your CONFIG.SYS */
/* */
/* Program: Checkbook - Implement simple checkbook program. */
/* FORTH version. */
/* Date: July 25, 1988 */
/* */
/* ************************************************************ */
\ FLOAD DMULDIV.SEQ
\ FLOAD DMATH.SEQ
\ FLOAD JBINPUT.SEQ
VARIABLE BAL_DOLLARS /* Checkbook balance dollar amount */
VARIABLE BAL_CENTS /* Checkbook balance cents amount */
VARIABLE TR_DOLLARS /* Transaction dollar amount */
VARIABLE TR_CENTS /* Transaction cents amount */
VARIABLE VALID /* Valid return code from scanf */
VARIABLE OLD_DOLLARS /* Initial dollar balance */
VARIABLE OLD_CENTS /* Initial cents balance */
VARIABLE CHK_DOLLARS /* Total check dollars */
VARIABLE CHK_CENTS /* Total check cents */
VARIABLE CHK_COUNT /* Number of checks this session */
VARIABLE DEP_COUNT /* Number of deposits this session */
VARIABLE DEP_DOLLARS /* Total deposit dollars */
VARIABLE DEP_CENTS /* Total deposit cents */
VARIABLE TEST
/* ************************************************************ */
/* */
/* Function: scan_for_int - scan input stream for a single */
/* integer. */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: SCAN_FOR_INT(-- n ) */
/* */
/* ************************************************************ */
: SCAN_FOR_INT ( -- num ) #IN ;
/* ************************************************************ */
/* Function: HBAR Draws a horizontal bar on display */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: HBAR ( n --) */
/* */
/* ************************************************************ */
: HBAR ( n -- )
0 DO ASCII = EMIT LOOP CR ;
/* ************************************************************ */
/* Function: CLR_HBAR Clear screan and draw horizontal bar */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: CLR_HBAR ( n -- ) */
/* */
/* ************************************************************ */
: CLR_HBAR ( n -- )
27 EMIT ." [2J" CR HBAR ;
/* ************************************************************ */
/* Function: GET_DOLLARS Fetch dollars with error checking. */
/* */
/* Date: July 22, 1988 */
/* */
/* Interface: GET_DOLLARS ( -- n ) */
/* */
/* ************************************************************ */
: GET_DOLLARS ( -- n )
BEGIN
27 EMIT ." [K Dollars: "
SCAN_FOR_INT CR DUP
9999 > OVER 0 < OR
WHILE
DROP 27 EMIT ." [1;A"
REPEAT ;
/* ************************************************************ */
/* Function: GET_CENTS Fetch cents with error checking. */
/* */
/* Date: July 22, 1988 */
/* */
/* Interface: GET_CENTS ( -- n ) */
/* */
/* ************************************************************ */
: GET_CENTS ( -- n )
BEGIN
27 EMIT ." [K Cents: "
SCAN_FOR_INT CR DUP
99 > OVER 0 < OR
WHILE
DROP
27 EMIT ." [1;A"
REPEAT ;
/* ************************************************************ */
/* */
/* Function: ROUND - Roll cents into dollars. */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: ROUND ( dollars cents -- dollars' cents' ) */
/* */
/* ************************************************************ */
: ROUND ( dollars cents -- dollars' cents')
DUP >R
100 / +
R> 100 MOD ;
/* ************************************************************ */
/* */
/* Function: ADD_TO_BAL - Add dollars, cents amount to balance */
/* */
/* Date: July 22, 1988 */
/* */
/* Interface: ADD_TO_BAL ( dollars cents -- ) */
/* dollars: dollar amount to be added */
/* cents : cents amount to be added */
/* */
/* ************************************************************ */
: ADD_TO_BAL ( dollars cents -- )
BAL_CENTS +!
BAL_DOLLARS +!
BAL_DOLLARS @ BAL_CENTS @ ROUND
BAL_CENTS ! BAL_DOLLARS ! ;
/* ************************************************************ */
/* */
/* Function: SUB_FROM_BAL - subtract dollars, cents amount */
/* from balance. */
/* */
/* Date: July 22, 1988 */
/* */
/* Interface: SUB_FROM_BAL ( dollars cents -- flag) */
/* dollars : dollar amount to be subtracted */
/* cents : cents amount to be subtracted */
/* flag = false if illeagal transaction. */
/* */
/* ************************************************************ */
VARIABLE D
VARIABLE C
: SUB_FROM_BAL ( dollars cents -- flag )
BAL_DOLLARS @ D !
BAL_CENTS @ C !
DUP C @ >
IF -1 D +! 100 C +! THEN
NEGATE C +! NEGATE D +!
D @ 0 <
IF 60 CLR_HBAR
7 DUP DUP EMIT EMIT EMIT
." You are trying to overdraw your account. You must" CR
." first make a deposit before trying to write a cheque" CR
." this large." CR
60 HBAR
FALSE
ELSE C @ BAL_CENTS ! D @ BAL_DOLLARS ! TRUE
THEN ;
: $XX.XX ( dollars cents -- )
0 <# # # ASCII . HOLD DROP #S ASCII $ HOLD #> TYPE ;
/* ************************************************************ */
/* */
/* Function WRITE_A_CHECK - Calculate new balance */
/* after check is written */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: WRITE_A_CHECK ( -- ) */
/* */
/* */
/* Notes: Calls SUB_FROM_BAL to perform the fixed point */
/* calculations. */
/* */
/* ************************************************************ */
: WRITE_A_CHECK ( -- )
40 CLR_HBAR
." Enter the amount of the check:" CR
40 HBAR
GET_DOLLARS GET_CENTS ROUND
OVER OVER
TR_CENTS ! TR_DOLLARS !
40 HBAR
SUB_FROM_BAL
IF 1 CHK_COUNT +!
TR_DOLLARS @ CHK_DOLLARS @ +
TR_CENTS @ CHK_CENTS @ +
ROUND
CHK_CENTS ! CHK_DOLLARS !
." After writing a check for: "
TR_DOLLARS @ TR_CENTS @ $XX.XX CR
." your new balance comes to: "
BAL_DOLLARS @ BAL_CENTS @ $XX.XX CR
40 HBAR
THEN ;
/* ************************************************************ */
/* */
/* Function: MAKE_A_DEPOSIT - calculates new balance after */
/* a deposit is made. */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: MAKE_A_DEPOSIT ( --) */
/* */
/* Notes: Calls add_to_bal to perform fixed point calculations. */
/* */
/* ************************************************************ */
: MAKE_A_DEPOSIT ( -- )
40 CLR_HBAR
." Enter the amount of the deposit: " CR
40 HBAR
GET_DOLLARS TR_DOLLARS !
GET_CENTS TR_CENTS !
1 DEP_COUNT +!
TR_DOLLARS @ DEP_DOLLARS @ +
TR_CENTS @ DEP_CENTS @ +
ROUND DEP_CENTS ! DEP_DOLLARS !
40 HBAR
TR_DOLLARS @ TR_CENTS @ ADD_TO_BAL
." After a deposit of "
TR_DOLLARS @ TR_CENTS @ $XX.XX CR
." your new balance comes to: "
BAL_DOLLARS @ BAL_CENTS @ $XX.XX CR
40 HBAR ;
/* ************************************************************ */
/* */
/* Function: NET_CHANGE Displays net change from session */
/* start. */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: NET_CHANGE ( -- ) */
/* */
/* ************************************************************ */
VARIABLE DIF_DOLLARS
VARIABLE DIF_CENTS
: NET_CHANGE ( -- )
BAL_DOLLARS @ DIF_DOLLARS !
BAL_CENTS @ DIF_CENTS !
/* 40 CLR_HBAR
OLD_CENTS @ BAL_CENTS @ >
IF -1 DIF_DOLLARS +!
100 DIF_CENTS +!
THEN
OLD_DOLLARS @ NEGATE DIF_DOLLARS +!
OLD_CENTS @ NEGATE DIF_CENTS +!
40 CLR_HBAR
." Net change this session: "
DIF_DOLLARS @ 0 <
IF 100 DIF_CENTS @ - DIF_CENTS !
1 DIF_DOLLARS @ + NEGATE DIF_DOLLARS !
ASCII - EMIT
THEN
DIF_DOLLARS @ DIF_CENTS @ $XX.XX CR
40 HBAR ;
/* ************************************************************ */
/* */
/* Function: TOT_CHECKS Displays total checks written */
/* this session. */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: TOT_CHECKS ( -- ) */
/* */
/* ************************************************************ */
: TOT_CHECKS ( -- )
70 CLR_HBAR
CHK_COUNT @ 0=
IF
." There have been no checks written so far this session "
." so the total is: "
ELSE CHK_COUNT @ 1 =
IF
." Only one check has been written so far this session "
." for a total of: "
ELSE
." There were " CHK_COUNT @ .
." checks written so far this session "
." that total: "
THEN
THEN
CHK_DOLLARS @ CHK_CENTS @ $XX.XX CR
70 HBAR ;
/* ************************************************************ */
/* */
/* Function: TOT_DEPOSIT Total deposits this session */
/* */
/* Date: July 25, 1988 */
/* */
/* Interface: TOT_DEPOSIT ( -- ) */
/* */
/* ************************************************************ */
: TOT_DEPOSIT ( -- )
70 CLR_HBAR
DEP_COUNT @ 0=
IF
." There have been no deposits so far this session "
." so the total is: "
ELSE DEP_COUNT @ 1 =
IF
." Only one deposite has been made so far this session "
." for a total of: "
ELSE
." There were " DEP_COUNT @ .
." deposits made so far this session "
." that total: "
THEN
THEN
DEP_DOLLARS @ DEP_CENTS @ $XX.XX CR
70 HBAR ;
/* ************************************************************ */
/* Function: AVERAGE Reports average check written this */
/* session */
/* Date: July 25, 1988 */
/* */
/* Interface: AVERAGE ( -- ) */
/* */
/* ************************************************************ */
CREATE MILLS 4 ALLOT
VARIABLE ADOLLARS
VARIABLE ACENTS
: AVERAGE ( -- )
CHK_COUNT @ 0=
IF
50 CLR_HBAR
." You have not written any checks this session." CR
50 HBAR
ELSE
CHK_DOLLARS @ 1000 UM*
CHK_CENTS @ 10 * 0 D+
CHK_COUNT @ 0 D/
5 0 D+ 10 0 D/
OVER OVER 100 0 D/ DROP ADOLLARS !
100 0 DMOD DROP ACENTS !
60 CLR_HBAR
." For this session the average check written was: "
ADOLLARS @ ACENTS @ $XX.XX CR
60 HBAR
THEN ;
/* ************************************************************ */
/* */
/* Function: BALANCE - Handle user menu in checkbook program. */
/* */
/* */
/* Notes: Uses a case statement to respond to choices made */
/* from a menu. */
/* */
/* ************************************************************ */
: BALANCE ( -- flag )
." You may choose one of the following:" CR CR
." (1) Write a check." CR
." (2) Make a deposit." CR
." (3) Check your balance." CR
." (4) Net change this session." CR
." (5) Total checks this session." CR
." (6) Total deposits this session." CR
." (7) Average check written this session." CR
." (8) Exit." CR
." (9) Reinitialize." CR CR
." Enter your choice by typing the corresponding number." CR
SCAN_FOR_INT CR
1 OVER = IF DROP WRITE_A_CHECK 1 ELSE
2 OVER = IF DROP MAKE_A_DEPOSIT 1 ELSE
3 OVER = IF DROP 40 HBAR
." Your current balance is: "
BAL_DOLLARS @ BAL_CENTS @ $XX.XX CR
40 HBAR 1 ELSE
4 OVER = IF DROP NET_CHANGE 1 ELSE
5 OVER = IF DROP TOT_CHECKS 1 ELSE
6 OVER = IF DROP TOT_DEPOSIT 1 ELSE
7 OVER = IF DROP AVERAGE 1 ELSE
8 OVER = IF DROP
60 CLR_HBAR
." Check Book terminated normally "
." with a balance of: "
BAL_DOLLARS @ BAL_CENTS @ $XX.XX CR
60 HBAR FAST QUIT ( CR 0 0 BDOS ) ELSE
9 OVER = IF DROP 0 ELSE
40 CLR_HBAR
7 DUP DUP EMIT EMIT EMIT
." That choice is unavailable, try again." CR
." Type 1, 2, 3, 4, 5, 6, 7, 8 or 9." CR
40 HBAR DROP 1
THEN THEN THEN THEN THEN
THEN THEN THEN THEN ;
/* ************************************************************ */
/* */
/* Function: Checkbook main function of the checkbook program. */
/* */
/* Date: July 21, 1988 */
/* */
/* Interface: int checkbook() */
/* */
/* Notes: This program will do your checkbook calculations */
/* Why would anyone use a computer to balance their checkbook? */
/* */
/* ************************************************************ */
: MAIN ( -- )
SLOW
BEGIN
BAL_DOLLARS OFF BAL_CENTS OFF TR_DOLLARS OFF TR_CENTS OFF
OLD_DOLLARS OFF OLD_CENTS OFF CHK_DOLLARS OFF CHK_CENTS OFF
DEP_COUNT OFF CHK_COUNT OFF DEP_DOLLARS OFF DEP_CENTS OFF
40 CLR_HBAR
." Welcome to your checkbook." CR
." Please enter your current balance:" CR
40 HBAR
GET_DOLLARS DUP OLD_DOLLARS ! BAL_DOLLARS !
GET_CENTS DUP OLD_CENTS ! BAL_CENTS !
40 HBAR
." Thank you. Your current balance is: "
BAL_DOLLARS @ BAL_CENTS @ $XX.XX CR
40 HBAR
BEGIN
BALANCE
0= UNTIL
AGAIN ;