您好,登錄后才能下訂單哦!
小編給大家分享一下怎么在ABAP里用函數式編程思想打印出非波拉契Fibonacci,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在ABAP里也有很多種方式實現這個需求。
下面這個report分別用遞歸和ABAP internal table的方式實現了非波拉契數列的打印。
REPORT Z_FIBO. PARAMETERS: N type i, v1 RADIOBUTTON GROUP v default 'X', v2 RADIOBUTTON GROUP v. data: f type i, t type i. data: product_guid type comm_product-product_guid. get run time field t.case 'X'. when v1\. perform fibonacci using n changing f. when v2\. perform fibonacci_2 using n changing f. endcase. write: / 'Fibonacci(', n, ') =', f. get run time field t. write: / 'Runtime', t, 'microseconds'. *&---------------------------------------------------------------------* *& Form fibonacci *&---------------------------------------------------------------------* form fibonacci using in type i changing fib type i. data: f_1 type i, f_2 type i, n_1 type i, n_2 type i.case in. when 0\. fib = 1. when 1\. fib = 1. when others. n_1 = in - 1. n_2 = in - 2. perform fibonacci using n_1 changing f_1. perform fibonacci using n_2 changing f_2. fib = f_1 + f_2. endcase. endform. "fibonacci *&---------------------------------------------------------------------* *& Form fibonacci_2 *&---------------------------------------------------------------------* form fibonacci_2 using in type i changing fib type i. data: f_1 type i, f_2 type i, n_1 type i, n_2 type i, l type i. data: fibo type table of i. append 1 to fibo. " fibonacci(0) append 1 to fibo. " fibonacci(1) n_1 = 1. n_2 = 2. l = in - 1. do l times. read table fibo index n_1 into f_1. read table fibo index n_2 into f_2. fib = f_1 + f_2. add 1 to n_1\. add 1 to n_2. append fib to fibo. enddo. endform. "fibonacci_2
以上兩種解決方案相對來說都比較傳統,再來看看使用ABAP 7.40提供的新關鍵字COND實現的非波拉契數列打印:
REPORT z.CLASS lcl_fibonacci DEFINITION. PUBLIC SECTION. TYPES: zint_tab TYPE TABLE OF int4 WITH EMPTY KEY. METHODS fibonacci IMPORTING !n TYPE i RETURNING VALUE(fib_numbers) TYPE zint_tab. ENDCLASS.CLASS lcl_fibonacci IMPLEMENTATION. METHOD fibonacci. fib_numbers = COND #( WHEN n = 0THEN VALUE #( ( |0| ) )WHEN n = 1THEN VALUE #( ( |0| ) ( |1| ) )ELSE VALUE #( LET fn1 = fibonacci( n - 1 )x = fn1[ lines( fn1 ) ] y = fn1[ lines( fn1 ) - 1 ] IN ( LINES OF fn1 ) ( x + y ) ) ). ENDMETHOD. ENDCLASS. START-OF-SELECTION. cl_demo_output=>display( NEW lcl_fibonacci( )->fibonacci( 10 ) ).
打印輸出:
以上是“怎么在ABAP里用函數式編程思想打印出非波拉契Fibonacci”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。