⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yabasic.htm

📁 一个小巧的BASIC解释器的源代码很小可它的确做到了
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<dl>    <dd>Finally note that the <font face="Courier New"><strong>atan()</strong></font>-function        comes in <strong>two flavors</strong>: Called with a        single argument (e.g. <font face="Courier New"><strong>atan(2)</strong></font>)        <font face="Courier New"><strong>atan()</strong></font>returns        a value between -pi/2 ... +pi/2. Called with two        arguments (e.g. <font face="Courier New"><strong>atan(2,-1)</strong></font>)        <font face="Courier New"><strong>atan()</strong></font>        returns a value between -pi and +pi; (This can be useful        e.g. when transforming from cartesian to polar        coordinates).</dd>    <dt><a name="Exponentiation:"><strong>Exponentiation:</strong></a></dt>    <dd>The <font face="Courier New"><strong>exp()</strong></font>        functions comes with its inverse. the <font        face="Courier New"><strong>log()</strong></font>-function:<font        face="Courier New"><strong> <br>        print exp(1),log(2),log(euler) </strong></font><strong><br>        </strong><font face="Courier New"><strong>log()</strong></font>        and <font face="Courier New"><strong>exp()</strong></font>        operate with the base e (=2.17828), which comes as a        predefined variable named <font face="Courier New"><strong>euler</strong></font>.        Knowing this you won't be surprised to get the following        output:<br>        <font face="Courier New"><strong>2.71828 0.693147 1</strong></font></dd>    <dt><a name="Integer and fractional parts:"><strong>Integer        and fractional parts:</strong></a></dt>    <dd>The functions<font size="2" face="Courier New"><strong>        int() </strong></font>and<font size="2"        face="Courier New"><strong> frac() </strong></font>split        their argument at the decimal point:<br>        <font face="Courier New"><strong>print        int(2.34),frac(2.34)</strong></font><strong> </strong>produces:        <font face="Courier New"><strong>2 0.34</strong></font></dd>    <dt><a name="Absolut values and signum"><strong>Absolut        values and signum</strong></a></dt>    <dd>The<font size="2" face="Courier New"><strong> abs() </strong></font>and<font        size="2" face="Courier New"><strong> sig() </strong></font>functions        return the absolute value and the signum of their        arguments:<br>        <font size="2" face="Courier New"><strong>print        abs(-2.34),abs(2.34),sig(-2.34),sig(0),sig(2.34) </strong></font>produces:<font        size="2" face="Courier New"><strong> 2.34 2.34 -1 0 1</strong></font></dd>    <dt><a name="Remainder"><strong>Remainder</strong></a></dt>    <dd>To get the remainder of a division employ the <font        face="Courier New"><strong>mod()</strong></font>-function;        e.g. <font face="Courier New"><strong>mod(11,4)</strong></font>        produces <font face="Courier New"><strong>3</strong></font>,        because when dividing 11 by 4 you get 2 and a remainder        of 3.</dd>    <dt><a name="Minimum and Maximum:"><strong>Minimum and        Maximum:</strong></a></dt>    <dd>Return the lower and higher value of their two arguments:<br>        <font face="Courier New"><strong>print min(2,3),max(2,3)</strong></font><strong>        </strong>gives: <font face="Courier New"><strong>2 3</strong></font></dd>    <dt><a name="The square root"><strong>Square root and square:</strong></a></dt>    <dd>The square root is calculated by <font face="Courier New"><strong>sqrt()</strong></font>,        the square by<font face="Courier New"><strong> sqr()</strong></font>:        <font face="Courier New"><strong><br>        print sqrt(2),sqr(2) </strong></font>gives <font        face="Courier New"><strong>1.41421 4</strong></font></dd>    <dt><a name="Random numbers"><strong>Random numbers</strong></a><strong>:</strong></dt>    <dd>are returned by the <font face="Courier New"><strong>ran()</strong></font>-function;        this function comes in <strong>two flavours</strong>:        Called without arguments (e.g. <font face="Courier New"><strong>print        ran()</strong></font>) you will get a random number        between 0 and 1. Called with a single argument (e.g. <font        face="Courier New"><strong>print ran(2)</strong></font>)        you will get a random number between 0 and the supplied        argument.<br>        The <font face="Courier New"><strong>ran()</strong></font>-function        of yabasic uses the <font face="Courier New"><strong>ran()</strong></font>-function        of the C standard library, so you had better not expect        too much randomness ...</dd></dl><p><a href="#Table of contents">Back to table of contents ...</a></p><hr><h2><a name="Making decisions">Making decisions: The <fontsize="4" face="Courier New"><strong>if</strong></font>-statement</a></h2><p>To make decisions you have to use the <font face="Courier New"><strong>if</strong></font>-statement:</p><blockquote>    <pre><strong>input &quot;Please enter a number&quot; aif (a&gt;10) then  print &quot;Your number is bigger than 10&quot;elsif (a&gt;5) then  print &quot;Your number is bigger than 5 but less or equal 10&quot;else  print &quot;Your number is less or equal 5&quot;endif</strong></pre></blockquote><p>As you can see, the condition has to be enclosed inparentheses (...). The <font face="Courier New"><strong>else</strong></font>and the <font face="Courier New"><strong>elsif</strong></font>-partof the <font face="Courier New"><strong>if</strong></font>-statementare optional and can be omitted, as in this example:</p><blockquote>    <pre><strong>input &quot;Please enter a number&quot; aif (a&gt;10 and a&lt;20) then  print &quot;bigger than 10&quot;:print &quot;but less than 20&quot; fi</strong></pre></blockquote><p><a name="Note that endif">Note that <font face="Courier New"><strong>endif</strong></font></a>can be written as <font face="Courier New"><strong>fi</strong></font>too. </p><p>Next, have a look at the condition <font face="Courier New"><strong>(a&gt;10and a&lt;20)</strong></font>of the <font face="Courier New"><strong>if</strong></font>-statement:</p><dl>    <dt><a name="Conditions"><strong>Conditions</strong></a><strong>:</strong></dt>    <dd>Numbers or arithmetic expressions can be compared with        the usual relational operators: <font face="Courier New"><strong>=</strong></font>        (equal), <font face="Courier New"><strong>&lt;&gt;</strong></font>        (not equal), <font face="Courier New"><strong>&lt;</strong></font>        (less than), <font face="Courier New"><strong>&lt;=</strong></font>        (less or equal), <font face="Courier New"><strong>&gt;</strong></font>        (greater than) and <font face="Courier New"><strong>&gt;=</strong></font><strong>        </strong>(greater or equal).<br>        Strings can be compared with just the same set of        operators, where characters are ordered according to the        ascii-charset; e.g. <font face="Courier New"><strong>(&quot;a&quot;&lt;&quot;b&quot;)</strong></font>        is true (because <font face="Courier New"><strong>&quot;a&quot;</strong></font>        precedes <font face="Courier New"><strong>&quot;b&quot;</strong></font>        within the ascii-charset) and likewise <font        face="Courier New"><strong>(&quot;a&quot;=&quot;b&quot;)</strong></font>        is false.<br>        More than one comparison can be combined with parentheses        <font face="Courier New"><strong>()</strong></font> and        these keywords: <font face="Courier New"><strong>or</strong></font>,        <font face="Courier New"><strong>and</strong></font>, <font        face="Courier New"><strong>not</strong></font>; Note that        <font face="Courier New"><strong>not</strong></font>        precedes <font face="Courier New"><strong>and</strong></font>,        which in turn precedes <font face="Courier New"><strong>or</strong></font>        (in the same way as <font face="Courier New"><strong>*</strong></font>        precedes <font face="Courier New"><strong>+</strong></font>        within arithmetic expressions).<br>        Finally, the enclosing parantheses can be omitted, i.e.<font        face="Courier New"><strong> if a&lt;10 then ... </strong></font>is        a valid statement.</dd>    <dt><a name="Multiple commands on one line"><strong>Multiple        commands on one line</strong></a></dt>    <dd>Note that more than one command can appear on one line,        as in <font face="Courier New"><strong><br>        print &quot;bigger than 10&quot;:print &quot;but less        than 20&quot;<br>        </strong></font>as long as you separate them with colons        (:).</dd></dl><p><a href="#Table of contents">Back to table of contents ...</a></p><hr><h2><a name="Strings and loops">Strings and loops</a></h2><p>Basic has always been simple and strong in string-processing;and yabasic also tries to continue in this tradition:</p><blockquote>    <pre><strong>input &quot;Please enter a word&quot; a$for a=len(a$) to 1 step -1:print mid$(a$,a,1);:next aprint &quot; is &quot;,a$,&quot; reversed !&quot;</strong></pre></blockquote><p>If you try this program, you will get this output:</p><blockquote>    <pre><strong>Please enter a word: </strong><em><strong><u>hello</u></strong></em><strong>olleh is hello reversed !</strong></pre></blockquote><dl>    <dt><a name="for-next-loop"><strong>for-next-loop</strong></a></dt>    <dd>The heart of the above program is the for-loop:        everything from <font face="Courier New"><strong>for</strong></font>        to <font face="Courier New"><strong>next</strong></font>        is repeated, while the variable (<font face="Courier New"><strong>a</strong></font>)        goes from its initial value <font face="Courier New"><strong>len(a$)</strong></font>        to its final value <font face="Courier New"><strong>1</strong></font>.        As you might have anticipated, <font face="Courier New"><strong>len(a$)</strong></font>        returns the length of its string-argument.<br>        Note the <font face="Courier New"><strong>step</strong></font>-clause:        the number after <font face="Courier New"><strong>step</strong></font>        (here: <font face="Courier New"><strong>-1</strong></font>)        is added to <font face="Courier New"><strong>a</strong></font>        after every repetition; in the example the <font        face="Courier New"><strong>step</strong></font>-clause        makes <font face="Courier New"><strong>a</strong></font>        go down with every iteration. If you omit the        step-clause, <font face="Courier New"><strong>step 1</strong></font>        is assumed. Finally note, that you can leave the for next        loop <strong>at any time</strong> by simple <font        face="Courier New"><strong>goto</strong></font>. You may        put the for-loop to some offbeat usage; e.g. <font        size="3" face="Courier New"><strong>for a=1 to 100 step        a:print a:next a</strong></font><font size="4"> </font>prints        the powers off 2 up to 64.</dd></dl><p>Within the for-next-loop above the string-functions <fontface="Courier New"><strong>len()</strong></font> and <fontface="Courier New"><strong>mid$()</strong></font> are applied,but there are many more string functions:</p><dl>    <dt><a name="Getting pieces out of a string:"><strong>Getting        pieces out of a string:</strong></a></dt>    <dd>There are three functions which give back parts of a        string:<br>        <font face="Courier New"><strong>a$=&quot;123456&quot;<br>        print        left$(a$,2),&quot;-&quot;,mid$(a$,2,3),&quot;-&quot;,right$(a$,3)</strong></font><br>        gives you the following output:<br>        <font face="Courier New"><strong>12-234-456<br>        </strong></font>As you see <font face="Courier New"><strong>left$()</strong></font>        cuts off as many characters as specified by its second        argument from the left of your string. <font        face="Courier New"><strong>right$()</strong></font> cuts        from the right, and <font face="Courier New"><strong>mid$()</strong></font>        cuts in the middle, where the first argument is the        starting point and the second one is the length of the        string to be cut out.<br>        Furthermore <font face="Courier New"><strong>mid$()</strong></font>        and its friends can even be used to selectively change        parts of a string:<br>        <font face="Courier New"><strong>a$=&quot;123456&quot;:left$(a$,2)=&quot;abcd&quot;:print        a$<br>        </strong></font>results in<br>        <font face="Courier New"><strong>ab3456<br>        </strong></font>As you see only the two leftmost        characters are changed (even though the string <font        face="Courier New"><strong>&quot;abcd&quot;</strong></font>        contains four characters); the same can be done with <font        face="Courier New"><strong>mid$()</strong></font> or <font        face="Courier New"><strong>right$()</strong></font>.</dd>    <dt><a name="strings to numbers (and reverse):"><strong>strings        to numbers (and reverse):</strong></a></dt>    <dd>The function <font face="Courier New"><strong>str$()</strong></font>converts        its numeric argument to a string: <br>        <font face="Courier New"><strong>print str$(12)</strong></font>        gives the string &quot;<font face="Courier New"><strong>12</strong></font>&quot;        as a result. The formatting of the number can be        influenced by an optional second argument: <font        face="Courier New"><strong>print        str$(12.123455,&quot;##.##&quot;)</strong></font> returns        the string <font face="Courier New"><strong>12.12</strong></font>.        The second argument has the same effect as the format of        the <a href="#print using"><font face="Courier New">print        using</font></a> statement.<br>        Just the opposite is done by the function <font        face="Courier New"><strong>val()</strong></font>: <font        face="Courier New"><strong>print 2+val(&quot;23&quot;)</strong></font>        gives <font face="Courier New"><strong>25</strong></font>        as a result, whereas <font face="Courier New"><strong>print        val(&quot;e2&quot;)</strong></font> delivers <font        face="Courier New"><strong>0</strong></font> (because <font        face="Courier New"><strong>&quot;e2&quot;</strong></font>        is not a valid number).</dd>    <dt><a name="The Ascii-Charset:"><strong>The ascii-charset:</strong></a></dt>    <dd>yabasic offers two functions to work with the        ascii-charset. <font face="Courier New"><strong>asc()</strong></font>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -