JavaScript所见即所得公式生成器和函数创建
是否有任何简单的方程式生成器(用于基本方程式-无需矩阵),不仅可以创建可读和可编辑的公式,还可以创建可调用的函数JavaScript所见即所得公式生成器和函数创建,javascript,math,Javascript,Math,是否有任何简单的方程式生成器(用于基本方程式-无需矩阵),不仅可以创建可读和可编辑的公式,还可以创建可调用的函数 我认为这与您要求的最接近..javascript计算器。在网上找到的。经过测试,效果良好。也试试看。当然,您可以添加一些功能 <div id="display" class="display"></div><ul> <li>7</li> <li>8</li> <l
我认为这与您要求的最接近..javascript计算器。在网上找到的。经过测试,效果良好。也试试看。当然,您可以添加一些功能
<div id="display" class="display"></div><ul>
<li>7</li>
<li>8</li>
<li>9</li>
<li>C</li>
<li>AC</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>x</li>
<li>/</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>+</li>
<li>-</li>
<li>0</li>
<li>.</li>
<li>=</li>
</ul>
.display {
border: 1px solid #AAA;
padding: 20px;
font-size: 20px;
width: 295px;
text-align: right;
height: 25px;}ul {
margin: 0;
padding:0;
overflow: hidden;
border: 1px solid #CCC;
width: 335px;}li {
float: left;
border: 1px solid #CCC;
background-color: #555;
color: White;
padding: 20px;
width: 25px;
height: 20px;
text-align: center;}/**5.12 6.12 0.12 96.12 6.65 6.6* 6.634 Works normally: 1.65, 2.65, 3.65, 8.65, 9.65, 10.65 Innaccurate decimal: 4.65, 5.65, 6.65, 7.65,
Works normally: 1.12, 2.12, 3.12, 8.12, 9.12, 10.12 Innaccurate decimal: 4.12, 5.12, 6.12, 7.12, *// var Calculator = function() { this.total = 0; this.current = 0;
this.operation = null;
this.fractionExp = 0;
}; Calculator.prototype = {
add: function() {
this.calculate();
this.operation = function(a, b) {
return a + b;
};
this.setCurrent(0, "");
this.fractionExp = 0; // decimal fix
},
subtract: function() {
this.calculate();
this.operation = function(a, b) {
return a - b;
};
this.setCurrent(0, "");
this.fractionExp = 0; // decimal fix
},
multiply: function() {
this.calculate();
this.operation = function(a, b) {
return a * b;
};
this.setCurrent(0, "");
this.fractionExp = 0; // decimal fix
},
divide: function() {
this.calculate();
this.operation = function(a, b) {
return a / b;
};
this.setCurrent(0, "");
this.fractionExp = 0; // decimal fix
},
clear: function() {
this.setCurrent(0, "");
this.fractionExp = 0; // decimal fix
},
allClear: function() {
this.clear();
this.total = 0;
this.operation = null;
console.log("total: " + this.total);
this.fractionExp = 0; // decimal fix
},
calculate: function() {
if (this.operation) {
this.total = this.operation(this.total, this.current);
} else {
this.total = this.current;
}
console.log("total: " + this.total);
},
equals: function() {
this.calculate();
this.updateDisplay(this.total);
},
digit: function(number) {
// shift current left, add number
// this is the key the decimal point
if (this.fractionExp) {
// not corrent but close
this.setCurrent(this.current + (number / Math.pow(10, this.fractionExp)));
this.fractionExp = this.fractionExp + 1
} else {
this.setCurrent((this.current * 10) + number);
};
},
point: function() {
this.fractionExp = 1;
},
setCurrent: function(value, text) {
this.current = value;
this.updateDisplay(typeof text == "undefined" ? this.current : text);
console.log("current: " + this.current);
},
updateDisplay: function(value) {
$("#display").text(value);
}
};
var calc = new Calculator();
$("li").click(function() {
var raw = $(this).text()
number = parseInt(raw, 10);
if (isNaN(number)) {
// must be an operator
switch (raw) {
case "C":
calc.clear();
break;
case "AC":
calc.allClear();
break;
case "x":
calc.multiply();
break;
case "/":
calc.divide();
break;
case "+":
calc.add();
break;
case "-":
calc.subtract();
break;
case "=":
calc.equals();
break;
case ".":
calc.point();
break;
}
} else {
// its a digit
calc.digit(number);
}
});
- 七,
- 八,
- 九,
- C
- 交流电
- 四,
- 五,
- 六,
- x
- /
- 一,
- 二,
- 三,
- +
- -
- 0
- =
.展示{
边框:1px实心#AAA;
填充:20px;
字体大小:20px;
宽度:295px;
文本对齐:右对齐;
高度:25px;}ul{
保证金:0;
填充:0;
溢出:隐藏;
边框:1px实心#CCC;
宽度:335px;}li{
浮动:左;
边框:1px实心#CCC;
背景色:#555;
颜色:白色;
填充:20px;
宽度:25px;
高度:20px;
文本对齐:居中;}/**5.12 6.12 0.12 96.12 6.65 6.6*6.634正常工作:1.65,2.65,3.65,8.65,9.65,10.65精确小数点:4.65,5.65,6.65,7.65,
正常工作:1.12,2.12,3.12,8.12,9.12,10.12;精确小数:4.12,5.12,6.12,7.12,*//var Calculator=function(){this.total=0;this.current=0;
this.operation=null;
this.fractionExp=0;
}; 计算器.原型={
添加:函数(){
这个.calculate();
此操作=功能(a,b){
返回a+b;
};
此.setCurrent(0,“”);
this.fractionExp=0;//十进制修复
},
减法:函数(){
这个.calculate();
此操作=功能(a,b){
返回a-b;
};
此.setCurrent(0,“”);
this.fractionExp=0;//十进制修复
},
乘法:函数(){
这个.calculate();
此操作=功能(a,b){
返回a*b;
};
此.setCurrent(0,“”);
this.fractionExp=0;//十进制修复
},
除法:函数(){
这个.calculate();
此操作=功能(a,b){
返回a/b;
};
此.setCurrent(0,“”);
this.fractionExp=0;//十进制修复
},
清除:函数(){
此.setCurrent(0,“”);
this.fractionExp=0;//十进制修复
},
allClear:function(){
这个.clear();
这个总数=0;
this.operation=null;
console.log(“总计:+this.total”);
this.fractionExp=0;//十进制修复
},
计算:函数(){
如果(此操作){
this.total=this.operation(this.total,this.current);
}否则{
this.total=this.current;
}
console.log(“总计:+this.total”);
},
等于:函数(){
这个.calculate();
this.updateDisplay(this.total);
},
数字:功能(数字){
//向左移动当前位置,添加数字
//这是小数点的关键
if(this.fractionExp){
//不恰当而接近
this.setCurrent(this.current+(number/Math.pow(10,this.fractionExp));
this.fractionExp=this.fractionExp+1
}否则{
this.setCurrent((this.current*10)+数字);
};
},
要点:函数(){
this.fractionExp=1;
},
setCurrent:函数(值、文本){
该值为当前值;
this.updateDisplay(typeof text==“undefined”?this.current:text);
console.log(“当前:+this.current”);
},
updateDisplay:函数(值){
$(“#显示”).text(值);
}
};
var calc=新计算器();
$(“li”)。单击(函数(){
var raw=$(this).text()
数字=parseInt(原始,10);
如果(isNaN(编号)){
//必须是操作员
开关(原始){
案例“C”:
计算清除();
打破
案例“AC”:
计算全部清除();
打破
案例“x”:
计算乘法();
打破
案例“/:
计算除法();
打破
格“+”:
计算添加();
打破
案例“-”:
计算减法();
打破
案例“=”:
计算等于();
打破
案例“.”:
计算点();
打破
}
}否则{
//这是一个数字
计算数字(数字);
}
});
因为有编辑器可以像您要求的那样构建数学函数,所以我想尝试一下“创建javascript函数”部分 我的方法是尝试用正则表达式检测任何数学函数、数学常量或变量,并用适当的javascript调用替换它们。这假设您有一个类似问题图像
2*\pi*r
中的数学表达式作为输入
var mathExpression = $(this).text();
var variables = [];
function handleElement(match){
if (match == '\\pi') return "Math.PI";
// Doesn't match any constant/function --> is a variable
if (variables.indexOf(match) < 0){
variables.push(match);
}
return match;
};
var jsExpression = mathExpression.replace(/[^\d\s.+\-*^()]+/g,handleElement);
var result = "var y = function(";
result += variables.join(', ');
result += "){ ";
result += "return ";
result += jsExpression
result += "; }";
console.log(result);
var mathExpression=$(this).text();
var变量=[];
函数句柄元素(匹配){
如果(match='\\pi')返回“Math.pi”;
//不匹配任何常量/函数-->是变量
if(变量索引of(匹配)<0){
变量。推送(匹配);
}
复赛;
};
var jsExpression=mathExpression.replace(/[^\d\s.+\-*^()]+/g,handleElement);
var result=“var y=函数(”;
结果+=变量。连接(',');
结果+=”{”;
结果+=“返回”;
结果+=JS表达式
结果+=“;}”;
控制台日志(结果);
当然,这只是一个示例解决方案,绝不是完整的。根据要生成的函数的复杂性,使用这种方法解析所有表达式可能非常困难(如果不是不可能的话)。以a^2
这样的表达式为例。您可能希望将其解析为Math.pow(a,2)
。虽然这样做很容易,但是