From: j8takagi Date: Sun, 28 Oct 2012 04:30:32 +0000 (+0900) Subject: fizzbuzz各種 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3f04e84859b5d5a0351ae7acaa222056ff04ef7;p=fizzbuzz.git fizzbuzz各種 C、JavaScript、CASL2、Ruby版を作成 --- e3f04e84859b5d5a0351ae7acaa222056ff04ef7 diff --git a/0.txt b/0.txt new file mode 100644 index 0000000..dd96488 --- /dev/null +++ b/0.txt @@ -0,0 +1,100 @@ +1 +2 +Fizz +4 +Buzz +Fizz +7 +8 +Fizz +Buzz +11 +Fizz +13 +14 +FizzBuzz +16 +17 +Fizz +19 +Buzz +Fizz +22 +23 +Fizz +Buzz +26 +Fizz +28 +29 +FizzBuzz +31 +32 +Fizz +34 +Buzz +Fizz +37 +38 +Fizz +Buzz +41 +Fizz +43 +44 +FizzBuzz +46 +47 +Fizz +49 +Buzz +Fizz +52 +53 +Fizz +Buzz +56 +Fizz +58 +59 +FizzBuzz +61 +62 +Fizz +64 +Buzz +Fizz +67 +68 +Fizz +Buzz +71 +Fizz +73 +74 +FizzBuzz +76 +77 +Fizz +79 +Buzz +Fizz +82 +83 +Fizz +Buzz +86 +Fizz +88 +89 +FizzBuzz +91 +92 +Fizz +94 +Buzz +Fizz +97 +98 +Fizz +Buzz diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..0bcad0e --- /dev/null +++ b/c/Makefile @@ -0,0 +1,16 @@ +CC ?= gcc +CFLAGS ?= -g -Wall -Wextra +RMF ?= rm -f + +COMMONSRC := word.c cmem.c cerr.c +CASL2SRC := struct.c hash.c +ASSRC := assemble.c token.c label.c +EXECSRC := exec.c dump.c + +.PHONY: clean + +fizzubzz: fizzbuzz.c + $(CC) $(CFLAGS) -o fizzbuzz $^ + +clean: + @$(RMF) fizzbuzz diff --git a/c/fizzbuzz.c b/c/fizzbuzz.c new file mode 100644 index 0000000..f54ea80 --- /dev/null +++ b/c/fizzbuzz.c @@ -0,0 +1,28 @@ +#include + +void puts_fizzbuzz(int n) +{ + int fizz, buzz; + + if ((fizz = (n % 3 == 0))) { + printf("Fizz"); + } + if ((buzz = (n % 5 == 0))) { + printf("Buzz"); + } + if (!fizz && !buzz) { + printf("%d", n); + } + printf("\n"); +} + +int main() +{ + static int max = 100, min = 1; + int i; + + for(i = min; i <= max; i++) { + puts_fizzbuzz(i); + } + return 0; +} diff --git a/casl/fizzbuzz.casl b/casl/fizzbuzz.casl new file mode 100644 index 0000000..261cb5d --- /dev/null +++ b/casl/fizzbuzz.casl @@ -0,0 +1,47 @@ +MAIN START + RPUSH + LD GR1,MIN ; GR1にMINを代入 +LOOP CALL FIZBUZ ; FIZBUZを呼び出す + LAD GR1,1,GR1 ; GR1 <- GR1 + 1 + CPL GR1,MAX ; GR1とMAXを比較 + JPL FIN ; GR1 > MAX の場合、FINへジャンプ + JUMP LOOP ; LOOPへジャンプ +FIN RPOP + RET +MIN DC 1 +MAX DC 100 + +;; GR1の値に応じて次を出力 +;; 3の倍数: Fizz +;; 5の倍数: Buzz +;; 3の倍数かつ5の倍数: FizzBuzz +;; それ以外の数: 数値 +FIZBUZ START + RPUSH + XOR GR4,GR4 ; GR4: 文字列の長さおよびフラグ +CHKFIZ LAD GR2,3 ; GR2に3を代入 + CALL DIVL ; GR3 <- GR1 % GR2:3 + LD GR3,GR3 ; GR3のフラグ値を取得 + JNZ CHKBUZ ; GR3が0でない場合、3の倍数ではないので、CHKBUZへジャンプ +SETFIZ LAD GR4,4 ; GR4に文字列「Fizz」の長さ4を代入 +CHKBUZ LAD GR2,5 ; GR2に5を代入 + CALL DIVL ; GR3 <- GR1 % GR2:5 + LD GR3,GR3 ; GR3のフラグ値を取得 + JZE SETBUZ ; GR3が0の場合、5の倍数なので、SETBUZへジャンプ + LD GR4,GR4 ; GR4のフラグ値を取得 + JZE PUTNUM ; GR4が0の場合、3の倍数でも5の倍数でもないので、PUTNUMへジャンプ + JUMP SETLEN ; SETLENへジャンプ(3の倍数で5の倍数ではない) +SETBUZ LD GR4,GR4 ; GR4のフラグ値を取得 + LAD GR4,4,GR4 ; GR4に文字列「Buzz」の長さ4を追加 +SETLEN ST GR4,LEN ; LENにGR4の値を出力 +CHKFB JZE PUTBUZ ; GR4が0の場合、3の倍数ではないので、PUTBUZへジャンプ +PUTFIZ OUT FIZZ,LEN ; 「Fizz」または「FizzBuzz」を出力 + JUMP FIN ; FINへジャンプ +PUTBUZ OUT BUZZ,LEN ; 「Buzz」を出力 + JUMP FIN ; FINへジャンプ +PUTNUM CALL OUTA ; GR1の数値を表示 +FIN RPOP + RET +FIZZ DC 'Fizz' +BUZZ DC 'Buzz' +LEN DS 1 diff --git a/js/fizzbuzz.js b/js/fizzbuzz.js new file mode 100644 index 0000000..b52eb32 --- /dev/null +++ b/js/fizzbuzz.js @@ -0,0 +1,19 @@ +min = 1; +max = 100; + +for(i = min; i <= max; i++) { + fizzbuzz_puts(i); +} + +function fizzbuzz_puts(n) { + if (fizz = (n % 3 == 0)) { + document.write("Fizz"); + } + if (buzz = (n % 5 == 0)) { + document.write("Buzz"); + } + if (!fizz && !buzz) { + document.write(n); + } + document.write("
\n"); +} diff --git a/js/index.html b/js/index.html new file mode 100644 index 0000000..9083003 --- /dev/null +++ b/js/index.html @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/ruby/fizzbuzz.rb b/ruby/fizzbuzz.rb new file mode 100755 index 0000000..60ab1b1 --- /dev/null +++ b/ruby/fizzbuzz.rb @@ -0,0 +1,15 @@ +#!/usr/local/bin/ruby + +def puts_fizzbuzz(n) + print "Fizz" if fizz = (n % 3 == 0) + print "Buzz" if buzz = (n % 5 == 0) + print n if not fizz and not buzz + print "\n" +end + +min = 1 +max = 100 + +for i in min..max + puts_fizzbuzz(i) +end