デモプログラム — プログラミング言語 Ring 1.14 取扱説明書

Skip to content

# デモプログラム

デモプログラムを紹介します。

- 対話型プログラミング言語シェル

- メインメニュー

## 対話型プログラミング言語シェル

このプログラムでは対話型プログラミング環境を作成します。

    while true
            see nl + "code:> "
            give cCode
            try
                    eval(cCode)
            catch
                    see cCatchError
            done
    end

実行結果:

    code:> see "hello world"
    hello world
    code:> for x = 1 to 10 see x + nl next
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    code:> func test see "Hello from test" + nl

    code:> test()
    Hello from test

    code:> bye

## メインメニュー

用例:

    # デモプログラム

    while true

            see "

            Main Menu
            ===========
            [1] Say Hello
            [2] Sum two numbers
            [3] Stars
            [4] Fact
            [5] Exit

            " give nMenu see nl

            # IF-BUT-ELSE-OK の代わりに Switch-ON-Other-OFF を使えます。

            Switch nMenu
            On 1 sayhello()
            On 2 Sum()
            On 3 Stars()
            On 4
                    see "Enter Number : " give x
                    see "Output : "

                    Try
                            see Fact(number(x))
                    Catch
                            see "Error in parameters!" + nl
                    Done

            On "5" return
            Other see "bad option" + nl
            Off

    end

    func sayhello
            see "Enter your name ? " give fname
            see "Hello " + fname + nl

    func sum
            see "number 1 : " give num1 see "number 2 : " give num2
            see "Sum : " see 0 + num1 + num2

    func stars
            for x = 1 to 10
                    see space(8)
                    for y = 1 to x see "*" next see nl
            next

    func fact x if x = 0 return 1 else return x * fact(x-1) ok

    func space x y = "" for t=1 to x y += " " next return y

実行結果:

            Main Menu
            ===========
            [1] Say Hello
            [2] Sum two numbers
            [3] Stars
            [4] Fact
            [5] Exit

            1

    Enter your name ? Mahmoud Fayed
    Hello Mahmoud Fayed

            Main Menu
            ===========
            [1] Say Hello
            [2] Sum two numbers
            [3] Stars
            [4] Fact
            [5] Exit

            2

    number 1 : 3
    number 2 : 4
    Sum : 7

            Main Menu
            ===========
            [1] Say Hello
            [2] Sum two numbers
            [3] Stars
            [4] Fact
            [5] Exit

            3

            *
            **
            ***
            ****
            *****
            ******
            *******
            ********
            *********
            **********

            Main Menu
            ===========
            [1] Say Hello
            [2] Sum two numbers
            [3] Stars
            [4] Fact
            [5] Exit

            4

    Enter Number : 5
    Output : 120

            Main Menu
            ===========
            [1] Say Hello
            [2] Sum two numbers
            [3] Stars
            [4] Fact
            [5] Exit

            5