创建一个HQ9 +解释器


31

编写此代码的目的是为HQ9 +编程语言创建一个解释器。此编程语言
有4个命令:

  • H-打印“你好,世界!”
  • Q-打印程序的源代码
  • 9-打印歌曲“ 99樽啤酒”的歌词
  • + -增加累加器

规则:

  1. 由于未定义累加器,因此您可以忽略命令+
  2. 您的程序应提示您输入(此输入将是源代码)或从文件中读取源代码
  3. 不允许将“ 99瓶啤酒”的歌词放入文件中,并从文件中读取文本
  4. 不允许使用GZip或BZip等压缩算法来压缩歌曲文本:您必须创建自己的压缩算法。这不一定是复杂的算法,但是请尝试尽可能地压缩文本(请记住:这是一个代码高尔夫球,字节数最少的代码将获胜)
  5. 如果源代码包含的字符不是HQ9+,则在运行任何命令之前输出“源代码包含无效字符” 这意味着,例如,如果源代码是H1,则在输出Hello, world!之前不要输出Source code contains invalid characters。否,立即输出Source code contains invalid characters
  6. 如果您的解释器同时接受大写和小写字符,则您的字符数为-8

祝好运!


3
规则4对于“他为什么禁止这样做?!”没有任何意义。水平; 规则5对“他是什么意思?!”没有任何意义。水平。
彼得·泰勒

4
规则5为何没有意义?
Mike C

我认为最好只要求使用大写字母,而不是随意说您可以接受一个免费的8个字符。根据语言的不同,图8可能会也可能不会。
Mike C

10
规则5没有意义,因为它违反了HQ9 +规范。
展位,

2
HQ9 +中的口译员应自动获胜。
TheNumberOne

Answers:


16

APL(Dyalog)(326-8 = 318)

将其粘贴到编辑器窗口中,然后调用H。输入来自键盘。

H
⎕←'Source code contains invalid characters'/⍨~∧/'HhQq9+'∊⍨V←⍞
B←' of beer'
W←' on the wall'
L←⎕TC[2]
Z←{k←' bottle','s'/⍨⍵≠1⋄⍵<0:'99',k⋄⍵=0:'No',k⋄k,⍨⍕⍵}
{⍵∊'Hh':⎕←'Hello, world!'
⍵∊'Qq':⎕←V
⍵∊'9':{⎕←G,K,(G←Z⍵),B,L,(⊃'Go to the store and buy some more' 'Take one down and pass it around'[1+×⍵]),L,(Z⍵-1),K←B,W,L}¨1-⍨⌽⍳100}¨V

6

Mathematica,349 346 341个字符

