# 3 条题解

• @ 2018-02-12 10:08:42

某些语言中有一个函数叫做 eval(string)

;(function () {
if (str.match(/[^\d\.\(\)\+\-\*\/]/g))
return print("FormulaException");
str = "var k = "+str;
if (str.match(/[^\d\.]0\d/g) ||
str.match(/[\*\/\+\-][\*\/\+\-]/g) ||
str.match(/((\D\.\d)|(\d\.\D)|(\D\.\D))/g))
return print("FormulaException");
try {
eval(str);
} catch (e) {
return print("FormulaException");
}
if (isNaN(k) || k == Infinity || k == -Infinity)
return print("ZeroException");
print(k.toFixed(2));
})();

• @ 2021-12-19 16:28:10

from decimal import Decimal

s = input()
if len(s)>1 and s[0]=='0' and (s[1] not in '.+-*/'):
print ('FormulaException')
elif any(st in s for st in [sig1+sig2 for sig1 in '+-*/' for sig2 in '+-*/'] +
[sig+'0'+num for sig in '+-*/()' for num in '0123456789'] +
[x for x in 'abcdefghijklmnopqrstuvwxyz']):
print( 'FormulaException')
else:
try:
print (Decimal(eval(s)).quantize(Decimal('0.00')))
except ZeroDivisionError:
print ('ZeroException')
except SyntaxError:
print( 'FormulaException')
except NameError:
print ('FormulaException')
except ValueError:
print ('FormulaException')

• @ 2018-03-24 01:31:51

@SW_Wind 的方法一样

from decimal import Decimal

s = raw_input()
if len(s)>1 and s[0]=='0' and (s[1] not in '.+-*/'):
print 'FormulaException'
elif any(st in s for st in [sig1+sig2 for sig1 in '+-*/' for sig2 in '+-*/'] +
[sig+'0'+num for sig in '+-*/()' for num in '0123456789'] +
[x for x in 'abcdefghijklmnopqrstuvwxyz']):
print 'FormulaException'
else:
try:
print Decimal(eval(s)).quantize(Decimal('0.00'))
except ZeroDivisionError:
print 'ZeroException'
except SyntaxError:
print 'FormulaException'
except NameError:
print 'FormulaException'
except ValueError:
print 'FormulaException'

• 1

ID
2019

8

(无)

491

40

8%

2