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

📄 bc.1

📁 Unix操作系统minix 2.0源码
💻 1
📖 第 1 页 / 共 3 页
字号:
.RSa = 3 < 5.RE.PPMost C programmers would assume this would assign the result of "3 <5" (the value 1) to the variable "a".  What this does in \fBbc\fR isassign the value 3 to the variable "a" and then compare 3 to 5.  It isbest to use parenthesis when using relational and logical operatorswith the assignment operators..PPThere are a few more special expressions that are provided in \fBbc\fR.These have to do with user defined functions and standardfunctions.  They all appear as "\fIname\fB(\fIparameters\fB)\fR".See the section on functions for user defined functions.  The standardfunctions are:.IP "length ( expression )"The value of the length function is the number of significant digits in theexpression..IP "read ( )"The read function (an extension) will read a number from the standardinput, regardless of where the function occurs.   Beware, this cancause problems with the mixing of data and program in the standard input.The best use for this function is in a previously written program thatneeds input from the user, but never allows program code to be inputfrom the user.  The value of the read function is the number read fromthe standard input using the current value of the variable \fBibase\fR for the conversion base..IP "scale ( expression )"The value of the scale function is the number of digits after the decimalpoint in the expression..IP "sqrt ( expression )"The value of the sqrt function is the square root of the expression.  Ifthe expression is negative, a run time error is generated..SS STATEMENTSStatements (as in most algebraic languages) provide the sequencing ofexpression evaluation.  In \fBbc\fR statements are executed "as soonas possible."  Execution happens when a newline in encountered andthere is one or more complete statements.  Due to this immediateexecution, newlines are very important in \fBbc\fR. In fact, both asemicolon and a newline are used as statement separators.  Animproperly placed newline will cause a syntax error.  Because newlinesare statement separators, it is possible to hide a newline by usingthe backslash character.  The sequence "\e<nl>", where <nl> is thenewline appears to \fBbc\fR as whitespace instead of a newline.  Astatement list is a series of statements separated by semicolons andnewlines.  The following is a list of \fBbc\fR statements and whatthey do: (Things enclosed in brackets ([]) are optional parts of thestatement.).IP "expression"This statement does one of two things.  If the expression starts with"<variable> <assignment> ...", it is considered to be an assignmentstatement.  If the expression is not an assignment statement, theexpression is evaluated and printed to the output.  After the numberis printed, a newline is printed.  For example, "a=1" is an assignmentstatement and "(a=1)" is an expression that has an embeddedassignment.  All numbers that are printed are printed in the basespecified by the variable \fBobase\fR. The legal values for \fBobase\fR are 2 through BC_BASE_MAX.  (See the section LIMITS.)  Forbases 2 through 16, the usual method of writing numbers is used.  Forbases greater than 16, \fBbc\fR uses a multi-character digit methodof printing the numbers where each higher base digit is printed as abase 10 number.  The multi-character digits are separated by spaces.Each digit contains the number of characters required to represent thebase ten value of "obase-1".  Since numbers are of arbitraryprecision, some numbers may not be printable on a single output line.These long numbers will be split across lines using the "\e" as thelast character on a line.  The maximum number of characters printedper line is 70.  Due to the interactive nature of \fBbc\fR printinga number cause the side effect of assigning the printed value the thespecial variable \fBlast\fR. This allows the user to recover thelast value printed without having to retype the expression thatprinted the number.  Assigning to \fBlast\fR is legal and willoverwrite the last printed value with the assigned value.  The newlyassigned value will remain until the next number is printed or anothervalue is assigned to \fBlast\fR..IP "string"The string is printed to the output.  Strings start with a double quotecharacter and contain all characters until the next double quote character.All characters are take literally, including any newline.  No newlinecharacter is printed after the string..IP "\fBprint\fR list"The print statement (an extension) provides another method of output.The "list" is a list of strings and expressions separated by commas.Each string or expression is printed in the order of the list.  Noterminating newline is printed.  Expressions are evaluated and theirvalue is printed and assigned the the variable \fBlast\fR. Stringsin the print statement are printed to the output and may containspecial characters.  Special characters start with the backslashcharacter (\e).  The special characters recognized by \fBbc\fR are"b" (bell), "f" (form feed), "n" (newline), "r" (carriage return), "t"(tab), and "\e" (backslash).  Any other character following thebackslash will be ignored.  This still does not allow the double quotecharacter to be part of any string..IP "{ statement_list }"This is the compound statement.  It allows multiple statements to begrouped together for execution..IP "\fBif\fR ( expression ) \fBthen\fR statement1 [\fBelse\fR statement2]"The if statement evaluates the expression and executes statement1 orstatement2 depending on the value of the expression.  If the expressionis non-zero, statement1 is executed.  If statement2 is present andthe value of the expression is 0, then statement2 is executed.  (Theelse clause is an extension.).IP "\fBwhile\fR ( expression ) statement"The while statement will execute the statement while the expressionis non-zero.  It evaluates the expression before each execution ofthe statement.   Termination of the loop is caused by a zeroexpression value or the execution of a break statement..IP "\fBfor\fR ( [expression1] ; [expression2] ; [expression3] ) statement"The for statement controls repeated execution of the statement.  Expression1 is evaluated before the loop.  Expression2 is evaluatedbefore each execution of the statement.  If it is non-zero, the statementis evaluated.  If it is zero, the loop is terminated.  After eachexecution of the statement, expression3 is evaluated before the reevaluationof expression2.  If expression1 or expression3 are missing, nothing isevaluated at the point they would be evaluated.If expression2 is missing, it is the same as substitutingthe value 1 for expression2.  (The optional expressions are anextension. POSIX \fBbc\fR requires all three expressions.)The following is equivalent code for the for statement:.nf.RSexpression1;while (expression2) {   statement;   expression3;}.RE.fi.IP "\fBbreak\fR"This statement causes a forced exit of the most recent enclosing whilestatement or for statement..IP "\fBcontinue\fR"The continue statement (an extension)  causes the most recent enclosingfor statement to start the next iteration..IP "\fBhalt\fR"The halt statement (an extension) is an executed statement that causesthe \fBbc\fR processor to quit only when it is executed.  For example,"if (0 == 1) halt" will not cause \fBbc\fR to terminate because the halt isnot executed..IP "\fBreturn\fR"Return the value 0 from a function.  (See the section on functions.).IP "\fBreturn\fR ( expression )"Return the value of the expression from a function.  (See the section on functions.).SS PSEUDO STATEMENTSThese statements are not statements in the traditional sense.  They arenot executed statements.  Their function is performed at "compile" time..IP "\fBlimits\fR"Print the local limits enforced by the local version of \fBbc\fR.  Thisis an extension..IP "\fBquit\fR"When the quit statement is read, the \fBbc\fR processoris terminated, regardless of where the quit statement is found.  Forexample, "if (0 == 1) quit" will cause \fBbc\fR to terminate..IP "\fBwarranty\fR"Print a longer warranty notice.  This is an extension..SS FUNCTIONSFunctions provide a method of defining a computation that can be executedlater.  Functions in .B bcalways compute a value and return it to the caller.  Function definitionsare "dynamic" in the sense that a function is undefined until a definitionis encountered in the input.  That definition is then used until anotherdefinition function for the same name is encountered.  The new definitionthen replaces the older definition.  A function is defined as follows:.nf.RS\fBdefine \fIname \fB( \fIparameters \fB) { \fInewline\fI    auto_list   statement_list \fB}\fR.RE.fiA function call is just an expression of the form"\fIname\fB(\fIparameters\fB)\fR"..PPParameters are numbers or arrays (an extension).  In the function definition,zero or more parameters are defined by listing their names separated bycommas.  Numbers are only call by value parameters.  Arrays are onlycall by variable.  Arrays are specified in the parameter definition bythe notation "\fIname\fB[]\fR".   In the function call, actual parametersare full expressions for number parameters.  The same notation is usedfor passing arrays as for defining array parameters.  The named array ispassed by variable to the function.  Since function definitions are dynamic,parameter numbers and types are checked when a function is called.  Anymismatch in number or types of parameters will cause a runtime error.A runtime error will also occur for the call to an undefined function..PPThe \fIauto_list\fR is an optional list of variables that are for"local" use.  The syntax of the auto list (if present) is "\fBauto\fIname\fR, ... ;".  (The semicolon is optional.)  Each \fIname\fR isthe name of an auto variable.  Arrays may be specified by using thesame notation as used in parameters.  These variables have theirvalues pushed onto a stack at the start of the function.  Thevariables are then initialized to zero and used throughout theexecution of the function.  At function exit, these variables arepopped so that the original value (at the time of the function call)of these variables are restored.  The parameters are really autovariables that are initialized to a value provided in the functioncall.  Auto variables are different than traditional local variablesin the fact that if function A calls function B, B may access functionA's auto variables by just using the same name, unless function B hascalled them auto variables.  Due to the fact that auto variables andparameters are pushed onto a stack, \fBbc\fR supports recursive functions..PPThe function body is a list of \fBbc\fR statements.  Again, statementsare separated by semicolons or newlines.  Return statements cause thetermination of a function and the return of a value.  There are twoversions of the return statement.  The first form, "\fBreturn\fR", returnsthe value 0 to the calling expression.  The second form, "\fBreturn ( \fIexpression \fB)\fR", computes the value of the expressionand returns that value to the calling expression.  There is an implied"\fBreturn (0)\fR" at the end of every function.  This allows a functionto terminate and return 0 without an explicit return statement..PPFunctions also change the usage of the variable \fBibase\fR.  Allconstants in the function body will be converted using the value of\fBibase\fR at the time of the function call.  Changes of \fBibase\fRwill be ignored during the execution of the function except for thestandard function \fBread\fR, which will always use the current valueof \fBibase\fR for conversion of numbers..SS MATH LIBRARYIf \fBbc\fR is invoked with the \fB-l\fR option, a math library is preloadedand the default scale is set to 20.   The math functions will calculate theirresults to the scale set at the time of their call.  The math library defines the following functions:.IP "s (\fIx\fR)"The sine of x in radians..IP "c (\fIx\fR)"The cosine of x in radians..IP "a (\fIx\fR)"The arctangent of x..IP "l (\fIx\fR)"The natural logarithm of x..IP "e (\fIx\fR)"The exponential function of raising e to the value x..IP "j (\fIn,x\fR)"The bessel function of integer order n of x..SS EXAMPLESIn /bin/sh,  the following will assign the value of "pi" to the shellvariable \fBpi\fR..RS\fBpi=$(echo "scale=10; 4*a(1)" | bc -l)\fR.RE

⌨️ 快捷键说明

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