All generations.

generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

def eval_formula(formula): import math import ast import operator as op operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor, ast.USub: op.neg} def eval_expr(node): if isinstance(node, ast.Num): # <number> return node.n elif isinstance(node, ast.operator): # <operator> return operators[type(node)] elif isinstance(node, ast.UnaryOp): # <unaryop> <expr> return eval_expr(node.op)(eval_expr(node.operand)) elif isinstance(node, ast.BinOp): # <expr> <operator> <expr> return eval_expr(node.op)(eval_expr(node.left), eval

Python
View
generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

s = '1 + 2 + 3 - 4 + 5 - 6 + 7 + 8 - 9 + 10' l = s.split(' ') def caculate(l): if l[1] == '+': return float(l[0]) + float(l[2]) elif l[1] == '-': return float(l[0]) - float(l[2]) elif l[1] == '*': return float(l[0]) * float(l[2]) elif l[1] == '/': return float(l[0]) / float(l[2]) elif l[1] == '**': return float(l[0]) ** float(l[2]) elif l[1] == '//': return float(l[0]) // float(l[2]) elif l[1] == '%': return float(l[0]) % float(l[2]) elif l[1] == '&': return

Python
View
generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

++ #include<iostream> #include<string> using namespace std; int main() { string str; int num1, num2; char op; while (cin >> str) { for (int i = 0; i < str.length(); i++) if ((str[i] == '+') || (str[i] == '-') || (str[i] == '*') || (str[i] == '/')) { op = str[i]; str[i] = ' '; break; } num1 = stoi(str); cin >> num2; switch (op) { case '+':cout << num1 + num2; break; case '-':cout << num1 - num2; break; case '*':cout << num1 * num2; break; case '/':cout << num1 /

generate functionSun, 16 Apr 2023

数学计算器

def cal(a, b, op='+'): if op == '+': return a + b elif op == '-': return a - b elif op == '*': return a * b elif op == '/': return a / b cal(1, 2, '+')

Python
View
generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式,使用二叉树数据结构,使用堆栈算法,生成计算结果

def calculate(string): operator = { '+': (1, lambda a, b: a + b), '-': (1, lambda a, b: a - b), '*': (2, lambda a, b: a * b), '/': (2, lambda a, b: a / b), '//': (2, lambda a, b: a // b), '%': (2, lambda a, b: a % b), '**': (3, lambda a, b: a ** b) } stack = [] for item in string.split(' '): if item in operator: while stack and operator[item][0] <= operator[stack[-1]][0]: a, b = stack.pop(), stack.pop() stack.append(operator[b][1](a, b)) stack.append(item) elif item: stack.append(float(item)) while len(stack) != 1: a

Python
View
generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

// (1 + 2) * 3 // -2 + 3 // -2 + 3 / 5 // 8 ** 2 // 把中缀表达式转化为后缀表达式 // 后缀表达式计算 // 实现链式调用,把中缀表达式转成后缀表达式的过程可以使用函数式编程的概念来实现 class Arithmetic { constructor(input) { this.input = input this.stack = [] this.output = [] } infixToPostfix() { // ... return this } calculate() { // ... return this } }

Javascript
View
generate functionTue, 18 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式,能检查语法错误,并计算结果

import re import sys import math def get_priority(op): if op in ['*', '/']: return 2 elif op == '**': return 3 else: return 1 def get_op(op): if op == '+': return lambda x, y: x + y elif op == '-': return lambda x, y: x - y elif op == '*': return lambda x, y: x * y elif op == '/': return lambda x, y: x / y elif op == '**': return lambda x, y: x ** y elif op == 'sqrt': return lambda x: math.sqrt(x) def cal(s): sl = re.split(r'([()+\-*/])', s) while '' in sl: sl.remove('') num = [] op = [] for i in sl: if i in ['(',

Python
View
generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

def eval_expr(expr): # TODO: YOUR CODE HERE expr_list = expr.split() if len(expr_list) == 1: return int(expr_list[0]) else: for i in range(len(expr_list)-1, 0, -1): if expr_list[i] == '+' or expr_list[i] == '-': expr_list[i-1] = str(eval(''.join(expr_list[i-1:i+2]))) expr_list.remove(expr_list[i]) expr_list.remove(expr_list[i]) for i in range(len(expr_list)-1, 0, -1): if expr_list[i] == '*' or expr_list[i] == '/': expr_list[i-1] = str(eval(''.join(expr_list[i-1:i+2]))) expr_list.remove(expr_list[i]) expr_list

Python
View
generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

++ #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { double a, b, c; char o, o2; cin >> a >> o >> b >> o2 >> c; switch (o) { case '+': a = a + b; break; case '-': a = a - b; break; case '*': a = a * b; break; case '/': a = a / b; break; case '^': a = 1; for (int i = 0; i < b; i++) a = a * b; break; case '$': a = 0; for (int i = 0; i < b; i++) a = a + b; break; default: break; } switch (o2) {

generate functionSun, 16 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式文本,自动计算结果

# #include <iostream> #include <string> #include <stack> #include <sstream> #include <cmath> using namespace std; int main() { string s = "2+3*4-5^2"; stringstream ss; stack<int> nums; stack<char> signs; int num; char sign; while (!s.empty()) { if (isdigit(s.back())) { ss << s.back(); s.pop_back(); if (s.empty() || !isdigit(s.back())) { ss >> num; nums.push(num); ss.clear(); } } else { if (s.back() == '^') { int num = nums.top(); nums.pop(); num = pow(num, 2); nums.push(num); }

generate functionTue, 18 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式,能检查语法错误,并计算结果

def check_math_expression(expression, check_syntax=True, print_result=True): import math import re if check_syntax: if not re.match(r'^[\+\-\*\/\(\)0-9\.\s]+$', expression): print('语法错误!') return False expression = expression.replace(' ', '') # 括号中的计算 while re.findall(r'\([^()]+\)', expression): expression = re.sub(r'\([^()]+\)', str(eval(re.findall(r'\([^()]+\)', expression)[0])), expression, count=1) # 乘方计算 while re.findall(r'\d+\^\d+', expression): expression = re.sub(r'\d+\^\d+', str(eval(re.findall(

Python
View
generate functionSun, 16 Apr 2023

输入一张包含文字内容的图像文件,其中的文字数字,并输出到txt文件中

def recognize_text(img): """Recognize text in the image. Args: img: The input image. Returns: Text in the image. """ img = img.convert('L') image = img.resize((200, 80)) image = np.array(image) # 二值化 threshold = 160 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) image = image.point(table, '1') # Normalize the image to 0 and 1. image = image * (1.0 / 255.0) # 黑白反转 image = 1 - image # 切割图片 image = image.reshape([1, 80, 200, 1]) image = image.astype(np.float32) # Predict. out_

Python
View
generate functionTue, 18 Apr 2023

任意输入含加减乘除乘方开方的文本数学表达式,能检查语法错误,并计算结果

def str2float(s): def fn(x, y): return x * 10 + y def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] def fun(s): return {'+':add, '-':sub, '*':mul, '/':div}[s] def calc(s): num, funs, n = 0, [], 0 num = reduce(fn, map(char2num, s.replace('.', ''))) for c in s: if c in '+-*/': funs.append(fun(c)) n -= 1 return reduce(lambda x, y: y(x, nums[n]), funs, num) return calc(s)

Python
View

Want to kickstart your project?Use the new AI Studio to create your code