Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7188808
  • 博文数量: 655
  • 博客积分: 10264
  • 博客等级: 上将
  • 技术积分: 8278
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-04 17:47
个人简介

ABAP顾问

文章分类

全部博文(655)

文章存档

2017年(2)

2014年(8)

2013年(3)

2012年(2)

2011年(18)

2010年(102)

2009年(137)

2008年(274)

2007年(134)

分类:

2008-04-17 14:06:07

例1.求一个节点下面所有节点的某一字段之和
*&---------------------------------------------------------------------*
*& Report  Z_BARRY_RECURSION2                                          *
*&                                                                     *
*&---------------------------------------------------------------------*
*& 求一个节点下面所有节点的某一字段之和                                *
*&---------------------------------------------------------------------*
 
AA -- BBB -- DDDD
   |      |
   |      -- EEEE
   |     
   |- CCC -- FFFF
   |      |
   |      -- GGGG
   |
   |- HHH
 
REPORT  z_barry_recursion2             .
DATA: BEGIN OF itab OCCURS 0 ,
        node(10) TYPE c,
        parentnode(10) TYPE c ,
        folder TYPE c ,
        menge TYPE i,
      END OF itab.
DATA: itab2 LIKE itab OCCURS 0 WITH HEADER LINE.
DATA: total TYPE i.
 
START-OF-SELECTION.
  itab-menge = 1.
  itab-node = 'AA'.
  itab-parentnode = ''.
  itab-folder = 'X'.
  APPEND itab.
  itab-menge = 2.
  itab-node = 'BBB'.
  itab-parentnode = 'AA'.
  itab-folder = 'X'.
  APPEND itab.
  itab-node = 'CCC'.
  itab-parentnode = 'AA'.
  itab-folder = 'X'.
  APPEND itab.
  itab-node = 'HHH'.
  itab-parentnode = 'AA'.
  itab-folder = ''.
  APPEND itab.
  itab-menge = 3.
  itab-node = 'DDDD'.
  itab-parentnode = 'BBB'.
  APPEND itab.
  itab-node = 'EEEE'.
  itab-parentnode = 'BBB'.
  APPEND itab.
  itab-node = 'FFFF'.
  itab-parentnode = 'CCC'.
  APPEND itab.
  itab-node = 'GGGG'.
  itab-parentnode = 'CCC'.
  APPEND itab.
 
  PERFORM getsum USING 'AA' total.
  WRITE total .
  SKIP 2.
 
  PERFORM gettable TABLES itab2
                   USING 'AA' .
  LOOP AT itab2.
    WRITE : / itab2-node ,itab2-parentnode,itab2-menge.
  ENDLOOP.
 
*&---------------------------------------------------------------------*
*&      Form  getsum
*&---------------------------------------------------------------------*
FORM getsum USING node sum.
  LOOP AT itab WHERE parentnode = node .
    IF itab-folder = ''.
      sum = sum + itab-menge .
    ENDIF.
    PERFORM getsum USING itab-node sum.
  ENDLOOP.
ENDFORM .                   "getsum
 
*&---------------------------------------------------------------------*
*&      Form  gettable
*&---------------------------------------------------------------------*
FORM gettable TABLES outtab
              USING node .
  LOOP AT itab WHERE parentnode = node.
    IF itab-folder = ''.
      APPEND itab TO outtab.
    ENDIF.
    PERFORM gettable TABLES outtab USING itab-node.
  ENDLOOP.
ENDFORM.                    "gettable
 
 
例2.求一个日期的前N个月的日期
REPORT  z_barry_recursion   .
DATA: old_date LIKE sy-datum ,
      new_date LIKE sy-datum ,
      back TYPE numc3 .
 
old_date = '20080417'.
back = 6.
 
PERFORM back_months USING old_date back
                    CHANGING new_date.
WRITE new_date.

*&---------------------------------------------------------------------*
*&      Form  GET_BOM
*&---------------------------------------------------------------------*
FORM back_months USING value(currdate) value(backmonths)
                 CHANGING newdate.
  DATA: year(4)    TYPE c,
        tmonths TYPE numc3,
        t1months TYPE numc3,
        day(2)     TYPE c.
  MOVE: currdate(4) TO year,
        currdate+4(2) TO tmonths,
        currdate+6(2) TO day.
  IF tmonths <= backmonths.
    currdate(4) = currdate(4) - 1.
    currdate+4(2) = '12'.
    backmonths = backmonths - tmonths.
    PERFORM back_months USING currdate backmonths
                        CHANGING newdate.
  ELSE.
    newdate(4) = currdate(4).
    newdate+6(2) = currdate+6(2).
    newdate+4(2) = currdate+4(2) - backmonths.
  ENDIF.
ENDFORM.                    "GET_BOM
阅读(6405) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~