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

📄 bc

📁 UNIX v6源代码 这几乎是最经典的unix版本 unix操作系统设计和莱昂氏unix源代码分析都是用的该版
💻
📖 第 1 页 / 共 2 页
字号:
are not disturbed.Functions may be called recursively and the automatic variablesat each level of call are protected.The parameters named in a function definition are treated inthe same way as the automatic variables of that functionwith the single exception that they are given a valueon entry to the function.An example of a function definition is.DS	define a(x,y){		auto z		z = x*y		return(z)	}.DEThe value of this function, when called, will be theproduct of itstwo arguments..PPA function is called by the appearance of its namefollowed by a string of arguments enclosed inparentheses and separated by commas.The resultis unpredictable if the wrong number of arguments is used..PPFunctions with no arguments are defined and called usingparentheses with nothing between them: b()..PPIf the function.ft Ia.ftabove has been defined, then the line.DSa(7,3.14).DEwould cause the result 21.98 to be printed and the line.DSx = a(a(3,4),5).DEwould cause the value of x to become 60..SHSubscripted Variables.PPA single lower-case letter variable namefollowed by an expression in brackets is called a subscriptedvariable (an array element).The variable name is called the array name and the expressionin brackets is called the subscript.Only one-dimensional arrays arepermitted.  The names of arrays are permitted tocollide with the names of simple variables and function names.Any fractionalpart of a subscript is discarded before use.Subscripts must be greater than or equal to zero and less than or equal to 2047..PPSubscripted variables may be freely used in expressions, infunction calls, and in return statements..PPAn array name may be used as an argument to a function,or may be declared as automatic ina function definition by the use of empty brackets:.DSf(a[\|])define f(a[\|])auto a[\|].DEWhen an array name is so used, the whole contents of the arrayare copied for the use of the function, and thrown away on exitfrom the function.Array names which refer to whole arrays cannot be usedin any other contexts..SHControl Statements.PPThe `if', the `while', and the `for' statementsmay be used to alter the flow within programs or to cause iteration.The range of each of them is a statement ora compound statement consisting of a collection ofstatements enclosed in braces.They are written in the following way.DSif(relation) statementwhile(relation) statementfor(expression1; relation; expression2) statement.DEor.DSif(relation) {statements}while(relation) {statements}for(expression1; relation; expression2) {statements}.DE.PPA relation in one of the control statements is an expression of the form.DSx>y.DEwhere  two expressions are related by one of the six relationaloperators <, >, <=, >=, ==, or !=.The relation ==stands for `equal to' and != stands for `not equal to'.The meaning of the remaining relational operators isclear..PPBEWARE of using = instead of == in a relational.  Unfortunately,both of them are legal, so you will not get a diagnosticmessage, but = really will not do a comparison..PPThe `if' statement causes execution of its rangeif and only if the relation is true.Then control passes to the next statement in sequence..PPThe `while' statement causes execution of its rangerepeatedly as long as the relationis true.  The relation is tested before each executionof its range and if the relationis false, control passes to the next statement beyond the rangeof the while..PPThe `for' statement beginsby executing `expression1'.  Then the relation is testedand, if true, the statements in the range of the `for' are executed.Then `expression2' is executed.  The relation is tested, and so on.The typical use of the `for' statement is for a controlled iteration,as in the statement.DSfor(i=1; i<=10; i=i+1) i.DEwhich will print the integers from 1 to 10.Here are some examples of the use of the control statements..DSdefine f(n){auto i, xx=1for(i=1; i<=n; i=i+1) x=x*ireturn(x)}.DEThe line.DS	f(a).DEwill print.ft Ia.ftfactorial if.ft Ia.ftis a positive integer.Here is the definition of a function which willcompute values of the binomial coefficient(m and n are assumed to be positive integers)..DSdefine b(n,m){auto x, jx=1for(j=1; j<=m; j=j+1) x=x*(n\-j+1)/jreturn(x)}.DEThe following function computes values of the exponential functionby summing the appropriate serieswithout regard for possible truncation errors:.DSscale = 20define e(x){	auto a, b, c, d, n	a = 1	b = 1	c = 1	d = 0	n = 1	while(1==1){		a = a*x		b = b*n		c = c + a/b		n = n + 1		if(c==d) return(c)		d = c	}}.DE.SHSome Details.PPThere are some language features that every user should knowabout even if he will not use them..PPNormally statements are typed one to a line.  It is also permissibleto type several statements on a line separated by semicolons..PPIf an assignment statement is parenthesized, it then hasa value and it can be used anywhere that an expression can.For example, the line.DS(x=y+17).DEnot only makes the indicated assignment, but also prints theresulting value..PPHere is an example of a use of the value of anassignment statement even when it is not parenthesized..DSx = a[i=i+1].DEcauses a value to be assigned to x and also increments ibefore it is used as a subscript..PPThe following constructs work in BC in exactly the same manneras they do in the C language.  Consult the appendix or theC manuals [2,3] for their exact workings..DS.ta 2ix=y=z  is the same as	x=(y=z)x =+ y	x = x+yx =\- y	x = x\-yx =* y	x = x*yx =/ y	x = x/yx =% y	x = x%yx =^ y	x = x^yx++	(x=x+1)\-1x\-\-	(x=x\-1)+1++x	x = x+1\-\-x	x = x\-1.DEEven if you don't intend to use the constructs,if you type one inadvertently, something correct but unexpectedmay happen..PPWARNING!  In some of these constructions, spaces aresignificant.There is a real difference betweenx=\-y and x= \-y.The first replaces x by x\-y and the second by \-y..SHThree Important Things.PP1.  To exit a BC program, type `quit'..PP2. There is a comment convention identical to that of C andof PL/I.  Comments begin with `/*' and end with `*/'..PP3. There is a library of math functions which may be obtained bytyping at command level.DSbc \-l.DEThis command will load a set of library functionswhich, at the time of writing, consists of sine (named `s'),cosine (`c'), arctangent (`a'), natural logarithm (`l'),exponential (`e') and Bessel functions of integer order (`j(n,x)').  Doubtless more functions will be addedin time.The library sets the scale to 20.  You can reset it to somethingelse if you like.The design of these mathematical library routinesis discussed elsewhere [4]..PPIf you type.DSbc file ....DEBC will read and execute the named file or files before acceptingcommands from the keyboard.  In this way, you may load yourfavorite programs and function definitions..SHAcknowledgement.PPThe compiler is written in YACC [5]; its originalversion  was written by S. C. Johnson..SHReferences.IP[1]K. Thompson and D. M. Ritchie,.ft IUNIX Programmer's Manual,.ftFifth Edition (1974).IP[2]D. M. Ritchie,.ft IC Reference Manual,.ft.IP[3]B. W. Kernighan,.ft IProgramming in C: A Tutorial,.ft.IP[4]Robert Morris,.ft IA Library of Reference Standard Mathematical Subroutines,.ft.IP[5]S. C. Johnson,.ft IYACC, Yet Another Compiler-Compiler,.ft.IP[6]R. Morris and L. L. Cherry,.ft IDC \- An Interactive Desk Calculator,.ft.LP

⌨️ 快捷键说明

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