h = If[StringMatchQ[#, ("H" | "Q" | "9" | "+") ...], 
   b = If[# > 0, ToString@#, "No"] <> " bottle" <> 
      If[# == 1, "", "s"] <> " of beer" &; 
   w = b@# <> " on the wall" &; 
   Print /@ 
    StringCases[#, {"H" -> "Hello, world!", "Q" -> #, 
      "9" -> "" <> 
        Table[w@n <> ",\n" <> b@n <> 
          ".\ntake one down, pass it around,\n" <> w[n - 1] <> 
          If[n == 1, ".", ".\n\n"], {n, 99, 1, -1}]}];, 
   "Source code contains invalid characters"] &

非常令人印象深刻,但似乎有一些小故障。'h[“ Q”]输出“ Q”。h [“ 9”]有效,但是h [9]输出整个程序(我不知道如何。)
DavidC

@DavidCarraher由于StringMatchQ和If失败而导致输出代码:If[StringMatchQ[3, "a"], x, y]
ssch 2013年

6

C,599个 587 487 481 467字符

我敢肯定这可以被击败。毕竟我在用C。有一个原因,您看不到用C编写的许多获胜高尔夫作品。由于对的滥用,该数字现已达到467个字符#define

将HQ9 +源文件指定为命令行参数。

编辑:现在接受来自stdin的源文件,而不是文件。启动程序,开始输入代码,CTRL- C在完成时运行它。

至少在MinGW / GCC中使用以下命令进行编译: gcc -o hq9+.exe hq9+.c

应该可以在MSVC中工作,但是我不想为此创建一个完整的解决方案。:)

#define B "bottles of beer"
#define C case
#define P printf
#define R break
a,i,j,s;
main(){
char p[9999],c;
for(;;) {
    switch(c=getch()){
    C 'H':C 'Q':C '9':C '+': C 3: R;
    default:P("Source code contains invalid characters\n");
    }
    if (c==3) R;
    p[s++]=c;
}
for(i=0;i<s;i++){
    c = p[i];
    switch(c){
    C 'H':
        P("Hello world!");
        R;
    C 'Q':
        for(j=0;j<s;j++)putchar(p[j]);
        R;
    C '9':
        j=99;
        while(j){
            P("%d "B" on the wall,\n%d "B".\nTake one down, pass it around,\n%d "B".\n",j,j,j-1);
            j--;
        }
        R;
    C '+':
        a++;
    }
}
}

要么:

#define B "bottles of beer"
#define C case
#define P printf
#define R break
a,i,j,s;main(){char p[9999],c;for(;;){switch(c=getch()){C 'H':C 'Q':C '9':C '+': C 3: R;default:P("Source code contains invalid characters\n");}if (c==3) R;p[s++]=c;}for(i=0;i<s;i++){c = p[i];switch(c){C 'H':P("Hello world!");R;C 'Q':for(j=0;j<s;j++)putchar(p[j]);R;C '9':j=99;while(j){P("%d "B" on the wall,\n%d "B".\nTake one down, pass it around,\n%d "B".\n",j,j,j-1);j--;}R;C '+':a++;}}}

通过使用stdin代替输入文件将其缩减为487个字符。
Mike C

通过滥用#define来设置464。
Mike C

您可以摆脱空间,例如之后的空间C和之前的空间R;p[s++]=0;在输入循环之后还可以减少8个字符,以简化打印p
Daniel Lubarov

3

Python的2 - 452个 453 443字节

q=raw_input()
if set(q)-set('HQ9+'):print'Source code contains invalid characters'
b=' bottles of beer'
b=[b,b.replace('s','')]
w=[a+' on the wall'for a in b]
t='Take one down, pass it around,\n'
c={'H':'Hello, world!','Q':q,'9':''.join(`i`+w[i<2]+',\n'+`i`+b[i<2]+'.\n'+t+(`i`if i>1 else'No')+w[0]+'.\n'for i in range(1,100)[::-1])+'No'+w[0]+',\nNo'+b[0]+'.\n'+'Go to the store, buy some more,\n99'+w[0]+'.'}
for d in q:
 if d in c:print c[d]

eh 它已经很暗了,但是后来我发现了一个错误,使我花了一个字节来修复。eh

(例如,我以前+在输出中不包含Q。例如,输入Q+++给出了输出Q。)

通过不退出无效输入来保存一些字符,就像我认为的那样。

即将推出解释版本。我放弃。我什至几乎不了解这种怪兽是如何工作的。如果有需求,我会再给它一次,但直到那时它都没有发生。


您无需exit输入无效字符,只需显示消息,即可在其中保存7个字符。
Kyle Kanos 2014年

@KyleKanos哦,我误会了。谢谢!
地下

您的歌词不正确-当前它说: 99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around, *99* bottles of beer on the wall. 它应该说: 99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around, *98* bottles of beer on the wall.
Oliver Ni

当我输入时,也会出现错误+。你应该忽略 +,不给错误
奥利弗镍

同样,在输出之间不应该有任何东西。就像我输入HQHH时应该说Hello World!HQHHHelloWorld!Hello World!
Oliver Ni

3

Perl,325-8 = 317

sub p{print@_}$_=<>;$a=' on the wall';$b=' bottle';$e=' of beer';$c='Take one down, pass it around';if(!/^[hqHQ9+]+$/){p"Source code contains invalid characters";exit}$k=$_;for(/./g){/H|h/&&p"Hello, World!";if(/9/){$i=99;$d=$b."s$e";while($i>0){p"$i$d$a
$i$d
$c
";$q=--$i==1?'':'s';$d="$b$q$e";$i||=No;p"$i$d$a
"}}/Q|q/&&p$k}

展开:

sub p{print@_}
$_=<>;
$a=' on the wall';
$b=' bottle';
$e=' of beer';
$c='Take one down, pass it around';
if(!/^[hqHQ9+]+$/){
    p"Source code contains invalid characters";
    exit
}
$k=$_;
for(/./g){
    /H|h/&&p"Hello, World!";
    if(/9/){
        $i=99;
        $d=$b."s$e";
        while($i>0){
            p"$i$d$a
$i$d
$c
";
            $q=--$i==1?'':'s';
            $d="$b$q$e";
            $i||=No;
            p"$i$d$a
"
        }
    }
    /Q|q/&&p$k
}

2

红宝石, 364360-8 = 352

仍然有很多改进的余地。从这里偷来的99瓶代码。

p=gets.upcase
if p=~/[^HQ9+
]/
puts"Source code contains invalid characters"else
p.each_char{|x|case x
when ?H
puts"Hello, world!"
when ?Q
puts p
when ?9
def c;"#{$n} bottle#{'s'if$n>1} of beer on the wall"end
($n=99).times{puts"#{c}, #{c[0..-13]}.
#{$n<2?"Go to the store and buy some more":"Take one down and pass it around"}, #{$n=($n-2)%99+1;c}.

"}end}end

应该No more bottles of beer on the wall在本1 bottle节之后。然后是Go to the store...
奥利弗·尼

1 bottle of beer on the wall, 1 bottle of beer on the wall, take one down, pass it around, no more bottles of beer on the wall. No more bottles of beer on the wall, no more bottles of beer, go to the store, buy some more, 99 bottles of beer on the wall.
奥利弗·倪

@Oliver问题未指定歌曲的确切歌词。在任何地方都不需要包含“ no more”部分。
Doorknob

好的,但是,输出之间不应该有换行符。
奥利弗·倪

@Oliver挑战在哪里说呢?
门把手

2

哈斯克尔298

main=interact g
g s|all(`elem`"HQ9+")s=s>>=(%s)|0<1="Source code contains invalid characters"
'H'%_="hello World!"
'Q'%s=s
'9'%_=drop 32$d 99
_%_=""
k=" bottles of beer on the wall"
d 0="No more"++k++"."
d n|a<-shows n k=[a,".\n\n",a,",\n",take 18a,".\ntake one down, pass it around\n",d$n-1]>>=id

这很简单。%返回命令的输出(给出了用于的源cod Q)。d由于打高尔夫球的原因,一开始会用垃圾线返回99瓶歌曲。一切都包裹在一个交互中(您可能想在测试时使用g而不是使用main


我应该如何运行?我尝试将其粘贴到WinGHCi中,但是得到了<interactive>:8:5: parse error on input ‘=’
ProgramFOX

@ProgramFOX我不知道...我什么时候可以检查
骄傲的haskeller 2015年

@ProgramFOX很抱歉花了很长时间:-)。您没有正确使用GHCi。在GHCi中,您无法像预期的那样直接输入定义。您可以使用a let插入定义(插入多个不以分隔的定义时会中断;),也可以运行文件中的代码。
自豪的haskeller 2015年

我懂了。我仍然无法尝试运行它;当我加载文件并调用时main,它说我尝试执行时源代码包含无效字符H。当我尝试运行g而不是时main,它立即显示错误。
ProgramFOX

@ProgramFOX main不起作用,因为附加的换行符不是合法命令。这就是为什么我建议g改为使用的原因。至于gputStrLn $ g "H" 不能正常工作?
骄傲的haskeller 2015年

2

J-444字节

我喜欢这个号码,所以停止打高尔夫球。这里,单表达式功能!

f=:'Source code contains invalid characters'"_`('Hello, world!'"_`[`((((s=:(<:@[s],L,(v,LF,'Take one down and pass it around, '"_,b@<:,' of beer on the wall.'"_)@[,''"_)`(],(L=:LF,LF),(v=:1&b,' of beer on the wall, '"_,b,' of beer.'"_)@[)@.([<1:))''"_),LF,'Go to the store and buy some more, '"_,(b=:({&'Nn'@([=0:),'o more'"_)`(":@])@.(]>0:),{.&' bottles'@(8:-=&1@])),' of beer on the wall.'"_)@(99"_))`]@.('HQ9+'&i.@])"0 1 0])@.(*./@e.&'HQ9+')

例子:

   hq9 '9QHHQ+'
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

...

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
9QHHQ+
Hello, world!
Hello, world!
9QHHQ+
+
   hq9 '9QHHaQ'
Source code contains invalid characters

不,我不会为此做任何解释。太长。
seequ 2014年

等等,为什么这票数低呢?
seequ

2

Fortran的528 470 481

它要求编译-fpp标志(+3得分)1使用预处理指令(节省的方式比3个字符以上,所以我与完全没关系)。它也不区分大小写,所以有-8:D。通过不预处理保存5个字符endif作为被使用一次呢

要求文件具有.F90扩展名(称其为hq9+.F90),以便编译器强制进行预处理。该代码区分大小写;使其不区分大小写会添加大约16个字符,因此保存8个字符确实不值得。我以前的答案没有考虑改变复数的bottles9; 此版本对其进行了更正(可悲的是添加了很多字符)。

#define P print*,
#define W " of beer on the wall"
#define N print'(x,i0,a,a)',
#define e enddo
#define S case
#define J len(trim(c))
character(len=99)::c,b=" bottles";read*,c;do i=1,J;if(all(c(i:i)/=["H","Q",'9',"+"])) then;P"Source code contains invalid characters";exit;endif;e;do i=1,J;select S(c(i:i));S("H");P"Hello, world!";S("Q");P c;S("9");l=8;do k=99,1,-1;N k,b(1:l),W;N k,b(1:l)," of beer";P "Take one down, pass it around";if(k==2)l=l-1;if(k==1)exit;N k-1,b(1:l),W;P"";e;P"No more",trim(b),W;S default;endselect;e;end

未经处理和未经预处理的外观要好得多(可能是因为您可以看到发生了什么事情):

program hq9
   character(len=99)::c,b=" bottles"
   read*,c
   do i=1,len(trim(c))
! change the below to ["H","h","Q","q","9","+"] to be case-insensitive
      if(all(c(i:i)/=["H","Q","9","+"]))then
         print*,"Source code contains invalid characters"
         exit
      endif
   enddo
   do i=1,len(trim(c))
      select case(c(i:i))
        case("H")                ! change to case("H","h") for case-insensitive
           print*,"Hello, world!"
        case("Q")                ! change to case("Q","q") for case-insensitive
           print*, c
        case("9")
           l=8
           do k=99,1,-1
              print'(x,i0,a,a)', k,b(1:l)," of beer on the wall"
              print'(x,i0,a)', k,b(1:l)," of beer"
              print*,"Take one down, pass it around"
              if(k==2) l=l-1
              if(k==1) exit
              print'(x,i0,a)', k-1,b(1:l)," of beer on the wall"
              print*,""
           enddo
           print*,"No more",trim(b)," of beer on the wall"
        case default
           ! do nothing
      endselect
   enddo
end program hq9

我认为可以要求特定的扩展名,以使编译器知道其编译内容是可以接受的。类似物可能需要命名C ++文件.cc以避免需要该-lstdc++标志。
2014年

2

Python 2,340-8 = 332

s,o=raw_input(),''
for z in s:
 if z in'Hh':o+='Hello World!'
 elif z in'Qq':o+=s
 elif'9'==z:
  i=298
  while~-i:print i/3or 99,'bottle'+'s of beer on the wall.\n'[2<i<6:9+i%3*12]+'..\nGToa kteo  otnhee  dsotwonr,e ,p absusy  isto maer omuonrde,,'[(i>3)+i%3*68::2];i-=1
 elif'+'!=z:o='Source code contains invalid characters';break
print o

1

Lua 443-8 = 435 = 456

我设法通过使用多个if-end而不是来保存21个字符if-elseif-end。几次之后,我还有一些多余的空白空间)

p=print o=" of beer"t=" on the wall"B=" bottle"b=" bottles"l=io.read("*l");g=l:lower()if g:match"[^hq9+]"then p("Source code contains invalid characters")end for i=1,#g do s=g:sub(i,i)if s=='h'then p("Hello, world")end if s=='q'then p(l)end if s=='9'then n=99 repeat p(n..b..o..t..", "..n..b..o)n=n-1 p("Take one down, pass it around, "..n..b..o..t..".")p()until n==1 p("1"..B..o..t..", 1"..B..o)p("No more"..b..o..t..", no more"..b..o)end end

我对此感到非常满意,尽管它比我的Fortran回答短很多。亚历山德罗(Alessandro)根据这个答案修改了99瓶啤酒密码。松散,我们有

-- reuse stuff
p=print
o=" of beer"
t=" on the wall"
B=" bottle"
b=" bottles"
-- read the line & then lowercase it for case insensitivity
l=io.read("*l");g=l:lower()
if g:match"[^hq9+]" then -- horray for basic regex
   p("Source code contains invalid characters")
end
for i=1,#g do
   s=g:sub(i,i)               -- take substring
   if s=='h' then p("Hello, world") end
   if s=='q' then p(l) end
   if s=='9' then
      n=99
      repeat
         p(n..b..o..t..", "..n..b..o)
         n=n-1
         p("Take one down, pass it around, "..n..b..o..t..".")
         p()
      until n==1
      p("1"..B..o..t..", 1"..B..o)
      p("No more"..b..o..t..", no more"..b..o)
   end
end

用于小写输入的代码需要12个字符,因此花钱不值得。
nyuszika7h,2015年

您不需要小写输入,您可以执行以下操作if s in'Hh'
Oliver Ni

它节省了8个字符
奥利弗镍

@MCParadox:嗯,这就是您要考虑的Python,Lua in用于for循环迭代器而不是比较;如果将其粘贴在其中,则会出现错误。这里有三个选择:(1)match在每一行使用,(2)s=='h'or s=='H'在每一行使用,(3)按照我的方式来做。显然,(3)比(1)和(2)短。
凯尔·坎诺斯

1

朱莉娅362

s = chomp(readline(STDIN))
l=""
z=" of beer"
q,r,w,t=" bottles$z"," bottle$z"," on the wall.\n","take one down, pass it around,\n"
for j=99:-1:2
b="$j$q"
l*="$b$w$b.\n$t"
end
l*="1$r$(w)1$r.\n$(t)No$q$w"
p=println
all(c->c in "HQ9+", s)||p("Source code contains invalid characters")
for c in s
    c=='Q'&&p(s)
    c=='H'&&p("Hello, world!")
    c=='9'&&p(l)
end

1

Tcl,515

set d [read stdin]
if {![regexp {^[hq9\+HQ]*$} $d]} {puts "Source code contains invalid characters"}
lmap c [split $d {}] {set b { bottles of beer}
switch -- $c H {puts "Hello, world"} Q {puts $d} 9 {for {set i 99} {$i>2} {incr i -1} {puts "$i$b on the wall,
$i$b.
Take one down, pass it around,
[expr $i-1]$b on the wall.
"}
puts "2$b on the wall,
2$b.
Take one down, pass it around,
1 bottle of beer on the wall.

1 bottle of beer on the wall,
1 bottle of beer.
Take one down, pass it around,
No$b on the wall."}}

只打了一点球,仍然比C小,还有正确的99瓶啤酒。


好吧,现在是合法的。干得好,你打败了我。我应该停止在高尔夫上使用C。
Mike C

您可以删除第一行并将其嵌入的最后一个参数regexp。您可以在codegolf.stackexchange.com/a/109818/29325
sergiol

1

JavaScript(ES6),385

s=>{n=99,b=' bottle',j=' of beer',d=' on the wall',e='Take one down, pass it around',k='Go to the store, buy some more',l='No',o='s',f=', ';for(i=s.split(m=v='');~n;)v+=[n||l,b,n-1?o:m,j,d,f,n||l,b,n-1?o:m,j,f,n?e:k,f,--n<1?99:n,b,n-1?o:m,j,d,'! '].join(m);return s.match(/[^HQ9\+]/,r='')?'Source code contains invalid characters':[...s].map(c=>({H:'Hello World!',9:v,Q:s})[c]).join``}

不选择不区分大小写的字符,将花费太多字符。甚至没有接近其他条目,但仍然很有趣!

JavaScript 344

我制作的歌曲版本不太完整:

(function(i){if(s.match(/[^HQ9\+]/)){m='Source code contains invalid characters'}else{n=99,b=' bottles of beer ',d='on the wall',e='take one down, pass it around',f=', ';for(;n;)v+=[n,b,d,f,n,b,f,e,f,--n||'no more',b,d,'! '].join(m);h={H:'Hello World!',Q:arguments.callee+m,9:v};for(;c=i[n++];)m+=h[c]||''}alert(m)})((s=prompt()).split(m=v=''))

但是看完其他条目(然后看一下真正的歌词,谁知道!)之后,我认为这有点不合时宜!


应该是Go to the store, buy some more, *99* bottles of beer on the wall,不是*No more* bottles of beer on the wall
Oliver Ni

如果您多买一些,又怎么会有呢?
奥利弗·倪

而且,Q对我也不起作用。
奥利弗·倪

@Oliver当时没有注意到您对此发表评论。。。我Q本来应该这样做的时候完全误解了,并提供了函数的源代码而不是输入内容……这最初是我这样做的原因在JS中,这部分很容易,但是我现在知道这根本不是必需的!
唐·黑斯廷斯

@Oliver更新了歌词!
Dom Hastings

1

C,562字节

char*c="%1d %3$s of %4$s on the %5$s, %1d %3$s of %4$s.\n\0Take one down and pass it around, %2d %3$s of %4$s on the %5$s.\n\0Go to the store and buy some more, %1d %3$s of %4$s on the %5$s.\n";main(int a,char**b){int x=0;for(int i=0;i<strlen(b[1]);i++){if(b[1][i]=='+')x++;else if(b[1][i]=='9'){int k=99;while(1){printf(&c[0],k,k,k==1?"bottle":"bottles","beer","wall");if(k!=1){printf(&c[49],k,k,"bottles","beer","wall");k--;}else{k=99;printf(&c[114],k,k,"bottles","beer","wall");break;}}}else printf("%s",b[1][i]=='H'?"Hello, world!":(b[1][i]=='Q'?b[1]:""));}}

作为一个完整的程序。第一个参数是HQ9 +程序。带有实际的蓄电池。我挑战自己不要使用define语句。

非高尔夫版本:

char* c = "%1d %3$s of %4$s on the %5$s, %1d %3$s of %4$s.\n\0Take one down and pass it around, %2d %3$s of %4$s on the %5$s.\n\0Go to the store and buy some more, %1d %3$s of %4$s on the %5$s.\n";
main (int a, char** b) {
    int x = 0;
    for (int i = 0; i < strlen(b[1]); i++) {
        if (b[1][i] == '+')
            x++;
        else if (b[1][i] == '9') {
            int k = 99;
            while (1) {
                printf(&c[0], k, k, k == 1 ? "bottle" : "bottles", "beer", "wall");
                if (k != 1) {
                    printf(&c[49], k, k, "bottles", "beer", "wall");
                    k--;
                } else {
                    k=99;
                    printf(&c[114], k, k, "bottles", "beer", "wall");
                    break;
                }
            }
        } else
            printf("%s",b[1][i] == 'H' ? "Hello, world!" : (b[1][i] == 'Q' ? b[1] : ""));
    }
}

0

Java,546字节

这是我第一次提交高尔夫代码。我相信我们可以做得更多。它将输入作为命令行参数读取。来自“ 99瓶啤酒” java答案的啤酒代码“借来的” (创造性的共同点)

class a{public static void main(String[] a){if(a[0].matches("^[HQ9\\Q+\\E]+$")){for(char c:a[0].toCharArray()){if(c=='H')p("Hello, world!");if(c=='Q')p(a[0]);if(c=='9')b();}}else{System.out.println("Source code contains invalid characters");}}static void p(String s){System.out.println(s);}static void b(){String b=" of beer",c=" on the wall",n=".\n",s;for(int i=100;i-->1;){s=" bottle"+(i>1?"s":"");p(i+s+b+c+", "+i+s+b+n+(i<2?"Go to the store and buy some more, 99":"Take one down and pass it around, "+(i-1))+" bottle"+(i!=2?"s":"")+b+c+n);}}}

让我知道命令行参数是否不可接受。这很有趣!


CLA很好。好答案。
Rɪᴋᴇʀ

0

Excel VBA,489字节

不缩进:

Sub h(s)
For c=1 To Len(s)
Select Case Mid(s,c,1)
Case "H"
Debug.? "Hello,World!"
Case "Q"
Set v=ActiveWorkbook.VBProject.VBComponents("M").CodeModule:For n=1 To v.countoflines:Debug.? v.Lines(n,1):Next
Case "+"
a=a+1
Case "9"
s=" Bottles of Beer":o=" Bottle of Beer":w=" on the wall":t=". Take 1 down pass it around,":p=",":d=".":For n=99 To 3 Step -1:Debug.? n;s;w;p;n;s;t;n-1;s;w;d:Next:Debug.? 2;s;w;p;2;s;t;1;o;w;d:Debug.? 1;o;w;p;1;o;t;"No";s;w;d
End Select
Next
End Sub

(为便于阅读而缩进)

Sub h(s)
For c=1 To Len(s)
Select Case Mid(s,c,1)
    Case "H"
        Debug.? "Hello,World!"
    Case "Q"
        Set v=ActiveWorkbook.VBProject.VBComponents("M").CodeModule
        For n=1 To v.countoflines
            Debug.? v.Lines(n,1)
        Next
    Case "+"
        a=a+1
    Case "9"
        s=" Bottles of Beer"
        o=" Bottle of Beer"
        w=" on the wall"
        t=". Take 1 down pass it around,"
        p=","
        d="."
        For n=99 To 3 Step -1
            Debug.? n;s;w;p;n;s;t;n-1;s;w;d
        Next
        Debug.? 2;s;w;p;2;s;t;1;o;w;d
        Debug.? 1;o;w;p;1;o;t;"No";s;w;d
End Select
Next
End Sub

将默认的Module1重命名为M
调用,h "+++++++++Your Code"
这也将在其他Office应用程序上起作用,将ActiveWorkbook更改为适当的文档类型


请注意,excel将添加空格并扩展缩写,因此在excel中,代码为531个字符
-SeanC

多数民众赞成在636个字符
奥利弗·倪

从我的缩进@oliver中删除所有空格
SeanC'1

您可以通过将For n=1 Toto的所有实例转换为For n=1To(删除之前的空白To)来释放3字节,通过将to的所有实例转换Debug.? 2;Debug.?2;(删除?关键字之后的空白)来释放4字节
Taylor Scott
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.