使用 cobol 和循环进行加法

Addition using cobol and loops

我正在尝试执行简单的加法,但我不确定如何在 cobol 中执行此操作。我想将最后四年的学费添加到一个变量total_tuition。但是,它只会在我的第一个循环完成后添加这个,因此给我增加了 4 次去年的学费(这不是我想要的)。我在 python 中完成了任务,以帮助阐明我要完成的任务。谢谢!

   WORKING-STORAGE SECTION.
   01 tuition pic 9(5)v99.
   01 increase pic 9(5)v99 value 00000.05. 
   01 final_tuition pic 9(5)v99. 
   01 total_tuition pic 9(5)v99 value 00000.00. 
   01 temp pic 9(7)v99.
   01 num pic 9 value 1. 
   
   PROCEDURE DIVISION.
   MAIN-PROCEDURE. 
       display 'Please enter the tuition amount: '. 
       accept tuition. 
       
       perform aLoop 10 times.
       display 'Tuition after 10 years is: $', final_tuition. 
       perform bLoop with test after until num > 6. 

       display '4-year tuition in 10 years is: $', total_tuition. 
   STOP RUN.
       aLoop. 
           multiply tuition by increase giving temp . 
           add temp to tuition giving final_tuition. 
           set tuition to final_tuition.
       bLoop. 
           add final_tuition to total_tuition giving total_tuition. 
           add 1 to num. 
           display total_tuition. 

Python 工作解决方案:

n = 0
tuition = 10000
increase = .05
temp = 0
total = 0
final = 0
i = 0
while n < 10 : 
    temp = increase * tuition 
    final = tuition + temp
    tuition = final
    if n >= 6: 
        total = final + total 
        print(i, ") total", total)
        i = i + 1
    n = n+1 
print(final, total) 

在 Python 版本中,您有一个 if 语句。在 COBOL 版本中,您有第二个循环。

试试这个代码:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
   DATA DIVISION.
   
   WORKING-STORAGE SECTION.
   01 tuition pic 9(5)v99 value 10000.
   01 increase pic 9(5)v99 value 00000.05. 
   01 final_tuition pic 9(5)v99. 
   01 total_tuition pic 9(5)v99 value 00000.00.
   01 temp pic 9(7)v99.
   01 num pic 9 value 0. 
   
   PROCEDURE DIVISION.
   MAIN-PROCEDURE.              
       perform aLoop 10 times.
       display 'Tuition after 10 years is: $', final_tuition.     
       display '4-year tuition in 10 years is: $', total_tuition. 
   STOP RUN.
       aLoop. 
            multiply tuition by increase giving temp . 
            add temp to tuition giving final_tuition. 
            set tuition to final_tuition.
            if num >= 6 then
                add final_tuition to total_tuition giving total_tuition
            end-if
            add 1 to num.

输出

Tuition after 10 years is: 288.91
4-year tuition in 10 years is: 647.68