Some More 例子
优质
小牛编辑
132浏览
2023-12-01
前面的章节向您解释了LOLCODE中的编程。 在本章中,您将学习一些示例,这些示例允许您在LOLCODE中进行高级编码。
示例1:计算数字幂的程序
在此示例中,您将找到用于计算输入数字功效的代码。 例如,升至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