| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #nop vim: set filetype=tt:;
- /*
- 本文件属于 PaoTin++ 的一部分。
- PaoTin++ © 2020~2023 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
- 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
- */
- #nop 本文件是 xtintin 的一部分,实现了一些数值计算函数;
- ///=== {
- ///// 数值计算函数:
- //
- // #@ math.Eval <字符串>
- // 将给定字符串作为算术表达式,进行算术运算,并返回结果。参见 #help math。
- // 本函数也可以简写为 eval,两个名称用途和用法完全相同。
- // };
- #func {eval} {#math result {%0}};
- #func {math.Eval} {#math result {%0}};
- ///=== {
- // #@ math.Random <最小值> <最大值>
- // 给出一个介于最小值和最大值之间的随机整数,注意随机范围包含了最小值和最大值,为闭区间。
- // 本函数也可以简写为 random,两个名称用途和用法完全相同。
- // };
- #func {random} {#return @math.Random{%0}};
- #func {math.Random} {
- #local min {%1};
- #local max {%2};
- #local range {@math.Eval{$max - $min + 1}};
- #math result { ( 1 d $range ) + $min - 1 };
- };
- ///=== {
- // #@ math.Int <值>
- // 对参数值按照四舍五入取整。
- // 本函数也可以简写为 int,两个名称用途和用法完全相同。
- //
- // #@ math.Floor <值>
- // 对参数值向下取整。
- //
- // #@ math.Ceil <值>
- // 对参数值向上取整。
- // };
- #func {int} {#return @math.Floor{1.000 * %0 + 0.5}};
- #func {math.Int} {#return @math.Floor{1.000 * %0 + 0.5}};
- #func {math.Floor} {#format result {%d} {1.000 * %0}};
- #func {math.Ceil} {
- #local value @math.Eval{1.000 * %0};
- #local floor @math.Floor{%0};
- #if { $floor < $value } {
- #return @math.Eval{$floor + 1};
- };
- #else {
- #return $floor;
- };
- };
- ///=== {
- // #@ math.Power <底数> <指数>
- // 幂运算。
- //
- // #@ math.Root <幂> <指数>
- // 开方运算。
- // };
- #func {math.Power} {#return @math.Eval{%1 ** %2}};
- #func {math.Root} {#return @math.Eval{%1 // %2}};
- ///=== {
- // #@ math.Abs <值>
- // 取绝对值。
- //
- // #@ math.Sign <值>
- // 取符号。
- // };
- #func {math.Abs} {#if {%0 >= 0} {#return %0} {#math result {- %0}}};
- #func {math.Sign} {#if {%0 >= 0} {#return 1} {#return -1}};
- ///=== {
- // #@ math.Max <值1> [<值2> ...]
- // 求一系列值的最大值。
- // 本函数也可以简写为 max,两个名称用途和用法完全相同。
- // };
- #func {max} {#return @math.Max{%0}};
- #func {math.Max} {
- #var result {%1};
- #local elem {};
- #foreach {%0} {elem} {
- #if { "$elem" == "" } {
- #continue;
- };
- #if { $elem > $result } {
- #var result {$elem};
- };
- };
- };
- ///=== {
- // #@ math.Min <值1> [<值2> ...]
- // 求一系列值的最小值。
- // 本函数也可以简写为 min,两个名称用途和用法完全相同。
- // };
- #func {min} {#return @math.Min{%0}};
- #func {math.Min} {
- #var result {%1};
- #local elem {};
- #foreach {%0} {elem} {
- #if { "$elem" == "" } {
- #continue;
- };
- #if { $elem < $result } {
- #var result {$elem};
- };
- };
- };
- ///=== {
- // #@ math.Sum <值1> [<值2> ...]
- // 求一系列值的和。
- // 本函数也可以简写为 sum,两个名称用途和用法完全相同。
- // };
- #func {sum} {#return @math.Sum{%0}};
- #func {math.Sum} {
- #local sum {0};
- #local tmp {};
- #foreach {%0} {tmp} {
- #math sum {$sum + $tmp};
- };
- #return {$sum};
- };
- ///=== {
- // #@ math.Avg <值1> [<值2> ...]
- // 求一系列值的算术平均值。
- // 本函数也可以简写为 avg,两个名称用途和用法完全相同。
- // };
- #func {avg} {#return @math.Avg{%0}};
- #func {math.Avg} {
- #var sum {0};
- #local tmp {};
- #local count {0};
- #foreach {%0} {tmp} {
- #math sum {$sum + $tmp};
- #math count {$count + 1};
- };
- #if { $count == 0 } {
- #return 0;
- };
- #else {
- #return @math.Eval{$sum / $count};
- };
- };
- ///=== {
- // ## math.Inc <变量名> [<增量值>]
- // 将增量值叠加到变量原来的值之上。若变量值为空,则视同为 0。如增量值省略,则视同为 1。
- // };
- #alias {math.Inc} {
- #local math.local.var {%1};
- #local value {@defaultNum{%2;1}};
- #math {$math.local.var} {@defaultNum{${$math.local.var};0} + $value};
- };
- ///=== {
- // #@ math.ParseCN <中文数字串>
- // 将中文数字串转换成十进制整数。
- // 本函数也可以简写为 c2d,两个名称用途和用法完全相同。
- // };
- #func {c2d} {#return @math.ParseCN{%0}};
- #func {math.ParseCN} {
- #local string {%0};
- #local number1 {0}; #nop 个位(覆盖);
- #local number2 {0}; #nop 十百千位(加法);
- #local number3 {0}; #nop 万位(乘法);
- #local number4 {0}; #nop 亿位(乘法);
- #local ch {};
- #parse {$string} {ch} {
- #if { "$ch" == "{1|2|3|4|5|6|7|8|9|0|\.}" } {
- #format number1 {%d} {$number1$ch};
- #continue;
- };
- #switch {"$ch"} {
- #case {"零"} { #local number1 {0} };
- #case {"一"} { #local number1 {1} };
- #case {"二"} { #local number1 {2} };
- #case {"三"} { #local number1 {3} };
- #case {"四"} { #local number1 {4} };
- #case {"五"} { #local number1 {5} };
- #case {"六"} { #local number1 {6} };
- #case {"七"} { #local number1 {7} };
- #case {"八"} { #local number1 {8} };
- #case {"九"} { #local number1 {9} };
- #case {"十"} {
- #if { $number1 == 0 } {
- #format number1 {1};
- };
- #math number2 {$number2 + $number1 * 10};
- #format number1 {0};
- };
- #case {"百"} {
- #math number2 {$number2 + $number1 * 100};
- #format number1 {0};
- };
- #case {"千"} {
- #math number2 {$number2 + $number1 * 1000};
- #format number1 {0};
- };
- #case {"万"} {
- #math number3 {($number2 + $number1) * 10000};
- #format number1 {0};
- #format number2 {0};
- };
- #case {"亿"} {
- #math number4 {($number3 + $number2 + $number1) * 100000000};
- #format number1 {0};
- #format number2 {0};
- #format number3 {0};
- };
- };
- };
- #local number {};
- #math number {$number1 + $number2 + $number3 + $number4};
- #return $number;
- };
- ///=== {
- // #@ util.Grade <当前值> <地板结果> <阈值1> <结果1> [...]
- // 根据当前值,计算落入哪个区间,返回对应的结果。阈值被定义为区间的下边沿。
- // 如果当前值比最小的区间的阈值还要小,则返回地板结果。
- // };
- #func {util.Grade} {
- #local current {@defaultNum{%1;0}};
- #local default {%2};
- #local args {};
- #local grade {};
- #list args create {%0};
- #list args delete {1} {2};
- #list grade create {};
- #local count {0};
- #while { $count < &args[] } {
- #local threshold {$args[@math.Eval{$count + 1}]};
- #local value {$args[@math.Eval{$count + 2}]};
- #list grade {add} {{
- {threshold}{$threshold}
- {value}{$value}
- }};
- #math count {$count + 2};
- };
- #list grade {indexate} {threshold};
- #list grade {order};
- #local elem {};
- #local value {$default};
- #foreach {$grade[%*]} {elem} {
- #if { $current >= $elem[threshold] } {
- #local value {$elem[value]};
- };
- #else {
- #break;
- };
- };
- #return {$value};
- };
|