GnuCOBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-COLORS.
    03 MY-COLOR OCCURS 3 PIC X(10).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "red" TO MY-COLOR(1).
    MOVE "yellow" TO MY-COLOR(2).
    MOVE "blue" TO MY-COLOR(3).
    DISPLAY MY-COLORS.
    DISPLAY MY-COLOR(2).
    STOP RUN.
[vagrant@localhost cobol]$ cobc -x --free hello.cob
[vagrant@localhost cobol]$ ./hello
red       yellow    blue
yellow

演算

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA.
    03 D1 PIC 99999 VALUE 0.
    03 D2 PIC 99999 VALUE 0.
    03 D3 PIC 99999 VALUE 0.
    03 D4 PIC 99999 VALUE 0.
PROCEDURE DIVISION.
  MAIN SECTION.
    ADD 5 TO D1 D2 D3.
    SUBTRACT 2 FROM D1 D2.
    DISPLAY MY-DATA.
    STOP RUN.

入力受付

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA PIC X(10).
PROCEDURE DIVISION.
  MAIN SECTION.
    DISPLAY "whats your name?".
    ACCEPT MY-DATA.
    DISPLAY "hello! " MY-DATA.
    STOP RUN.

条件分岐

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-SCORE PIC 9(3).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 80 TO MY-SCORE.
    If MY-SCORE > 60 THEN
      DISPLAY "OK!"
    END-IF.
    STOP RUN.

条件分岐

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-SIGNAL PIC X(5).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "red" TO MY-SIGNAL.
    EVALUATE MY-SIGNAL
      WHEN "red"
        DISPLAY "stop!"
      WHEN "blue"
          DISPLAY "run!"
      WHEN "yello"
            DISPLAY "caution!"
    END-EVALUATE.
    STOP RUN.

繰り返し処理

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-COUNTER PIC 9(3) VALUE 1.
PROCEDURE DIVISION.
  MAIN SECTION.
    PERFORM 10 TIMES
      DISPLAY "counter = " MY-COUNTER
      ADD 1 TO MY-COUNTER
    END-PERFORM.
    STOP RUN.

ファイルの書き込み

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
    FILE-CONTROL.
      SELECT OUT-FILE ASSIGN TO 'out.txt'
        ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
  FILE SECTION.
    FD OUT-FILE.
      01 OUT-FILE-REC PIC 99.
 WORKING-STORAGE SECTION.
  01 LINE-COUNT PIC 99 VALUE 0.
PROCEDURE DIVISION.
  MAIN SECTION.
    OPEN OUTPUT OUT-FILE.

    PERFORM 10 TIMES
      ADD 1 TO LINE-COUNT
      MOVE LINE-COUNT TO OUT-FILE-REC
      WRITE OUT-FILE-REC
      DISPLAY OUT-FILE-REC
    END-PERFORM.

    CLOSE OUT-FILE.
    STOP RUN.

ファイルの読み出し

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
   FILE-CONTROL.
    SELECT IN-FILE ASSIGN TO 'in.txt'
     ORGANIZATION IS LINE SEQUENTIAL
     STATUS IN-FILE-STATUS.
DATA DIVISION.
  FILE SECTION.
   FD IN-FILE.
    01 In-FILE-REC PIC X(20).
  WORKING-STORAGE SECTION.
   01 IN-FILE-STATUS PIC XX.
PROCEDURE DIVISION.
  MAIN SECTION.
    OPEN INPUT IN-FILE.

    PERFORM UNTIL IN-FILE-STATUS NOT = "00"
     READ IN-FILE
       AT END
         DISPLAY "-- end --"
       NOT AT END
         DISPLAY "Content: " IN-FILE-REC
    END-READ
    END-PERFORM.

    CLOSE IN-FILE.
    STOP RUN.

OPEN-COBOL

COBOLはcommon business oriented languageの略で、書式のルールが厳格です。文末がピリオドで、大文字です。

DIVISION > SECTION

cobol

開発環境に必要となるファイルをインストールしていきましょう。

# yum -y install gcc
# yum -y install gmp-devel
# yum -y install ncurses-devel
# yum -y install db4-devel
# wget http://downloads.sourceforge.net/open-cobol/open-cobol-1.0.tar.gz
# tar zxvf open-cobol-1.0.tar.gz
# cd open-cobol-1.0
# ./configure
# make
# make install
# ln -s /usr/local/lib/libcob.so.1.0.0 /usr/lib/libcob.so.1
# ln -s /usr/local/lib/libcob.so.1.0.0 /usr/lib/libcob.so
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/li

自由書式です。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
  MAIN SECTION.
    DISPLAY 'HELLO WORLD!'.
    STOP RUN.
[vagrant@localhost cobol]$ cobc -x --free hello.cob
[vagrant@localhost cobol]$ ls
hello  hello.cob
[vagrant@localhost cobol]$ ./hello
HELLO WORLD!

