From 9d3c8c0e6e1a7ba43bf3dc19350d1dca68b657a3 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sun, 10 Feb 2019 11:16:07 +0800 Subject: Initial commit. --- blog/data/poll.js | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 blog/data/poll.js (limited to 'blog/data/poll.js') diff --git a/blog/data/poll.js b/blog/data/poll.js new file mode 100644 index 0000000..55b4e22 --- /dev/null +++ b/blog/data/poll.js @@ -0,0 +1,171 @@ +//By Chris Xiong, 2018 +//License: Expat +let p=null; +let Caption; +let Choices; +let Score; +let Next; +let values=[]; +let curq; +function evalexpr(e) +{ + let ret=0; + let op=function(a,o,b){ + switch(o){ + case '+':return a+b; + case '-':return a-b; + case '*':return a*b; + case '/':return a/b; + } + }; + for(let p=e.match(/[+\-*\/]/);p;p=e.match(/[+\-*\/]/)) + { + let vp=e.match(/[A-Za-z]\w*/); + if(vp) + { + const v=vp[0]; + if(values[v]===undefined)throw 'shit'; + ret=op(ret,p[0],values[v]); + e=e.substr(vp.index+vp[0].length); + } + else + { + let np=e.match(/\d+(.\d*)?/); + if(np) + { + const n=Number(np[0]); + ret=op(ret,p[0],n); + e=e.substr(np.index+np[0].length); + }else throw 'shit'; + } + } + return ret; +} +function evalsetexpr(e) +{ + const s=e.split('='); + if(s.length<2)throw 'shit'; + values[s[0].trim()]=evalexpr(s[1]); +} +function evaltestexpr(e) +{ + const m=e.match(/([A-Za-z]\w*)\s*(leq|geq|lt|gt)\s*([A-Za-z0-9.]*)/); + if(!m)throw 'shit'; + const v=m[1],op=m[2],vr=m[3]; + const vv=values[v]; + const vvr=isNaN(Number(vr))?values[vr]:Number(vr); + if(vv===undefined||vvr===undefined)throw 'shit'; + switch(op.trim()) + { + case 'leq':return vv<=vvr; + case 'geq':return vv>=vvr; + case 'lt':return vvvvr; + } + throw 'shit'; +} +function evalcondexpr(e) +{ + if(!e.trim().length)return true; + let ret=false; + let op=function(a,o,b){ + switch(o){ + case '|':return a||b; + case '&':return a&&b; + } + }; + for(let p=e.search(/[|&]/);p!=-1;p=e.search(/[|&]/)) + { + let np=e.substr(p+1).search(/[|&]/); + let n=e.substr(p+1,np!=-1?np-1:undefined); + ret=op(ret,e[p],evaltestexpr(n.trim())); + e=np?e.substr(np):""; + } + return ret; +} +function initq() +{ + const q=p.question[curq]; + for(let i=0;i=p.question.length)return; + const q=p.question[curq]; + let seln=0; + for(let i=0;i