如何生成包含正在进行的所有素数的文本文件4gl

时间:2016-11-21 10:23:30

标签: progress-4gl openedge

在下面的代码中,我将导入一个数字为3到99的文本文件。

我有部分编写的代码来查找给定数字的素数。

 define variable a as integer.

 define variable b as integer.

 define variable i as integer initial 0.


    INPUT FROM Value ( "C:\src\New folder\bingo.txt"). 
    repeat :

      IMPORT a. 
      b = a mod i.
      if b=0 then
      do:
        if a=i then 
        do:
          message " prime number"a view-as alert-box.
        end.
        else if a<>i then 
        do:
          i=i + 1.
          message " not prime number"a view-as alert-box.

        end.

        else if a < i then 
        do:
          message "not prime number"a view-as alert-box.   
        end.    
      end.  
      END.     
      if b<>0 then
      do:    
        i=i + 1.       
        message "b<>0"b view-as alert-box.      
      end.
      end.
   Output To Value( "C:\src\New folder\even.txt")append. 
   export a space.
   output close.
   end.   
   end.

帮助我。 编程

1 个答案:

答案 0 :(得分:2)

我认为将主要检查存储在函数或过程中会更有意义。这次优质检查不是必要的,但它适用于这些条件。

在处理输入和输出时,最好定义和使用流。

DEFINE VARIABLE a AS INTEGER     NO-UNDO.

DEFINE VARIABLE b AS INTEGER     NO-UNDO.
DEFINE STREAM strIn.
DEFINE STREAM strOut.

FUNCTION isPrime RETURNS LOGICAL (INPUT iNum AS INTEGER ):

    DEFINE VARIABLE iDiv   AS INTEGER     NO-UNDO.

    DO iDiv = 2 TO INTEGER(SQRT(iNum)):
        IF iNum MOD iDiv = 0 THEN
            RETURN FALSE.
    END.

    RETURN TRUE.
END.

OUTPUT STREAM strOut TO VALUE ("c:\temp\primes.txt").
INPUT STREAM strIn FROM Value ( "C:\temp\bingo.txt"). 
repeat :

    IMPORT STREAM strIn a. 

    IF isPrime(a) THEN DO:
        /*MESSAGE "prime number" a view-as alert-box. */
        PUT STREAM strOut UNFORMATTED a SKIP.
    END. 
    ELSE DO:
        /*MESSAGE "not prime number" a view-as alert-box. */
    END.    

END.
INPUT STREAM strIn CLOSE.
OUTPUT STREAM strOut CLOSE.