1 1 ....5....0....5 D +123$$$$$ d +78$$$$$ c $$$$$$$$$$ P $$$$$ +50 D +100$$$$$ D +10$$$$$ p $$$$$ +120 C $$$$$$$$$$ P $$$$$ +50 D +150$$$$$
If the first character is a D or d, this is a deposit line. In this case, positions 6 to 10 contain the deposit amount and positions 11 to 15 contain $$$$$ so that nobody can overwrite this field. If the first character is a P or p, this is a payment line. In this case, positions 6 to 10 contain $$$$$ and positions 11 to 15 contain the payment amount. If the first character is a C or c, then positions 6 to 15 contain $$$$$$$$$$.
Write a Fortran program that reads in these input lines and prints the following table.
1 1 2 2 3 3 4 ....5....0....5....0....5....0....5....0 Check Book Reference Listing ============================ Count Payment Deposit Balance ----- ------- ------- ------- 1 ******* +123 +123 2 ******* +78 +201 ----------------------------------- 3 +0 +201 +201 ----------------------------------- 4 +50 ******* +151 5 ******* +100 +251 6 ******* +10 +261 7 +120 ******* +141 ----------------------------------- 8 +170 +311 +141 ----------------------------------- 9 +50 ******* +91 10 ******* +150 +241 ----------------------------------- Total +220 +461 +241
This table has four columns. The first column prints the transaction numbers. The second column contains the payment amount. It should contain the amount of payment if that transaction is a payment line; otherwise, it should contain *******. The third column contains the deposit amount. It should contain the amount of deposit if that transaction is a deposit line; otherwise, it should contain *******. The fourth column contains the balance after each transaction is made.
If the input line is a C or c, then your program should print a dashed line, followed by the total payment and total deposit up to this point, and the current balance, followed by another dashed line. At the end of the report, a dashed line followed by the total payment, total deposit and current balance must be printed.
Click here to download this program.PROGRAM CheckBook IMPLICIT NONE CHARACTER(LEN=1) :: Code CHARACTER(LEN=15) :: Line INTEGER :: Payment, Deposit, Balance, Input INTEGER :: Count INTEGER :: Status CHARACTER(LEN=*), PARAMETER :: Filler = " *******" CHARACTER(LEN=*), PARAMETER :: DashLine = "-----------------------------------" CHARACTER(LEN=*), PARAMETER :: TitleFormat = "(1X, A/1X, A//1X, A/1X, A)" CHARACTER(LEN=*), PARAMETER :: InputLine = "(A)" CHARACTER(LEN=*), PARAMETER :: CheckLine = "(1X, A/ 1X, I5, SP, 3I10/1X, A)" CHARACTER(LEN=*), PARAMETER :: PaymentLine = "(1X, I5, SP, I10, A, I10)" CHARACTER(LEN=*), PARAMETER :: DepositLine = "(1X, I5, A, SP, 2I10)" CHARACTER(LEN=*), PARAMETER :: LastLine = "(1X, A/ 1X, A, SP, 3I10)" WRITE(*,TitleFormat) "Check Book Reference Listing", & "============================", & "Count Payment Deposit Balance", & "----- ------- ------- -------" Payment = 0 Deposit = 0 Balance = 0 Count = 0 DO READ(*,InputLine,IOSTAT=Status) Line IF (Status < 0) EXIT Code = Line(1:1) Count = Count + 1 SELECT CASE (Code) CASE("C", "c") WRITE(*,CheckLine) DashLine, Count, Payment, Deposit, Balance, DashLine CASE("P", "p") READ(Line,"(T11,I5)") Input Payment = Payment + Input Balance = Balance - Input WRITE(*,PaymentLine) Count, Input, Filler, Balance CASE("D", "d") READ(Line,"(T6,I5)") Input Deposit = Deposit + Input Balance = Balance + Input WRITE(*,DepositLine) Count, Filler, Input, Balance END SELECT END DO WRITE(*,LastLine) DashLine, "Total", Payment, Deposit, Balance END PROGRAM CheckBook
The output of the program is:1 1 ....5....0....5 D +123$$$$$ d +78$$$$$ c $$$$$$$$$$ P $$$$$ +50 D +100$$$$$ D +10$$$$$ p $$$$$ +120 C $$$$$$$$$$ P $$$$$ +50 D +150$$$$$
1 1 2 2 3 3 4 ....5....0....5....0....5....0....5....0 Check Book Reference Listing ============================ Count Payment Deposit Balance ----- ------- ------- ------- 1 ******* +123 +123 2 ******* +78 +201 ----------------------------------- 3 +0 +201 +201 ----------------------------------- 4 +50 ******* +151 5 ******* +100 +251 6 ******* +10 +261 7 +120 ******* +141 ----------------------------------- 8 +170 +311 +141 ----------------------------------- 9 +50 ******* +91 10 ******* +150 +241 ----------------------------------- Total +220 +461 +241
That is, the A is for reading in the first character, 4X to skip the next four spaces, and 2I5 for reading in the deposit and payment values. This seems a reasonable format; however, it just does not work. Let us examine the first input line. D is read into a CHARACTER variable, four spaces are skipped by 4X and the first I5 reads in a deposit value. But, the second I5 will cause a problem because the next five positions contain $$$$$ which do not form a valid integer. Similar reason holds true for a payment line. For a line with C or c in the first position, both I5s will cause the same problem. So, the above format just does not work.(A, 4X, 2I5)
After this, we have the complete line in our hand so that we could examine it over and over again without getting into format troubles.CHARACTER(LEN=15) :: Line CHARACTER(LEN=*), PARAMETER :: InputLine = "(A)" READ(*,InputLine,IOSTAT=Status) Line
Code = Line(1:1) SELECT CASE (Code) CASE("C", "c") this is a balance line CASE("P", "p") this is a payment line CASE("D", "d") this is a deposit line END SELECT
Thus, the deposit value is read into variable Input. Note that this value is read in from the input line which is now stored in Line rather than directly from the keyboard input.READ(Line,"(T6,I5)") Input