Skip to content

Instantly share code, notes, and snippets.

@andy0130tw
Created August 9, 2015 14:19
Show Gist options
  • Save andy0130tw/53eefe93000ecc046ab0 to your computer and use it in GitHub Desktop.
Save andy0130tw/53eefe93000ecc046ab0 to your computer and use it in GitHub Desktop.
Air Conditioner Cost Calculating in HSNU 1296.
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<script>
/***
*** Programmed by 1296Andy
*** For a/c money processing, in 1296.
***/
const NUM=25+1;
const MONEY_MULTIPLY=.75;
const BLANK="&nbsp;";
const DATE_WIDTH=2;
const CELL_WIDTH=5;
const EXTRA_COL=4;
const PROG_VERSION="v1.71";
const LAST_MODIFIED="2013/11/12";
//別忘了第0項的0
const remnant=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var ttl=0,ttlcnt=0;
var dataarr=[];
var moneysum=[];
var buffer=[];
var tbbuffer=[];
var _s1,_s2,_t1,_t2;
window.onload=function(){
document.getElementById('myfile').onchange=readFile;
};
function reset(){
//init for the next use
ttl=ttlcnt=0;
dataarr=[];
moneysum=[];
buffer=[];
tbbuffer=[];
}
function readFile(){
file = this.files[0];
var fReader = new FileReader();
fReader.onload = function(event){
var ec=parseFile(event.target.result);
//document.getElementById('fileContent').value = event.target.result;
tc();
if(ec!=0){
tbuf("Function exited with Error #"+ec);
_t1=_t2=0;
}else flush(dataarr);
//add buf to the end
flushbuf();
//print at once, improved performance.
tf();
reset();
};
_s1=new Date().getTime();
fReader.readAsText(file);
_s2=new Date().getTime();
}
function parseFile(rawtext){
var list=rawtext.split("\n");
for(var i=1;i<NUM;i++){
moneysum[i]=remnant[i];
}
var moneynow=0,moneyinit=0;
if(list.length>0){
moneynow=moneyinit=Number(list[0]);
if(isNaN(moneynow)){
tbuf("Error occured! The file is invaild. Line 1, initial $ should be given.");
tbuf("Line total: "+list.length+" line(s).");
_s2=new Date().getTime();
return 1;
}else t("Initial $:")(moneynow,true);
}
for(var i=1;i<list.length;i++){
var obj={money:0,num:0,usage:arrFactory(NUM,0),empty:false,hide:false};
var cell=list[i].split(" ");
//v1.6, add for comment!
//Any lines that begins with invalid char. will be interpreted as a comment line.
if(isNaN(cell[0]))continue;
//第0項=剩下的錢
if(cell.length==1){
obj.empty=true;
if(cell[0]==0) cell=[moneynow];
else obj.hide=true;
}else{
obj.money=(moneynow-cell[0])*MONEY_MULTIPLY;
ttl+=obj.money;
}
obj.money=(moneynow-cell[0])*MONEY_MULTIPLY;
moneynow=cell[0];
for(var j=1;j<cell.length;j++){
//Prevent error
var seatnum=Number(cell[j]);
if(isNaN(seatnum)){
tbuf("Error occured! Line "+(i+1)+"["+(j)+"], receiving NaN '"+cell[j]+"', which will be ignored.");
continue;
}else if(seatnum>=NUM){
tbuf("Error occured! Line "+(i+1)+"["+(j)+"], receiving num "+cell[j]+"(>="+NUM+"), which will be ignored.");
continue;
}else if(seatnum<=0){
tbuf("Error occured! Line "+(i+1)+"["+(j)+"], receiving num "+cell[j]+"(<=0), which will be ignored.");
continue;
}
obj.usage[Math.floor(cell[j])]++;
obj.num++;
ttlcnt++;
}
//統計
if(obj.num>0){
var avg=obj.money/obj.num;
for(var k=1;k<=NUM;k++){
moneysum[k]+=avg*obj.usage[k];
}
}
dataarr.push(obj);
//t("Money reduced ")(obj.money)(", now ")(moneynow,true);
}
return 0;
//alert(rawtext);
}
function printHeader(){
//保留縮排
t(format("",DATE_WIDTH+3));
for(var i=1;i<NUM;i++){
//印座號
t(format(i,CELL_WIDTH))(" ");
}
//7,5,5
t(" COST")(" COUNT")(" AVG");
//印分隔線
t("",true)(new Array((CELL_WIDTH+1)*(NUM+EXTRA_COL-1)+2).join("-"),true);
//印殘餘
t("PREV|");
for(var i=1;i<NUM;i++){
t(format(remnant[i],CELL_WIDTH))(" ");
}
t("",true);
//印分隔線
t(new Array((CELL_WIDTH+1)*(NUM+EXTRA_COL-1)+2).join("-"),true);
}
function printContent(data){
var dcnt=1;
for(var i=0;i<data.length;i++){
//印日期,靠右
if(data[i].hide)continue;
t("#"+format(dcnt,2,3,"0"));
t(data[i].empty?"@":" ")("|");
if(!data[i].empty){
for(var j=1;j<data[i].usage.length;j++){
var d=data[i].usage[j],d2;
if(!d)d2=[];
else{
d2=new Array(d+1).join("*");
}
t(format(d2,CELL_WIDTH))(" ");
}
//印小計
t(" ")(formatFloat(data[i].money,7));
t(" ")(format(data[i].num,5));
t(" ")(formatFloat(data[i].money/data[i].num,5));
}
t("",true);
dcnt++;
}
}
function printFooter(){
//印分隔線
t(new Array((CELL_WIDTH+1)*(NUM+EXTRA_COL-1)+2).join("-"),true);
//總計
t("SUM |");
for(var i=1;i<NUM;i++){
t(formatFloat(moneysum[i],CELL_WIDTH))("|");
}
//總Cost、Count、Avg
t(" ")(formatFloat(ttl,7));
t(" ")(format(ttlcnt,5));
if(ttlcnt)t(" ")(formatFloat(ttl/ttlcnt,5));
t("",true);
t(" =>|")
var droppedsum=0,drp=Array([NUM]);
//應繳,無條件捨去至10位
for(var i=1;i<NUM;i++){
drp[i]=Math.floor(Math.round(moneysum[i])/10)*10;
t(format(drp[i],CELL_WIDTH))("|");
droppedsum+=drp[i];
}
t(" ")(formatFloat(droppedsum,7))("",true);
//捨去部分剩下
t("REM |")
var remsum=0;
for(var i=1;i<NUM;i++){
var rem=Math.round(moneysum[i]-drp[i]);
t(format(rem,CELL_WIDTH))("|");
remsum+=rem;
}
t(" ")(formatFloat(remsum,7));
t(" ")(formatFloat(droppedsum+remsum-ttl,11,2));
t("",true);
}
function flush(data){
_t1=new Date().getTime();
printHeader();
printContent(data);
printFooter();
_t2=new Date().getTime();
}
function flushbuf(){
for(var i=0;i<buffer.length;i++){
terr(buffer[i],true);
}
t("",true)("##EOF## Elapsed time: "+(_s2-_s1)+" + "+(_t2-_t1)+" = "+(_s2-_s1+_t2-_t1)+" ms.",true);
t(PROG_VERSION+" "+LAST_MODIFIED+" rev.\t製表:潘廣霖;事務股長:何昀霖,於")(new Date())("。參數:MONEY_MULTIPLY=")(MONEY_MULTIPLY)("。");
}
function arrFactory(num,init){
var a=new Array(num);
for(var i=0;i<num;i++){
a[i]=init;
}
return a;
}
function tc(){
document.getElementById("textbox").innerHTML="";
}
function formatFloat(obj,dig,style,suff){
//Turns into ###.# format
num=Number(obj);
maxnum=Math.pow(10,dig-2)-1;
if(num>=Math.pow(10,dig-2)-1)return format(">"+String(maxnum),dig);
//rounding
num=Math.round(num*10)/10;
//Add for .0 suffix
str=String(num);
if(str.indexOf(".")<0)str+=".0";
//Give enough space
return format(str,dig,style,suff);
}
function format(obj,dig,style,suff){
if(dig==0)return "";
if(!suff)suff=" ";
str=String(obj);
if(dig<str.length){
//form a ... like format
if(dig<=2)return new Array(dig+1).join("#");
else{
str=str.substr(0,dig-2);
str+="##";
};
}
//1 for left, 2 for center, 3(or else) for right
while(dig>str.length){
if(style==1)str=str+suff;
else if(style==2){
if(str.length%2){
str=suff+str;
}else{
str=str+suff;
}
}else str=suff+str;
}
return str;
}
function t(text,br){
text=String(text);
if(br)text+="<br/>";
tbbuffer.push(text.split(" ").join(BLANK));
//document.getElementById("textbox").innerHTML+=(text.split(" ").join(BLANK));
return t;
}
function tf(){
document.getElementById("textbox").innerHTML+=tbbuffer.join("");
}
function tbuf(text,br){
if(br)text+="<br/>";
//加入緩衝
buffer.push(text);
return tbuf;
}
function terr(text,br){
text="<span class='err'>"+text+"</span>";
if(br)text+="<br/>";
document.getElementById("textbox").innerHTML+=text;
return terr;
}
</script>
<title>Air Conditioner by ANDY</title>
</head>
<style>
#title{
padding: 5px 0 5px 0;
font-family: "華康細黑體(P)";
text-align: center;
font-size: 26px;
}
#myfile{
font-family: "華康細黑體(P)";
}
#textbox{
font-family: Consolas;
font-size: 83%;
letter-spacing: -.5px;
}
.err{
color:red;
}
.inf{
color:gray;
}
</style>
<body>
<p>
<div id="title">夜自習冷氣使用統計表‧結算報表</div>
<input id="myfile" type="file"/>
</p>
<p id="textbox">
</p>
</body>
</html>
2000
1916 10 3 24 22 16 17 9
1806.4 3 5 24 9 11 17 15 16 20
1725.6 23 13 24 3 10 22
1649.6 3 5 11 16 17 21 24 20 9
1534.4 22
1452.8 17
1402.4 23 24 3 17 25
1195.2
1101.6 9 22 3 24 15 16
1047.2
1020.8 3 5 9 10 16 17 24
959.2
889.6 16 17 20 18 15 22 9 11
770.4 22 23 18 3 17
565.6
459.2 3 17 16 22 20
448
362.4 22 20
302.4
180.8 14 17 3 5 9 24 15 18
400
329.3 17 11 16 10 15
254.9 10 11 15 16 17 9 18 24
246.1 15 17 3 22 24 18 9 12
151.7 22
29.8 22
2399.8
2397.4 15
2290.7 2 3 5 9 10 11 16 17 18 23 24
2235 22
2216.6 3 5 10 11 16 17 20
2212.6 10 15 16 17
2200.6 15
2176.6 23 17 15
2083.27 9 2 2 2 2 7 17
#9.2.7.17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment