1
0

MAX/MINを実装

This commit is contained in:
otya128
2016-09-03 16:07:30 +09:00
parent cc85f76b81
commit 361a8e04c1
3 changed files with 77 additions and 3 deletions

View File

@@ -1,4 +1,16 @@
'--no-direct-mode --file TEST.TXT '--no-direct-mode --file TEST.TXT
DEF MAXMINTEST
ASSERT__ MAX(0,1,2,3,4,5,4,3,2,1,0)==5,"MAX"
ASSERT__ MIN(5,4,3,2,1,0,1,2,3,4,5)==0,"MIN"
DIM A[5]
FOR I=0 TO LEN(A)-1
A[I]=I+1
NEXT
ASSERT__ MAX(A)==5,"MAX"
ASSERT__ MIN(A)==1,"MIN"
END
MAXMINTEST
DEF SORTTEST DEF SORTTEST
DIM A[4] DIM A[4]
DIM B$[4] DIM B$[4]

View File

@@ -1831,6 +1831,50 @@ class BuiltinFunction
} }
} }
} }
static double MAX(Value array)
{
import std.algorithm.searching;
if (array.type == ValueType.IntegerArray)
{
return minPos!"a > b"(array.integerArray.array)[0];
}
if (array.type == ValueType.DoubleArray)
{
return minPos!"a > b"(array.doubleArray.array)[0];
}
throw new TypeMismatch();
}
static double MAX(Value[] args)
{
import std.algorithm.searching;
if (args.length == 0)
{
throw new IllegalFunctionCall("MAX");
}
return minPos!"a.castDouble > b.castDouble"(args)[0].castDouble;
}
static double MIN(Value array)
{
import std.algorithm.searching;
if (array.type == ValueType.IntegerArray)
{
return minPos!"a < b"(array.integerArray.array)[0];
}
if (array.type == ValueType.DoubleArray)
{
return minPos!"a < b"(array.doubleArray.array)[0];
}
throw new TypeMismatch();
}
static double MIN(Value[] args)
{
import std.algorithm.searching;
if (args.length == 0)
{
throw new IllegalFunctionCall("MIN");
}
return minPos!"a.castDouble < b.castDouble"(args)[0].castDouble;
}
//alias void function(PetitComputer, Value[], Value[]) BuiltinFunc; //alias void function(PetitComputer, Value[], Value[]) BuiltinFunc;
static BuiltinFunctions[wstring] builtinFunctions; static BuiltinFunctions[wstring] builtinFunctions;
static wstring getBasicName(BFD)(const wstring def) static wstring getBasicName(BFD)(const wstring def)

View File

@@ -136,15 +136,33 @@ struct Value
} }
int castInteger() int castInteger()
{ {
return this.type == ValueType.Integer ? this.integerValue : (this.type == ValueType.Double ? cast(int)this.doubleValue : 0); if (this.type == ValueType.Integer)
{
return this.integerValue;
}
if (this.type == ValueType.Double)
{
return cast(int)this.doubleValue;
}
throw new TypeMismatch();
} }
double castDouble() double castDouble()
{ {
return this.type == ValueType.Integer ? this.integerValue : (this.type == ValueType.Double ? this.doubleValue : 0); if (this.type == ValueType.Integer)
{
return this.integerValue;
}
if (this.type == ValueType.Double)
{
return this.doubleValue;
}
throw new TypeMismatch();
} }
wstring castString() wstring castString()
{ {
return this.type == ValueType.String ? this.stringValue : ""; if (this.type == ValueType.String)
return this.stringValue;
throw new TypeMismatch();
} }
string toString() string toString()
{ {