変数

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA1 PIC X(20).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 'HELLO WORLD!' TO MY-DATA1.
    DISPLAY MY-DATA1.
    STOP RUN.

数値の変数

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA1 PIC X(20).
  01 MY-DATA2 PIC 99999.
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 120 TO MY-DATA2.
    MOVE 'HELLO WORLD!' TO MY-DATA1.
    DISPLAY MY-DATA2.
    STOP RUN.

集合項目

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-PRODUCT.
    03 PRODUCT-ID PIC X(5).
    03 PRODUCT-PRICE PIC 9(5).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "ID-12" TO PRODUCT-ID.
    MOVE 2980 TO PRODUCT-PRICE.
    DISPLAY PRODUCT-PRICE.
    DISPLAY MY-PRODUCT.
    STOP RUN.

gcc

[vagrant@localhost c]$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
コンフィグオプション: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
スレッドモデル: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
#include <stdio.h>

int main(void){
  printf("hello world!\n");
  return 0;
}
[vagrant@localhost c]$ gcc -o hello hello.c
[vagrant@localhost c]$ ll
合計 12
-rwxrwxr-x. 1 vagrant vagrant 6425 11月 10 06:31 2016 hello
-rw-rw-r--. 1 vagrant vagrant   84 11月 10 06:30 2016 hello.c
[vagrant@localhost c]$ ./hello
hello world!

なお、Cでつまづきやすいと言われているポインタですが、アドレスの値を定義して(参照渡し)、データ型よりもメモリの消費を節約する仕組みで、より高速なプログラムが書けるようになります。

basicの操作

basicの拡張子はbasです。save、loadでファイルの保存呼び出しを行います。
コメント:rem

10 rem コメント
20 print "hello world"
25 print "hello world again"
>save "hello.bas"
>list
10 rem コメント
20 print "hello world"
25 print "hello world again"
>new
>list
>load "hello.bas"
>list
10 rem コメント
20 print "hello world"
25 print "hello world again"

goto文

>list
10 rem コメント
20 print "hello world"
25 print "hello world again"
30 goto 20
>

変数

>new
>10 x = 5
>20 print x
>run
5

if文

>new
>10 score = 80
>20 if score > 60 then print "ok"
>list
10 score = 80
20 if score > 60 then print "ok"
>run
ok

ifとgoto文

>20 if socre > 60 then goto 30 else goto 40
>30 print "ok"
>40 print "ng"
>30 print "ok": end
>run
ng

ループ

>new
>10 for i = 1 to 10
>20 print i
>30 next i
>list
10 for i = 1 to 10
20 print i
30 next i
>run
1
2
3
4
5
6
7
8
9
10

サブルーチン

>new
>10 print "hello"
>20 gosub 100
>30 end
>100 print "hello from subroutine!"
>110 return
>list
10 print "hello"
20 gosub 100
30 end
100 print "hello from subroutine!"
110 return
>run
hello
hello from subroutine!
>

サブルーチン

>new
>10 input "your age? ", age
>20 print "your are "; age; " years old!"
>list
10 input "your age? ",age
20 print "your are ";age;" years old!"
>ru
 Syntax error
>run
your age? 18
your are 18  years old!
>

basicの数当てゲーム

10 answer = int(rnd(1)*10)+1
20 input "guess (1-10)? ",guess
30 if guess > answer then print "too high!" : goto 20
40 if guess < answer then print "too low!" : goto 20
50 if guess = answer then print "you got it!" : end
>run
guess (1-10)? 4
you got it!

chipmunk Basic

Basicを使うには、コマンドラインでchipmunk Basicを使います。サイトからダウンロードして、フォルダにbasic.exeを入れて、以下のように移動します。

Desktop\basic> ./basic
Chipmunk BASIC v3.6.5(b6)
>

basicは行番号と命令を対にして書いていきます。なんとも言えない雰囲気ですね。
実行するにはrun、終了はquitを使います。
%e7%84%a1%e9%a1%8c

endは省略可能、listで命令文を表示します。

>list
10 cls
20 print "hello world"
30 end
>10 rem コメント
>list
10 rem コメント
20 print "hello world"
30 end
>25 print "hello world again"
>list
10 rem コメント
20 print "hello world"
25 print "hello world again"
30 end
>del 30
>list
10 rem コメント
20 print "hello world"
25 print "hello world again"

php さいころ

三項演算子で、条件判定し、trueならぞろ目と表示させています。

<?php

$dice = mt_rand(1, 6);
$dice2 = mt_rand(1, 6);

$zorome = ($dice == $dice2) ? true: false;

