我想在JavaScript中制作字符串数据计算器



此代码是任何错误的输出输入="11.10 * 4"输出= 40问题解决了制作输入为string而不使用eval的计算器

我的新问题是,如果第一个值是操作符,如+10-5或-10/5输出NaN。

如何解决这个问题?

另一个问题:10-+5或10++55我想这个问题,但它不是解决函数不返回真值。

const hatatespit = (str) => {
const _error=['--','+-','//','**','-+','++'];
let asd;
while(_error.includes(str))
{
asd=str.replace('--','+');
asd=str.replace('-+','-');
asd=str.replace('+-','-');
asd=str.replace('//','/');
asd=str.replace('**','*');
asd=str.replace('++','+');

}
return asd;
};

//

const calcute = str => {
const deger = [];
const operator = [];
const Numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."];
for (let i = 0; i < str.length; i++) {

if (Numbers.includes(str[i])) {
let arabellek = "";
while (i < str.length && Numbers.includes(str[i])) {
arabellek += str[i];
i++;
}
deger.push(parseFloat(arabellek));
i--;
} else if (str[i] === '(') {
operator.push(str[i]);
} else if (str[i] === ')') {
while (operator[operator.length - 1] !== '(') {
deger.push(hesapla(operator.pop(), deger.pop(), deger.pop()));
}
operator.pop();
} 
else if (str[i] === '+' || str[i] === '-' || str[i] === '*' || str[i] === '/') {
while (operator.length && islemOnceligi(str[i], operator[operator.length - 1])) {

deger.push(hesapla(operator.pop(), deger.pop(), deger.pop()));
}
operator.push(str[i]);
}
}
while (operator.length) {
deger.push(hesapla(operator.pop(), deger.pop(), deger.pop()));
}
return deger.pop();
};
const islemOnceligi = (op1, op2) => {
if (op2 === '(' || op2 == ')') {
return false;
}
if ((op1 === '*' || op1 === '/') && (op2 === '+' || op2 === '-')) {
return false;
}
return true;
};
const hesapla = (op, y, x) => {
switch (op) {
case '+':
return parseFloat(x + y);
case '-':
return parseFloat(x - y);
case '*':
return parseFloat(x * y);
case '/':
if (y === 0) {
throw 'Sıfıra bölüm hatası!';
}
return parseFloat(x / y);
}
return 0;
};
var cumle = prompt("HESAPLAMAK İSTEDİNİZ İSLEMİ GİRİN");
document.write(calcute(cumle));
<h2>CALCULATOR</h2>

如果问题是为什么是40,那么只需更改"hasOwnProperty()"包括()";例如:

const calcute = str => {
const deger = [];
const operator = [];
const Numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."];
for (let i = 0; i < str.length; i++) {
if (Numbers.includes(str[i])) {
let arabellek = "";
while (i < str.length && Numbers.includes(str[i])) {
arabellek += str[i];
i++;
}
deger.push(parseFloat(arabellek));
i--;
} else if (str[i] === '(') {
operator.push(str[i]);
} else if (str[i] === ')') {
while (operator[operator.length - 1] !== '(') {
deger.push(hesapla(operator.pop(), deger.pop(), deger.pop()));
}
operator.pop();
} else if (str[i] === '+' || str[i] === '-' || str[i] === '*' || str[i] === '/') {
while (operator.length && islemOnceligi(st1 + r[i], operator[operator.length - 1])) {
deger.push(hesapla(operator.pop(), deger.pop(), deger.pop()));
}
operator.push(str[i]);
}
}
while (operator.length) {
deger.push(hesapla(operator.pop(), deger.pop(), deger.pop()));
}
return deger.pop();
};
const islemOnceligi = (op1, op2) => {
if (op2 === '(' || op2 == ')') {
return false;
}
if ((op1 === '*' || op1 === '/') && (op2 === '+' || op2 === '-')) {
return false;
}
return true;
};
const hesapla = (op, y, x) => {
switch (op) {
case '+':
return parseFloat(x + y);
case '-':
return parseFloat(x - y);
case '*':
return parseFloat(x * y);
case '/':
if (y === 0) {
throw 'Sıfıra bölüm hatası!';
}
return parseFloat(x / y);
}
return 0;
};
var cumle = prompt("HESAPLAMAK İSTEDİNİZ İSLEMİ GİRİN");
document.write(calcute(cumle));

最新更新