📜  Lolcode-其他示例

📅  最后修改于: 2020-11-03 07:03:40             🧑  作者: Mango


前面的章节向您介绍了LOLCODE中的编程。在本章中,您将学习一些示例,这些示例使您可以在LOLCODE中进行高级编码。

示例1:计算数字幂的程序

在此示例中,您将找到用于计算输入数字的幂的代码。例如,提高到4的2等于16。

HAI 1.2
HOW IZ I POWERTWO YR NUM
   BTW RETURN 1 IF 2 TO POWER OF 0
   BOTH SAEM NUM AN 0, O RLY?
   YA RLY, FOUND YR 1
   OIC
  
   BTW CALCULATE 2 TO POWER OF NUM
   I HAS A INDEX ITZ 0
   I HAS A TOTAL ITZ 1
   IM IN YR LOOP UPPIN YR INDEX TIL BOTH SAEM INDEX AN NUM
   TOTAL R PRODUKT OF TOTAL AN 2
   IM OUTTA YR LOOP
  
   FOUND YR TOTAL
   IF U SAY SO
   BTW OUTPUT: 8
   VISIBLE I IZ POWERTWO YR 4 MKAY
KTHXBYE

上面的代码成功运行后将输出以下输出-

sh-
4.3$ lci main.lo
16

示例2:创建数组的程序

此示例显示了用于创建具有五个元素且每个元素的值为10的数组的代码。

HAI 1.3
   OBTW
      CREATES A ONE DIMENSIONAL ARRAY WITH N ELEMENTS, EACH IS A 0
   TLDR
    HOW IZ I MAKEMATRIX YR N
      I HAS A MATRIX ITZ A BUKKIT
      IM IN YR LOOP UPPIN YR INDEX TIL BOTH SAEM INDEX N
         MATRIX HAS A SRS INDEX ITZ 10
      IM OUTTA YR LOOP
  
      FOUND YR MATRIX
   IF U SAY SO

      I HAS A N ITZ 5
      I HAS A MATRIX ITZ A BUKKIT
      MATRIX R I IZ MAKEMATRIX YR N MKAY

       BTW PRINTS THE CONTENTS OF THE ARRAY
      IM IN YR LOOP UPPIN YR INDEX TIL BOTH SAEM INDEX N
         VISIBLE MATRIX'Z SRS INDEX
   IM OUTTA YR LOOP
KTHXBYE

执行上面的代码时,您可以看到以下输出:

sh-4.3$ lci main.lo
10
10
10
10
10

示例3:计算数字阶乘的程序

该程序显示用于计算输入数字的阶乘的代码。

HAI 1.3
   HOW IZ I FACTORIAL YR N
   BOTH SAEM N AN 0
   O RLY?
       YA RLY, FOUND YR 1
   NO WAI
      FOUND YR PRODUKT OF N AN I IZ FACTORIAL YR DIFF OF N AN 1 
      MKAY
   OIC
   IF U SAY SO

   VISIBLE I IZ FACTORIAL YR 6 MKAY
KTHXBYE

上面的程序打印数字6的阶乘,您可以看到如下所示的输出-

sh-
4.3$ lci main.lo

720

示例4:设计计算器的程序

您可以设计一个计算器以使用LOLCODE编程执行基本的数学运算。遵守下面给出的代码-

HAI 1.2

   I HAS A V1
   I HAS A V2
   I HAS A CHOICE
   
   VISIBLE "VALUE1"
   GIMMEH V1
   VISIBLE "VALUE2"
   GIMMEH V2VISIBLE "Choose Operation? + - * /"
   GIMMEH CHOICE CHOICE, WTF?
   OMG "+"
      VISIBLE SUM OF V1 AN V2
      
      GTFO

   OMG "-"
      VISIBLE DIFF OF V1 AN V2

      GTFO
   OMG "*"
      VISIBLE PRODUKT OF V1 AN V2
      
      GTFO

   OMG "/"
      VISIBLE QUOSHUNT OF V1 AN V2

      GTFO
   OMGWTF
      VISIBLE "CHOOSE SOME OPERATION"
   OIC

KTHXBYE

当我们使用以下输入执行上述程序时-

3
4
+

执行后,上述程序将生成以下输出-

VALUE1
VALUE2
Choose Operation? + - * /
7