?>
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>さいころ</title>
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
  <h1>サイコロ</h1>
  <p>サイコロの目は「<?php echo $dice; ?>」と「<?php echo $dice2; ?>」でした!</p>
  <?php if ($zorome == true): ?>
    ぞろ目です!
  <?php endif; ?>
  <p><a href="<?php echo $_SERVER&#91;"SCRIPT_NAME"&#93;; ?>">もう一度!</a></p>
</body>
</html>

mt_rand()はrand()より、4倍処理が速いとphpのドキュメントに記載があります。
php mt_rand()

/*
Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function. The mt_rand() function is a drop-in replacement for this. It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.
*/

php おみくじ

phpで配列の数はcount、ランダムはmt_rand()、ページのURLは$_SERVER[“SCRIPT_NAME”];で取得します。

<?php

$omikuji = array('小吉','大吉','中吉','凶');

$result = $omikuji&#91;mt_rand(0, count($omikuji)-1)&#93;;
?>
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>おみくじ</title>
  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
  <h1>おみくじ</h1>
  <p>今日の運勢は「<?php echo $result; ?>」です!</p>
  <p><a href="<?php echo $_SERVER&#91;"SCRIPT_NAME"&#93;; ?>">もう一度</a></p>
</body>
</html>

PHPのcountに関するドキュメントです。
PHP; count-Manual

jQuery LocalStorage メモ帳

localStorage.getItemでデータを取得、localStorage.setItemでローカル保存、localStorage.removeItemでデータを削除します。保存した場合は、ブラウザを更新しても、データが残っております。 $(‘#memo’).bind(‘keyup’, function() のbind ‘keyup’は、キーボード入力時に、キーボードが上がった場合に呼び出されます。

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>メモ帳</title>
    <style>
    body {

    }
    </style>
  </head>
  <body>
    <h1>メモ帳</h1>
    <textarea id="memo" rows="10" cols="40" name="40"></textarea>
    <p><input type="button" id="clear" value="消去"></p>
    <script>
    $(function(){
      if (localStorage.getItem('memo')){
        $('#memo').val(localStorage.getItem('memo'));
      }

      $('#clear').click(function(){
        $('#memo').val('');
        localStorage.removeItem('memo');

      });

      $('#memo').bind('keyup', function(){
        localStorage.setItem('memo', $('#memo').val());
      });

    });
    </script>
  </body>
</html>

local storageのドキュメントはjqueryの公式サイトなどを参照ください。
https://plugins.jquery.com/tag/localstorage/

JavaScript 王様ゲーム

任意で一致しない複数の乱数を取得する場合は、Math.floor(Math.random()* (n+1))をdo whileで一致しない限りループさせて取得します。htmlに値を返すのは、document.getElementById(”).innerHTMLです。

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>王様ゲーム</title>
    <style>
    body {

    }
    </style>
  </head>
  <body>
    <h1>王様ゲーム</h1>
    <p>人数: <input type="text" id="num"></p>
    <p><input type="button" value="王様曰く!" onclick="kingSaid();"></p>
    <p id="result"></p>    
    <script>
      function kingSaid(){
        var orders = [
        'デコピンしなさい',
        'クイズを出しなさい',
        'ほめちぎりなさい',
        'ドリンクを注ぎなさい'
        ];
        var order = orders[Math.floor(Math.random()* orders.length)]; 

        var num = document.getElementById('num').value;
        var p1 = Math.floor(Math.random()* (num)) + 1;
        var p2;

        if (num < 2){
          p2 = 1;
        } else {
          do {
            p2 = Math.floor(Math.random()* (num)) + 1;
          } while (p1 == p2);

        }

        

        document.getElementById('result').innerHTML = 
          '#' + p1 + 'の人が#' + p2 + 'の人に' + order;
      }
    </script>
  </body>
</html>

JavaScript getTimeによる日付計算

date(year, month, day)をgetTime()で取得し、1000(ミリ秒) * 60(秒) * 60(分) * 24(時間)で割ります。 

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <title>日付計算の練習</title>
    <style>
    body {

    }
    </style>
  </head>
  <body>
    <h1>日付計算の練習</h1>
    <p>誕生日: <input type="text" name="birthday" id="birthday" value="1990-04-01"></p>
    <p><input type="button" value="計算!" onclick="getAge();"></p>
    <p>生まれてから<span id="daysPast">???</span>日経過していて、およそ<span id="age">???</span>歳です!</p>
    <script>
      function getAge(){
          var birthday = document.getElementById('birthday').value.split("-");

          var d1 = new Date(birthday[0], birthday[1]-1, birthday[2])
          var d2 = new Date();
          var diff = d2.getTime() - d1.getTime();
          var daysPast = Math.floor(diff / (1000 * 60 * 60 * 24));
          var age = Math.floor(daysPast / 365.25);

          document.getElementById('daysPast').innerHTML = daysPast;
          document.getElementById('age').innerHTML = age;
      }
    </script>
  </body>
</html>