B2B网络软件
标题:
AIWROK软件运算符封装库示例
[打印本页]
作者:
YYPOST群发软件
时间:
5 小时前
标题:
AIWROK软件运算符封装库示例
AIWROK软件运算符封装库示例
2.png
(619.59 KB, 下载次数: 2)
下载附件
5 小时前
上传
3.png
(533.59 KB, 下载次数: 0)
下载附件
5 小时前
上传
/*
🍎AIwork运算符的高级封装和用法
🍎交流QQ群711841924群一,苹果内测群,528816639
JavaScript 运算符封装库示例
*/
// 日志窗口配置(如需使用请取消注释)
logWindow.show()
logWindow.clear()
logWindow.setHeight(2500)
logWindow.setWidth(1500)
logWindow.setNoClickModel()
var OperatorUtils = {};
OperatorUtils.version = '1.0.0';
OperatorUtils.MathEngine = function() {
this.history = [];
this.variables = {};
};
OperatorUtils.MathEngine.prototype = {
calculate: function(expression) {
var keys = [];
for (var key in this.variables) {
if (this.variables.hasOwnProperty(key)) {
keys.push(key);
}
}
var values = [];
for (var i = 0; i < keys.length; i++) {
values.push(this.variables[keys[i]]);
}
var funcArgs = keys.join(',');
var funcBody = 'return ' + expression;
var func = new Function(funcArgs, funcBody);
var result = func.apply(null, values);
this.history.push({
expression: expression,
result: result,
timestamp: new Date().getTime()
});
return result;
},
setVariable: function(name, value) {
this.variables[name] = value;
return this;
},
getVariable: function(name) {
return this.variables[name];
},
getHistory: function() {
return this.history;
},
clearHistory: function() {
this.history = [];
return this;
}
};
OperatorUtils.Arithmetic = {
add: function() {
var result = 0;
for (var i = 0; i < arguments.length; i++) {
var num = Number(arguments[i]);
if (isNaN(num)) {
throw new TypeError('参数必须是数字');
}
result += num;
}
return result;
},
subtract: function(a, b) {
a = Number(a);
b = Number(b);
if (isNaN(a) || isNaN(b)) {
throw new TypeError('参数必须是数字');
}
return a - b;
},
multiply: function() {
var result = 1;
for (var i = 0; i < arguments.length; i++) {
var num = Number(arguments[i]);
if (isNaN(num)) {
throw new TypeError('参数必须是数字');
}
result *= num;
}
return result;
},
divide: function(a, b) {
a = Number(a);
b = Number(b);
if (isNaN(a) || isNaN(b)) {
throw new TypeError('参数必须是数字');
}
if (b === 0) {
throw new Error('除数不能为零');
}
return a / b;
},
modulo: function(a, b) {
a = Number(a);
b = Number(b);
if (isNaN(a) || isNaN(b)) {
throw new TypeError('参数必须是数字');
}
if (b === 0) {
throw new Error('除数不能为零');
}
return a % b;
},
increment: function(value, prefix) {
value = Number(value);
if (isNaN(value)) {
throw new TypeError('参数必须是数字');
}
if (prefix) {
return ++value;
}
var original = value;
value++;
return original;
},
decrement: function(value, prefix) {
value = Number(value);
if (isNaN(value)) {
throw new TypeError('参数必须是数字');
}
if (prefix) {
return --value;
}
var original = value;
value--;
return original;
},
power: function(base, exponent) {
base = Number(base);
exponent = Number(exponent);
if (isNaN(base) || isNaN(exponent)) {
throw new TypeError('参数必须是数字');
}
var result = 1;
for (var i = 0; i < exponent; i++) {
result *= base;
}
return result;
},
sqrt: function(value) {
value = Number(value);
if (isNaN(value)) {
throw new TypeError('参数必须是数字');
}
if (value < 0) {
throw new Error('不能计算负数的平方根');
}
return Math.sqrt(value);
},
abs: function(value) {
value = Number(value);
if (isNaN(value)) {
throw new TypeError('参数必须是数字');
}
return value < 0 ? -value : value;
},
round: function(value, decimals) {
value = Number(value);
if (isNaN(value)) {
throw new TypeError('参数必须是数字');
}
decimals = decimals || 0;
var factor = Math.pow(10, decimals);
return Math.round(value * factor) / factor;
},
sum: function(arr) {
if (Object.prototype.toString.call(arr) !== '[object Array]') {
throw new TypeError('参数必须是数组');
}
var total = 0;
for (var i = 0; i < arr.length; i++) {
total += Number(arr[i]) || 0;
}
return total;
},
average: function(arr) {
if (Object.prototype.toString.call(arr) !== '[object Array]') {
throw new TypeError('参数必须是数组');
}
if (arr.length === 0) {
return 0;
}
return this.sum(arr) / arr.length;
},
max: function(arr) {
if (Object.prototype.toString.call(arr) !== '[object Array]') {
throw new TypeError('参数必须是数组');
}
if (arr.length === 0) {
throw new Error('数组不能为空');
}
var maxVal = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
},
min: function(arr) {
if (Object.prototype.toString.call(arr) !== '[object Array]') {
throw new TypeError('参数必须是数组');
}
if (arr.length === 0) {
throw new Error('数组不能为空');
}
var minVal = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
},
range: function(start, end, step) {
start = Number(start);
end = Number(end);
step = step || 1;
step = Number(step);
if (isNaN(start) || isNaN(end) || isNaN(step)) {
throw new TypeError('参数必须是数字');
}
if (step === 0) {
throw new Error('步长不能为零');
}
var result = [];
var current = start;
if (step > 0) {
while (current <= end) {
result.push(current);
current += step;
}
} else {
while (current >= end) {
result.push(current);
current += step;
}
}
return result;
}
};
OperatorUtils.Assignment = {
assign: function(obj, key, value) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('第一个参数必须是对象');
}
obj[key] = value;
return obj;
},
addAssign: function(obj, key, value) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('第一个参数必须是对象');
}
obj[key] = (obj[key] || 0) + value;
return obj;
},
subtractAssign: function(obj, key, value) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('第一个参数必须是对象');
}
obj[key] = (obj[key] || 0) - value;
return obj;
},
multiplyAssign: function(obj, key, value) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('第一个参数必须是对象');
}
obj[key] = (obj[key] || 0) * value;
return obj;
},
divideAssign: function(obj, key, value) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('第一个参数必须是对象');
}
if (value === 0) {
throw new Error('除数不能为零');
}
obj[key] = (obj[key] || 0) / value;
return obj;
},
moduloAssign: function(obj, key, value) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('第一个参数必须是对象');
}
if (value === 0) {
throw new Error('除数不能为零');
}
obj[key] = (obj[key] || 0) % value;
return obj;
},
merge: function(target) {
if (typeof target !== 'object' || target === null) {
throw new TypeError('第一个参数必须是对象');
}
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
if (typeof source === 'object' && source !== null) {
for (var key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
}
}
return target;
},
deepClone: function(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Object.prototype.toString.call(obj) === '[object Array]') {
var arrCopy = [];
for (var i = 0; i < obj.length; i++) {
arrCopy[i] = this.deepClone(obj[i]);
}
return arrCopy;
}
var objCopy = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
objCopy[key] = this.deepClone(obj[key]);
}
}
return objCopy;
}
};
OperatorUtils.StringOps = {
concat: function() {
var result = '';
for (var i = 0; i < arguments.length; i++) {
result += String(arguments[i] || '');
}
return result;
},
join: function(sep) {
sep = sep || '';
var result = '';
for (var i = 0; i < arguments.length; i++) {
if (i > 0) {
result += sep;
}
result += String(arguments[i] || '');
}
return result;
},
repeat: function(str, count) {
str = String(str);
count = Number(count) || 0;
var result = '';
for (var i = 0; i < count; i++) {
result += str;
}
return result;
},
padLeft: function(str, length, char) {
str = String(str);
char = char || ' ';
while (str.length < length) {
str = char + str;
}
return str;
},
padRight: function(str, length, char) {
str = String(str);
char = char || ' ';
while (str.length < length) {
str = str + char;
}
return str;
},
truncate: function(str, length, suffix) {
str = String(str);
suffix = suffix || '...';
if (str.length <= length) {
return str;
}
return str.substring(0, length - suffix.length) + suffix;
},
capitalize: function(str) {
str = String(str);
if (str.length === 0) return str;
return str.charAt(0).toUpperCase() + str.slice(1);
},
camelCase: function(str) {
str = String(str);
var words = str.split(/[\s_-]+/);
var result = words[0].toLowerCase();
for (var i = 1; i < words.length; i++) {
result += this.capitalize(words[i].toLowerCase());
}
return result;
},
kebabCase: function(str) {
str = String(str);
return str.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]+/g, '-')
.toLowerCase();
},
snakeCase: function(str) {
str = String(str);
return str.replace(/([a-z])([A-Z])/g, '$1_$2')
.replace(/[\s-]+/g, '_')
.toLowerCase();
},
reverse: function(str) {
str = String(str);
var result = '';
for (var i = str.length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
},
contains: function(str, search) {
str = String(str);
search = String(search);
return str.indexOf(search) !== -1;
},
startsWith: function(str, prefix) {
str = String(str);
prefix = String(prefix);
return str.indexOf(prefix) === 0;
},
endsWith: function(str, suffix) {
str = String(str);
suffix = String(suffix);
return str.slice(-suffix.length) === suffix;
},
stripTags: function(str) {
str = String(str);
return str.replace(/<[^>]*>/g, '');
},
escapeHtml: function(str) {
str = String(str);
var htmlEntities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return str.replace(/[&<>"']/g, function(match) {
return htmlEntities[match];
});
},
unescapeHtml: function(str) {
str = String(str);
var htmlEntities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'"
};
return str.replace(/&(amp|lt|gt|quot|#39);/g, function(match) {
return htmlEntities[match];
});
},
wordCount: function(str) {
str = String(str);
var trimmed = str.replace(/^\s+|\s+$/g, '');
var words = trimmed.split(/\s+/);
return trimmed === '' ? 0 : words.length;
},
countChars: function(str, char) {
str = String(str);
char = String(char);
var count = 0;
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) === char) {
count++;
}
}
return count;
},
format: function(str) {
str = String(str);
var args = arguments;
return str.replace(/\{(\d+)\}/g, function(match, index) {
var idx = parseInt(index, 10);
return args[idx + 1] !== undefined ? String(args[idx + 1]) : match;
});
},
template: function(str, data) {
str = String(str);
return str.replace(/\{\{(\w+)\}\}/g, function(match, key) {
return data[key] !== undefined ? String(data[key]) : match;
});
}
};
OperatorUtils.TypeCoercion = {
smartAdd: function(a, b) {
var numA = Number(a);
var numB = Number(b);
if (!isNaN(numA) && !isNaN(numB) &&
String(a).replace(/^\s+|\s+$/g, '') !== '' && String(b).replace(/^\s+|\s+$/g, '') !== '') {
return numA + numB;
}
return String(a) + String(b);
},
smartConcat: function() {
var hasNumber = false;
var hasString = false;
var numbers = [];
var strings = [];
for (var i = 0; i < arguments.length; i++) {
var val = arguments[i];
var num = Number(val);
if (!isNaN(num) && String(val).replace(/^\s+|\s+$/g, '') !== '' &&
!isNaN(parseFloat(val)) && isFinite(val)) {
numbers.push(num);
hasNumber = true;
} else {
strings.push(String(val));
hasString = true;
}
}
if (hasNumber && !hasString) {
var sum = 0;
for (var j = 0; j < numbers.length; j++) {
sum += numbers[j];
}
return sum;
}
var result = '';
for (var k = 0; k < arguments.length; k++) {
result += String(arguments[k]);
}
return result;
},
toNumber: function(value, defaultValue) {
var num = Number(value);
return isNaN(num) ? (defaultValue || 0) : num;
},
toInt: function(value, defaultValue) {
var num = parseInt(value, 10);
return isNaN(num) ? (defaultValue || 0) : num;
},
toFloat: function(value, defaultValue) {
var num = parseFloat(value);
return isNaN(num) ? (defaultValue || 0) : num;
},
toString: function(value, defaultValue) {
if (value === null || value === undefined) {
return defaultValue || '';
}
return String(value);
},
toBoolean: function(value) {
if (typeof value === 'boolean') {
return value;
}
if (typeof value === 'string') {
return value.toLowerCase() === 'true';
}
if (typeof value === 'number') {
return value !== 0;
}
return Boolean(value);
},
isArray: function(value) {
return Object.prototype.toString.call(value) === '[object Array]';
},
isObject: function(value) {
return typeof value === 'object' && value !== null && !this.isArray(value);
},
isFunction: function(value) {
return typeof value === 'function';
},
isString: function(value) {
return typeof value === 'string';
},
isNumber: function(value) {
return typeof value === 'number' && !isNaN(value);
},
isInteger: function(value) {
return this.isNumber(value) && value % 1 === 0;
},
isFloat: function(value) {
return this.isNumber(value) && value % 1 !== 0;
}
};
OperatorUtils.Comparison = {
equals: function(a, b) {
if (a === b) return true;
if (a === null || b === null) return false;
if (typeof a !== typeof b) return false;
if (typeof a === 'object') {
var aKeys = [];
for (var key in a) {
if (a.hasOwnProperty(key)) {
aKeys.push(key);
}
}
var bKeys = [];
for (var key in b) {
if (b.hasOwnProperty(key)) {
bKeys.push(key);
}
}
if (aKeys.length !== bKeys.length) return false;
for (var i = 0; i < aKeys.length; i++) {
var key = aKeys[i];
if (!b.hasOwnProperty(key) || !this.equals(a[key], b[key])) {
return false;
}
}
return true;
}
return false;
},
deepEquals: function(a, b) {
return this.equals(a, b);
},
between: function(value, min, max, inclusive) {
value = Number(value);
min = Number(min);
max = Number(max);
if (inclusive) {
return value >= min && value <= max;
}
return value > min && value < max;
},
clamp: function(value, min, max) {
value = Number(value);
min = Number(min);
max = Number(max);
if (value < min) return min;
if (value > max) return max;
return value;
},
inRange: function(value, range) {
if (Object.prototype.toString.call(range) !== '[object Array]' || range.length !== 2) {
throw new TypeError('范围参数必须是包含两个元素的数组');
}
return this.between(value, range[0], range[1], true);
},
compare: function(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
};
OperatorUtils.Bitwise = {
and: function(a, b) {
return (a | 0) & (b | 0);
},
or: function(a, b) {
return (a | 0) | (b | 0);
},
xor: function(a, b) {
return (a | 0) ^ (b | 0);
},
not: function(a) {
return ~(a | 0);
},
leftShift: function(a, b) {
return (a | 0) << (b | 0);
},
rightShift: function(a, b) {
return (a | 0) >> (b | 0);
},
unsignedRightShift: function(a, b) {
return (a | 0) >>> (b | 0);
}
};
OperatorUtils.Logical = {
and: function() {
for (var i = 0; i < arguments.length; i++) {
if (!arguments[i]) {
return arguments[i];
}
}
return arguments[arguments.length - 1];
},
or: function() {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i]) {
return arguments[i];
}
}
return arguments[arguments.length - 1];
},
not: function(value) {
return !value;
},
ternary: function(condition, trueValue, falseValue) {
return condition ? trueValue : falseValue;
},
nullish: function(value, defaultValue) {
return value != null ? value : defaultValue;
},
coalesce: function() {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] != null) {
return arguments[i];
}
}
return null;
}
};
OperatorUtils.Chainable = function(initialValue) {
this.value = initialValue;
};
OperatorUtils.Chainable.prototype = {
add: function(n) {
this.value = OperatorUtils.Arithmetic.add(this.value, n);
return this;
},
subtract: function(n) {
this.value = OperatorUtils.Arithmetic.subtract(this.value, n);
return this;
},
multiply: function(n) {
this.value = OperatorUtils.Arithmetic.multiply(this.value, n);
return this;
},
divide: function(n) {
this.value = OperatorUtils.Arithmetic.divide(this.value, n);
return this;
},
modulo: function(n) {
this.value = OperatorUtils.Arithmetic.modulo(this.value, n);
return this;
},
power: function(n) {
this.value = OperatorUtils.Arithmetic.power(this.value, n);
return this;
},
round: function(decimals) {
this.value = OperatorUtils.Arithmetic.round(this.value, decimals);
return this;
},
abs: function() {
this.value = OperatorUtils.Arithmetic.abs(this.value);
return this;
},
clamp: function(min, max) {
this.value = OperatorUtils.Comparison.clamp(this.value, min, max);
return this;
},
valueOf: function() {
return this.value;
},
toString: function() {
return String(this.value);
}
};
OperatorUtils.chain = function(value) {
return new OperatorUtils.Chainable(value);
};
OperatorUtils.Calculator = function() {
this.stack = [];
this.memory = 0;
};
OperatorUtils.Calculator.prototype = {
push: function(value) {
this.stack.push(Number(value));
return this;
},
pop: function() {
return this.stack.pop();
},
add: function() {
if (this.stack.length < 2) throw new Error('栈中至少需要两个值');
var b = this.stack.pop();
var a = this.stack.pop();
this.stack.push(a + b);
return this;
},
subtract: function() {
if (this.stack.length < 2) throw new Error('栈中至少需要两个值');
var b = this.stack.pop();
var a = this.stack.pop();
this.stack.push(a - b);
return this;
},
multiply: function() {
if (this.stack.length < 2) throw new Error('栈中至少需要两个值');
var b = this.stack.pop();
var a = this.stack.pop();
this.stack.push(a * b);
return this;
},
divide: function() {
if (this.stack.length < 2) throw new Error('栈中至少需要两个值');
var b = this.stack.pop();
var a = this.stack.pop();
this.stack.push(a / b);
return this;
},
modulo: function() {
if (this.stack.length < 2) throw new Error('栈中至少需要两个值');
var b = this.stack.pop();
var a = this.stack.pop();
this.stack.push(a % b);
return this;
},
power: function() {
if (this.stack.length < 2) throw new Error('栈中至少需要两个值');
var b = this.stack.pop();
var a = this.stack.pop();
this.stack.push(Math.pow(a, b));
return this;
},
memoryStore: function() {
if (this.stack.length > 0) {
this.memory = this.stack[this.stack.length - 1];
}
return this;
},
memoryRecall: function() {
this.stack.push(this.memory);
return this;
},
memoryClear: function() {
this.memory = 0;
return this;
},
memoryAdd: function() {
if (this.stack.length > 0) {
this.memory += this.stack[this.stack.length - 1];
}
return this;
},
memorySubtract: function() {
if (this.stack.length > 0) {
this.memory -= this.stack[this.stack.length - 1];
}
return this;
},
getResult: function() {
return this.stack.length > 0 ? this.stack[this.stack.length - 1] : 0;
},
clear: function() {
this.stack = [];
return this;
}
};
OperatorUtils.runAllExamples = function() {
print.log("=== JavaScript 运算符高级封装库 - 完整测试 ===\n");
print.log("1. 算术运算符测试:");
print.log("-------------------");
print.log("加法(5+3+2): " + OperatorUtils.Arithmetic.add(5, 3, 2));
print.log("减法(10-4): " + OperatorUtils.Arithmetic.subtract(10, 4));
print.log("乘法(3*4*2): " + OperatorUtils.Arithmetic.multiply(3, 4, 2));
print.log("除法(15/3): " + OperatorUtils.Arithmetic.divide(15, 3));
print.log("取模(17%5): " + OperatorUtils.Arithmetic.modulo(17, 5));
print.log("幂运算(2^10): " + OperatorUtils.Arithmetic.power(2, 10));
print.log("平方根(16): " + OperatorUtils.Arithmetic.sqrt(16));
print.log("绝对值(-42): " + OperatorUtils.Arithmetic.abs(-42));
print.log("四舍五入(3.14159,2): " + OperatorUtils.Arithmetic.round(3.14159, 2));
print.log("");
print.log("2. 数组运算测试:");
print.log("----------------");
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
print.log("数组求和: " + OperatorUtils.Arithmetic.sum(numbers));
print.log("数组平均: " + OperatorUtils.Arithmetic.average(numbers));
print.log("数组最大: " + OperatorUtils.Arithmetic.max(numbers));
print.log("数组最小: " + OperatorUtils.Arithmetic.min(numbers));
print.log("范围生成(1-10,步长2): " + JSON.stringify(OperatorUtils.Arithmetic.range(1, 10, 2)));
print.log("");
print.log("3. 赋值运算符测试:");
print.log("------------------");
var obj = { x: 10, y: 5 };
OperatorUtils.Assignment.addAssign(obj, 'x', 5);
OperatorUtils.Assignment.multiplyAssign(obj, 'x', 2);
print.log("对象操作后: " + JSON.stringify(obj));
var target = { a: 1 };
OperatorUtils.Assignment.merge(target, { b: 2 }, { c: 3 });
print.log("对象合并: " + JSON.stringify(target));
print.log("");
print.log("4. 字符串运算测试:");
print.log("--------------------");
print.log("字符串连接: " + OperatorUtils.StringOps.concat('Hello', ' ', 'World'));
print.log("字符串重复: " + OperatorUtils.StringOps.repeat('Abc', 3));
print.log("左填充: " + OperatorUtils.StringOps.padLeft('42', 5, '0'));
print.log("右填充: " + OperatorUtils.StringOps.padRight('Hi', 5, '*'));
print.log("首字母大写: " + OperatorUtils.StringOps.capitalize('hello'));
print.log("驼峰命名: " + OperatorUtils.StringOps.camelCase('hello-world'));
print.log("字符串反转: " + OperatorUtils.StringOps.reverse('Hello'));
print.log("包含检查: " + OperatorUtils.StringOps.contains('Hello World', 'World'));
print.log("");
print.log("5. 类型转换测试:");
print.log("----------------");
print.log("智能加法(5+5): " + OperatorUtils.TypeCoercion.smartAdd(5, 5));
print.log("智能加法(\"5\"+5): " + OperatorUtils.TypeCoercion.smartAdd('5', 5));
print.log("转数字(\"123.45\"): " + OperatorUtils.TypeCoercion.toNumber('123.45'));
print.log("转整数(\"123.45\"): " + OperatorUtils.TypeCoercion.toInt('123.45'));
print.log("转浮点(\"123.45\"): " + OperatorUtils.TypeCoercion.toFloat('123.45'));
print.log("转字符串(12345): " + OperatorUtils.TypeCoercion.toString(12345));
print.log("转布尔(\"true\"): " + OperatorUtils.TypeCoercion.toBoolean('true'));
print.log("是数组([1,2,3]): " + OperatorUtils.TypeCoercion.isArray([1, 2, 3]));
print.log("是对象({a:1}): " + OperatorUtils.TypeCoercion.isObject({ a: 1 }));
print.log("");
print.log("6. 比较运算测试:");
print.log("----------------");
print.log("深度相等: " + OperatorUtils.Comparison.deepEquals({ a: 1 }, { a: 1 }));
print.log("范围检查(5在1-10): " + OperatorUtils.Comparison.between(5, 1, 10, true));
print.log("限制范围(15在1-10): " + OperatorUtils.Comparison.clamp(15, 1, 10));
print.log("");
print.log("7. 位运算测试:");
print.log("-------------");
print.log("按位与(5&3): " + OperatorUtils.Bitwise.and(5, 3));
print.log("按位或(5|3): " + OperatorUtils.Bitwise.or(5, 3));
print.log("按位异或(5^3): " + OperatorUtils.Bitwise.xor(5, 3));
print.log("按位非(~5): " + OperatorUtils.Bitwise.not(5));
print.log("左移(5<<2): " + OperatorUtils.Bitwise.leftShift(5, 2));
print.log("右移(16>>2): " + OperatorUtils.Bitwise.rightShift(16, 2));
print.log("");
print.log("8. 逻辑运算测试:");
print.log("----------------");
print.log("逻辑与: " + OperatorUtils.Logical.and(true, false, 'hello'));
print.log("逻辑或: " + OperatorUtils.Logical.or(false, null, 'world'));
print.log("逻辑非(true): " + OperatorUtils.Logical.not(true));
print.log("三元运算(true,\"yes\",\"no\"): " + OperatorUtils.Logical.ternary(true, 'yes', 'no'));
print.log("空值合并(null,\"default\"): " + OperatorUtils.Logical.nullish(null, 'default'));
print.log("");
print.log("9. 链式调用测试:");
print.log("----------------");
var chainResult = OperatorUtils.chain(10)
.add(5)
.multiply(2)
.subtract(5)
.divide(5)
.round(2)
.valueOf();
print.log("链式计算: 10+5*2-5/5 = " + chainResult);
print.log("");
print.log("10. 计算器栈测试:");
print.log("-----------------");
var calc = new OperatorUtils.Calculator();
calc.push(10).push(5).add().push(3).multiply();
print.log("栈计算 (10+5)*3 = " + calc.getResult());
print.log("");
print.log("11. 数学引擎测试:");
print.log("-----------------");
var engine = new OperatorUtils.MathEngine();
engine.setVariable('x', 5).setVariable('y', 10);
var result = engine.calculate('x * y + 20');
print.log("变量计算 x=5,y=10, x*y+20 = " + result);
print.log("");
print.log("=== 测试完成 ===");
};
OperatorUtils.quickTest = function() {
print.log("=== 快速测试 ===");
print.log("加法: " + OperatorUtils.Arithmetic.add(1, 2, 3));
print.log("乘法: " + OperatorUtils.Arithmetic.multiply(2, 3, 4));
print.log("连接: " + OperatorUtils.StringOps.concat('A', 'B', 'C'));
print.log("智能加法: " + OperatorUtils.TypeCoercion.smartAdd('5', 5));
print.log("链式: " + OperatorUtils.chain(10).add(5).multiply(2).valueOf());
print.log("=== 快速测试完成 ===");
};
print.log("JavaScript 运算符封装库加载完成!");
print.log("");
print.log("使用方法:");
print.log(" OperatorUtils.runAllExamples() - 运行完整测试");
print.log(" OperatorUtils.quickTest() - 运行快速测试");
print.log("");
OperatorUtils.runAllExamples();
复制代码
欢迎光临 B2B网络软件 (http://bbs.niubt.cn/)
Powered by Discuz! X3.2