美文网首页ABAP开发
ABAP循环结构练习题

ABAP循环结构练习题

作者: 扇动蝴蝶的翅膀 | 来源:发表于2019-04-14 10:47 被阅读0次
      1. 求100以内的所有素数。
    DATA: num type i VALUE 2,  "1既不是素数也不是合数
          cyc TYPE i.
    WHILE num <= 100.
      cyc = 2.
     WHILE cyc < num.
       IF num mod cyc = 0.
         EXIT.              "跳出while循环,因为他不是素数
       ENDIF.
       cyc = cyc + 1.
     ENDWHILE.
    
     IF num = cyc.               
       WRITE:/ '100以内的素数有:',num.
     ENDIF.
    num = num + 1.
    ENDWHILE.
    
      1. 局部类型,只能本程序使用。
    TYPES: BEGIN OF STUDENT,
                NO(14) TYPE N,
                NAME(20) TYPE C,
                BIRTH TYPE D,
           END OF STUDENT.
    DATA: LI TYPE STUDENT,
          SI TYPE STUDENT.
    LI-NO = '16610413151301'.
    LI-NAME = '张三'.
    LI-BIRTH = '20190327'.
    SI-NO = '26610413151301'.
    SI-NAME = '李四'.
    SI-BIRTH = '10190327'.
    WRITE:/ LI,/ LI-BIRTH,/ SI,/ SI-BIRTH.
    
      1. 计算自己的实际年龄
    REPORT ZABAP1903_07BASIC02.
    
    *parameters :实现用户自己输入
    parameters y0 type i.
    parameters m0 type i.
    parameters d0 type i.
    parameters y1 type i.
    parameters m1 type i.
    parameters d1 type i.
    *start-of-selection和end-of-selection是标准的逻辑流,搜索开始和搜索结束
    start-of-selection.
      data: day type i,
            month type i,
            year type i.
    *日减日,月减月,年减年,日减日为负向月借1,月减月为负也向年借1
      day = d1 - d0.
      if day < 0.
        m1 = m1 - 1.
      endif.
      month = m1 - m0.
      if month < 0.
        y1 = y1 - 1.
      endif.
      year = y1 - y0.
    end-of-selection.
      write:/ year.
    
      1. 输出每月的天数
    *能被4整出且不能被400整出或能被400整出的是闰年。
    PARAMETERS: year TYPE i,
                month TYPE i.
    CASE month.
      WHEN 2.
        IF year mod 4 = 0 and year mod 100 <> 0 or year mod 400 = 0 .
          WRITE:/ '29'.
        ELSE.
            WRITE:/ '28'.
        ENDIF.
       WHEN 4 or 6 or 9 or 11.
          WRITE:/ '30'.
       WHEN 1 or 3 or 5 or 7 or 8 or 10 or 12.
          WRITE:/ '31'.
       WHEN OTHERS.
         WRITE:/ '月份输入误'.
    ENDCASE.
    
      1. 计算最佳情侣身高
    REPORT ZABAP1903_07YUJUC07.
    
    PARAMETERS: sex type c,                  "当c类型长度为为1时,可以不用指定,默认值即可
                heigh  type p DECIMALS 2.    "p类型可以不用指定长度
    DATA: result type p DECIMALS 2,
         info(30) type c value '请输入正确的身高和性别',
         around type c.
    IF sex = 'F' and  1 <= heigh and heigh <= 3 .
      result = heigh * '1.09'.
      WRITE:/ result.
    elseif sex = 'M' and 1 <= heigh and heigh <= 3.
      result = heigh / '1.09' .
      WRITE:/ result.
    else.
      WRITE:/ info.
    ENDIF.
    
      1. 字符串处理
    REPORT ZABAP1903_07STRINGC05.
    *大小写转换
    PARAMETERS: letter(5) type c.
    TRANSLATE letter TO UPPER CASE.
    WRITE:/ letter.
    
    * 输出字符串长度
    * 当测试输入空格的时候,空格位于字符串后面时,忽略空格。
    PARAMETERS: letter_a(10) type c.
    DATA: stren type i.
    stren = STRLEN( letter_a ).
    WRITE:/ stren.
    
    *合并必须是字符类型,例:c、n
    PARAMETERS: sh(5) type c,
                ch(5) type c,
                ph(5) type c.
    DATA: result(20) type c.
    CONCATENATE sh ch ph into result SEPARATED BY '- '.
    WRITE:/ result.
    
    *分割字符串
    PARAMETERS: ca(15) type c.
    DATA: seg(1) type c VALUE '%',
          data1(5) type c,
          data2(5) type c.
    SPLIT ca AT seg INTO data1 data2.
    WRITE:/ data1, data2.
    
    
    data: a type f value '2.3'.
    WRITE:/ a.
    
      1. 求出[m,n]的和。
    PARAMETERS: m TYPE i,
                n type i.
    DATA: v TYPE i,
          sum TYPE i VALUE 0.
    "定义变量的目的是为了不改变m的值,即不改变求和范围,防止后面要用到最初的m值。
    v = m.
    IF m <= n.
      WHILE v <= n.
      sum = sum + v.   "
      v = v + 1.
      ENDWHILE.
      WRITE:/ sum.
    ELSE.
      WRITE:/ '输入有误!'.
    ENDIF.
    
      1. 1~100的和
    REPORT ZABAP1903_07WHILE.
    DATA: num TYPE i VALUE 1,
          sum type i VALUE 0.
    WHILE num <= 100.
      sum = sum + num.
      num = num + 1.
    ENDWHILE.
    WRITE:/ sum.
    
      1. 求出所有的水仙花数
    * 水仙花数:是一个三位数其各位数字的立方和等于该数本身。例如:3^3 + 7^3+ 0^3 = 370
    * 1000以内的水仙花数
    
    DATA: num type i value 100,
          g_num type i,
          s_num type i,
          b_num type i.
    WHILE num < 1000 and num >= 100.
      b_num = num div 100.
    s_num = num mod 100 div 10.
    g_num = num mod 100 mod 10.
    
      IF num = b_num ** 3 + s_num ** 3 + g_num ** 3.
      WRITE:/ num.
      ENDIF.
      num = num + 1.
    
    ENDWHILE.
    
      1. 求使整数数列1,2,3,……,n的和不大于6000的最大正整数n
    *求使整数数列1,2,3,……,n的和不大于6000的最大正整数n.  [1,n]  累加公式 n(n + 1) / 2
    *DIV:整除忽略余数
    *MOD:取模
    */: 除法
    
    *第一种方法
    DATA: n TYPE i VALUE 0,
          sum TYPE i VALUE 0.
    WHILE sum <= 6000.
      n = n + 1.
      sum = sum + n.
    ENDWHILE.
    WRITE:/ n,sum.
    
    *第二种方法
    *DATA: n TYPE i VALUE 1,
    *      sum TYPE i VALUE 0.
    *WHILE n > 0.
    *  sum = n * ( n + 1 ) / 2.
    *  IF sum > 6000.
    *    exit.
    *  ENDIF.
    *  n = n + 1.
    *ENDWHILE.
    *WRITE:/ 'n:',n,/ 'sum:', sum.
    
      1. 求(100,1000)之间所有的能被3整除的偶数的和
    求(100,1000)之间所有的能被3整除的偶数的和。
    DATA: num TYPE i VALUE 100,
          ou_num TYPE i VALUE 0,
          sum TYPE i VALUE 0.
    WHILE: num >= 100 and num <= 1000.
      IF num mod 3 = 0 and num mod 2 = 0.
        WRITE:/ 'num:',num.
       sum = sum + num.
      ENDIF.
    num = num + 1.
    ENDWHILE.
    WRITE:/ 'sum:',sum.
    
      1. 按照升序显示出小于输入值的所有正偶数
    *5、按照升序显示出小于输入值的所有正偶数
    PARAMETERS: num TYPE c LENGTH 4.
    DATA: con TYPE i VALUE 0.
    WHILE con < num.
      IF con > 0 and con mod 2 = 0.
        WRITE:/ con.
      ENDIF.
      con = con + 1.
    ENDWHILE.
    
      1. 显示输入的整数值的所有约数
    *显示输入的整数值的所有约数
    *如果一个整数能被另一个整数整除,那么第二个整数就是第一个整数的约数
    PARAMETERS: num TYPE i.
    DATA: con TYPE i VALUE 1.
    WHILE con <= num.
      IF num mod con = 0.
          WRITE:/ con.
      ENDIF.
      con = con + 1.
    ENDWHILE.
    

    相关文章

      网友评论

        本文标题:ABAP循环结构练习题

        本文链接:https://www.haomeiwen.com/subject/dfhrwqtx.html