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

📄 prompt.cal

📁 Calc Software Package for Number Calc
💻 CAL
字号:
/* * prompt - eemonstration of some uses of prompt() and eval() * * Copyright (C) 1999  Ernest Bowen * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL.  You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA. * * @(#) $Revision: 29.3 $ * @(#) $Id: prompt.cal,v 29.3 2006/06/20 09:29:16 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/prompt.cal,v $ * * Under source code control:	1995/12/18 04:43:25 * File existed as early as:	1995 * * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/ *//* * Demonstration of some uses of prompt() and eval(). * * adder() simulates a simple adding machine: starting with sum = 0, * each number entered in response to the ? prompt is added to sum * and the result displayed.  Operation of adder() is ended by * entering "end", "exit" or "quit"; "end" returns to the level from * which adder() is called, e.g. with: * *		for (;;) adder() * * entering "end" would start a new edition with sum = 0; "quit" and * "exit" return to the top level. * * Each response to ? is read as * a string terminated by a newline; the statements and expressions * in this string are compiled and evaluated as in function evaluation; * thus the string may include variables, assignments, functions, etc. * as in: * *		2 + 3 *		x = 2 + 3, x^3 *		x^2 *		local x = 2; while (x < 100) x *= 2; x % 100 *		x *		exp(2, 1e-5) *		sum *		print sum^2; *		3; print sum^2; * * (Here the second line creates x as a global variable; the local * variable x in the fourth line has no effect on the global x.	 In * the last three lines, sum is the sum of numbers already entered, so * the third last line doubles the value of sum.  The value returned * by "print sum^2;" is the null value, so the second last line adds * nothing to sum.  The last line returns the value 3, i.e. the last * non-null value found for the expressions separated by semicolons, * so sum will be increased by 3 after the "print sum^2;" command * is executed.	 xxx The terminating semicolon is essential in the * last two lines.  A command like eval("print 7;") is acceptable to * calc but eval("print 7") causes an exit from calc. xxx) * * If the value returned is not a number (e.g. the name of a list or matrix, * or if the string has syntax errors as in "2 + ", in which case the * value returned is an error value), the compile error messages and a * request for another number are displayed. * * Calling showvalues(str) assumes str defines a function of x as in: * *	"sin(x)", "x^2 + 3*x", "exp(x, 1e-5)". * * Values of the function so defined are returned for values of x * entered in reponse to the ? prompt.	Operation is terminated by * entering "end", "exit" or "quit". */define adder() {	global sum = 0;	local s, t;	for (;;) {		s = prompt("? ");		if (s == "end")			break;		t = eval(s);		if (!isnum(t)) {			print "Please enter a number";			continue;		}		sum += t;		print "\t":sum;	}}global prompt_x;define showvalues(str) {	local s;	for (;;) {		s = prompt("? ");		if (s == "end")			break;		prompt_x = eval(s);		if (!isnum(prompt_x)) {			print "Please enter a number";			continue;		}		print "\t":eval(str);	}}

⌨️ 快捷键说明

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