Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,56 @@
|
|||
func evalArithmeticExp(s) {
|
||||
|
||||
func evalExp(s) {
|
||||
|
||||
func operate(s, op) {
|
||||
s.split(op).map{|c| c.to_num }.reduce(op);
|
||||
}
|
||||
|
||||
func add(s) {
|
||||
operate(s.sub(/^\+/,'').sub(/\++/,'+'), '+');
|
||||
}
|
||||
|
||||
func subtract(s) {
|
||||
s.gsub!(/(\+-|-\+)/,'-');
|
||||
|
||||
if (s ~~ /--/) {
|
||||
return(add(s.sub(/--/,'+')));
|
||||
}
|
||||
|
||||
var b = s.split('-');
|
||||
b.len == 3 ? (-1*b[1].to_num - b[2].to_num)
|
||||
: operate(s, '-');
|
||||
}
|
||||
|
||||
s.gsub!(/[()]/,'').gsub!(/-\+/, '-');
|
||||
|
||||
var reM = /\*/;
|
||||
var reMD = %r"(\d+\.?\d*\s*[*/]\s*[+-]?\d+\.?\d*)";
|
||||
|
||||
var reA = /\d\+/;
|
||||
var reAS = /(-?\d+\.?\d*\s*[+-]\s*[+-]?\d+\.?\d*)/;
|
||||
|
||||
while (var match = reMD.match(s)) {
|
||||
match[0] ~~ reM
|
||||
? s.sub!(reMD, operate(match[0], '*').to_s)
|
||||
: s.sub!(reMD, operate(match[0], '/').to_s);
|
||||
}
|
||||
|
||||
while (var match = reAS.match(s)) {
|
||||
match[0] ~~ reA
|
||||
? s.sub!(reAS, add(match[0]).to_s)
|
||||
: s.sub!(reAS, subtract(match[0]).to_s);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
var rePara = /(\([^\(\)]*\))/;
|
||||
s.split!.join!('').sub!(/^\+/,'');
|
||||
|
||||
while (var match = s.match(rePara)) {
|
||||
s.sub!(rePara, evalExp(match[0]));
|
||||
}
|
||||
|
||||
return evalExp(s).to_num;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
for expr,res in [
|
||||
['2+3' => 5],
|
||||
['-4-3' => -7],
|
||||
['-+2+3/4' => -1.25],
|
||||
['2*3-4' => 2],
|
||||
['2*(3+4)+2/4' => 2/4 + 14],
|
||||
['2*-3--4+-0.25' => -2.25],
|
||||
['2 * (3 + (4 * 5 + (6 * 7) * 8) - 9) * 10' => 7000],
|
||||
] {
|
||||
var num = evalArithmeticExp(expr);
|
||||
assert_eq(num, res);
|
||||
"%-45s == %10g\n".printf(expr, num);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue