Lolcode - 更多示例


前面的章节介绍了LOLCODE 中的编程。在本章中,您将学习一些示例,让您能够在 LOLCODE 中进行高级编码。

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

在此示例中,您将找到计算输入数字的幂的代码。例如,2 的 4 次方等于 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