• 发表于 9个月前
• 阅读 4980
• 收藏 47
• 评论 62

# 示例

## 99表

``````for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
printf("%d*%d=%d\t",i,j,i*j);
}
println();
}
``````

``````1*1=1
2*1=2	2*2=4
3*1=3	3*2=6	3*3=9
4*1=4	4*2=8	4*3=12	4*4=16
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	``````

## 数组

``````abc={1,2,4,9,6,3};
println(max(abc));
println(min(abc));
println(avg(abc));
println(median(abc));
println(varp(abc));
println(stdevp(abc));
def={"aa","bb","cc"};
println(max(def));
``````

``````9
1
4.166666666666667
3.5
7.138888888888889
2.6718699236468995
cc
``````

## 列表

``````a=[1,2,3,4,5];
b=[2,3,4,6];
println(a-b);
println(a+b);
println(a&b);
println(a^b);
c=["abc","def","gg"];
d=["abc","def1","gg1"];
println(c+d);
println(c-d);
println(d-c);
``````

``````[1, 5]
[1, 2, 3, 4, 5, 6]
[2, 3, 4]
[1, 5, 6]
[abc, def, gg, def1, gg1]
[def, gg]
[def1, gg1]``````

``````a=[1,2,3];
println(a);
a++;
println(a);
a*=3;
println(a);
b=["abc","def","ghijk"];
println(b.length());``````
``````[1, 2, 3]
[2, 3, 4]
[6, 9, 12]
[3, 3, 5]``````

``````add(a,b)->{	return a+b;};

``6``

## Java语法可以直接使用：

``````import java.util.*;
list=[1,2,3,9,8,7];
Collections.sort(list);
println(list);``````

``[1, 2, 3, 7, 8, 9]``

java的所有属性和方法都可以直接被调用：

``````a=[1,2,3,4,5];
println(a.getClass().getName());
b=[];
println(b);``````

``````java.util.ArrayList
[1, 4, 9, 16, 25]``````

``````a=[1,2,3,4,5];
println(a.filter(#%2==1));
println(a.filter(#%2==0));
println(a.filter(#>3&&#%2==0));
``````

``````[1, 3, 5]
[2, 4]
[4]
``````

``````class User{
age,name;
}

userList=[];
for(i=1;i<9;i++){
user=new User();
user.name="user"+i;
user.age=i;
}

filterList=userList.filter(#.age>3&&#.age%2==1);
println(filterList);
``````

``[User[name=user5,age=5], User[name=user7,age=7]]``

## Map

``````map={"abc":1,"aaa":2,"ddd":4};
map1={"abc":1,"aaa":2,"bbb":3};
println(map);
println(map.get("abc"));
println(map+map1);
println(map^map1);
println(map&map1);
println(map1-map);``````

``````{aaa=2, abc=1, ddd=4}
1
{aaa=2, abc=1, bbb=3, ddd=4}
{bbb=3, ddd=4}
{aaa=2, abc=1}
{bbb=3}``````

``````ele=[1,2,3];
for(i:ele){
ele.permute(i,(e) -> {
println(e);
});
println("==============");
}``````

``````[1]
[2]
[3]
==============
[1, 1]
[1, 2]
[1, 3]
[2, 1]
[2, 2]
[2, 3]
[3, 1]
[3, 2]
[3, 3]
==============
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 1]
[1, 2, 2]
[1, 2, 3]
[1, 3, 1]
[1, 3, 2]
[1, 3, 3]
[2, 1, 1]
[2, 1, 2]
[2, 1, 3]
[2, 2, 1]
[2, 2, 2]
[2, 2, 3]
[2, 3, 1]
[2, 3, 2]
[2, 3, 3]
[3, 1, 1]
[3, 1, 2]
[3, 1, 3]
[3, 2, 1]
[3, 2, 2]
[3, 2, 3]
[3, 3, 1]
[3, 3, 2]
[3, 3, 3]
==============``````

## 水仙花数

``````class Narcissus{
compute(num){
for(i=100;i<num;i++){
a=i/100;
b=(i-a*100)/10;
c=i-a*100-b*10;
if(pow(a,3)+pow(b,3)+pow(c,3)==i){
System.out.println(i);
}
}
}
}

narcissus=new Narcissus();
narcissus.compute(999);``````

``````153
370
371
407
``````

``````elements = [0,1,2,3,4,5,6,7,8,9];
elements.permute(3,(e) -> {
value=e[1]*100+e[2]*10+e[3];
if(pow(e[1],3)+pow(e[2],3)+pow(e[3],3)==value){
System.out.println(value);
}
});
``````

``````0
1
153
370
371
407``````

## 石头剪刀布游戏

``````info=["石头","剪刀","布"];

for(i=0;i<10;i++){
mine=randInt()%3;
your=randInt()%3;
if(mine==your){
printf("平了，都是%s\n",info[mine+1]);
}else{
if(mine==your-1||mine==2&&your==0){
printf("我赢了，我是%s你是%s\n",info[mine+1],info[your+1]);
}else{
printf("我输了，我是%s你是%s\n",info[mine+1],info[your+1]);
}
}
}``````

``````我输了，我是布你是剪刀

``````

## 类支持

``````class User {

name,age;
User(name,age){
this.name = name;
this.age =age;
}
User(name){
this.name = name;
}

}

user = new User("红薯",18);
user2 = new User("tom");
user3 = new User();  //无参构造函数
println(user);
println(user2);
println(user3);``````

``````User[name=红薯,age=18]
User[name=tom,age=null]
User[name=null,age=null]``````

## 计算连续3天涨停的股票

``````class Example1 {

/* 统计一个月内连续三天涨停的股票 */
countStock(path) {
ratio = 0.095d;
groupds =ds.insertColumn(3,"UP").convert(CL,"double").group(CODE).sortGroup("DT ASC");
groupds.subGroup(1,1).update(UP,0d);  //每月的第一天涨停率为0
groupds.update(UP,(CL[0]-CL[-1])/CL[-1]);  //之后的每天统计当天的涨停率。
resultds = groupds.filterGroup(UP[0]>ratio && UP[1]>ratio && UP[2]>ratio);
return resultds;
}
}
m = new Example1();
groupDs= m.countStock(\"src/test/resources/StockRecords.txt\");
for(int i=0;i<groupDs.getRows();i++){
println("code="+groupDs.getData(i+1,1));
}
``````

``ds=[[select * from stockRecords]];``

``````code=201745
code=550766
code=600045
code=700071``````

## 计算订单邮费

某B2C网站需要计算订单的邮寄费用，大部分情况下，邮费有包裹的总重量决定，但是，当订单的价格超过300美元时，提供免费付运。详细规则如下面的mailCharge表所示：

 FIELD MINVAL MAXVAL CHARGE COST 300 1000000 0 WEIGHT 0 1 10 WEIGHT 1 5 20 WEIGHT 5 10 25 WEIGHT 10 1000000 40

某B2C网站需要计算订单的邮寄费用，大部分情况下，邮费有包裹的总重量决定，但是，当订单的价格超过300美元时，提供免费付运。详细规则如下面的mailCharge表所示：

 FIELD MINVAL MAXVAL CHARGE COST 300 1000000 0 WEIGHT 0 1 10 WEIGHT 1 5 20 WEIGHT 5 10 25 WEIGHT 10 1000000 40

 ID COST WEIGHT(KG) JOSH1 150 6 DRAKE 100 3 MEGAN 100 1 JOSH2 200 3 JOSH3 500 1

``````class Example2 {
/* 根据规则统计不同订单的邮费 */
countMailCharge(path1,path2){
costRuleDs = ruleDs.filter(FIELD=="COST").sort("MINVAL");   //cost维度规则集
weightRuleDs = ruleDs.filter(FIELD=="WEIGHT").sort("MINVAL");  //weight维度规则集
orderDs.match(costRuleDs,COST > MINVAL).update(POSTAGE,CHARGE);   //通过match函数匹配COST规则
orderDs.match(weightRuleDs,POSTAGE ==null && WEIGHT > MINVAL && WEIGHT <=MAXVAL).update(POSTAGE,CHARGE);  //通过match函数匹配WEIGHT规则
return orderDs;
}
}
m = new Example2();
resultDs=m.countMailCharge(\"src/test/resources/mailCharge.txt\",\"src/test/resources/testOrder.txt\");
for(int i=0;i<resultDs.getRows();i++){
resultDs.absolute(i+1);
println("POSTAGE="+resultDs.getData("POSTAGE"));
}
``````

``````POSTAGE=25
POSTAGE=20
POSTAGE=10
POSTAGE=20
POSTAGE=0``````

## 统计销售大户

某年内按销售额排名的客户，达到一半销售额的前n个客户称为该年的“大客户”，请列出1998年该企业的大客户名单。

``````class Example3 {
/* 统计某年的大客户  */
countVip(){
firstDs  = contractDs.filter(SellDate.startsWith("1998")).join(clientDs,Client = ID); //过滤1998年的数据，并关联client表
groupDs  = firstDs.select("Name","Amount").group(Name);  //保留计算字段,并按用户分组
groupDs.sumGroup(Amount); //统计每个用户的金额
halfAmount = groupDs.sort("sumGroup_Amount desc").sum(sumGroup_Amount)/2;
list = new java.util.ArrayList();  //建立空序列
groupDs.forEach( (i) -> {
halfAmount -= sumGroup_Amount;
if(halfAmount>0){
}
} );
return list;
}
}
m = new Example3();
obj=return m.countVip();
for(Object obj:list){
println("name="+obj);
}
``````

``````name=QUICK-Stop
name=Save-a-lot Markets
name=Ernst Handel
name=Mère Paillarde
name=Hungry Owl All-Night Grocers
name=Rattlesnake Canyon Grocery
name=Simons bistro
name=Berglunds snabbk?p
name=Lehmanns Marktstand
name=HILARION-Abastos
name=Folk och f? HB``````

## 统计客户账户余额

``````class Example4 {
/* 统计订购产品的客户账户余额  */
countBalance(){
tempDs =  orderDetailDs.join(orderDs,order_id=order_id).join(customerDs ,customer_id=customer_id).copy().select("customer_id","order_id","item_id","acct_balance");
//关联前三张表
groupDs = tempDs.group(customer_id); //按客户分组
groupDs.distinctGroup(item_id); //统计分组客户消费的产品
num = productDs.getRows(); //统计全部产品数目
allProductDs = groupDs.filterGroup(distinctGroup_item_id.size()==num);  //购买全部产品的客户分组集
otherDs =  groupDs.filterGroup(distinctGroup_item_id.size() < num); //购买部分产品的客户分组集
balance1 =  allProductDs.avg(acct_balance);
balance2 =  otherDs.avg(acct_balance);
System.out.println("balance1="+balance1+" balance2="+balance2);
}
}
m = new Example4();
m.countBalance();``````

``balance1=4990.5 balance2=7363.875``

:+1: great idea.

:+1: great idea.

×