JS算法
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/6/27
Time: 11:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>大批量生成数组数据</title>
<style>
.BB{
400px;
height: 300px;
position: absolute;
left:400px;
top: 1px;
margin-left: 50px;
/*margin-top: -150px;*/
/**/
}
.DD{
400px;
height:300px;
position: absolute;
left: -40px;
top:400px;
margin-left: 50px;
}
.EE{
400px;
height: 300px;
position: absolute;
left:250px ;
top: 400px;
margin-left: 50px;
/*margin-top: -150px;*/
/**/
}
.FF{
400px;
height: 300px;
position: absolute;
left:550px ;
top: 400px;
margin-left: 50px;
/*margin-top: -150px;*/
/**/
}
.GG{
400px;
height: 300px;
position: absolute;
left:800px;
top: 1px;
margin-left: 50px;
/*margin-top: -150px;*/
/**/
}
.HH{
400px;
height: 300px;
position: absolute;
left:800px;
top: 400px;
margin-left: 50px;
/*margin-top: -150px;*/
/**/
}
</style>
<script type="text/javascript">
var myArr=new Array(); //创建存储生成数据的数组
var dc = new Array(); //创建存储待检测数据的数组
var cf="";
var er="";
var start; //用来计算重复
var end; //计算重复到哪里介束
var zero=0; //用于计算多少个0的变量
var qz=""; //
var qz1=""; //字符串
//生成数据
function newArray(){
var patrn = /^[0-9]*$/; //公式
qz = document.getElementById("prefix").value;//前缀
var numStart=document.getElementById("textst").value; //数量起
var String = GetLastLetterIndex(numStart); //转换字符串
qz1=String[0]; //设置字符串的长度为0
numStart = String[2]; //定义numStart的字符串为长度2
var numEnd=document.getElementById("textEnd").value; //数量止
var String1 = GetLastLetterIndex(numEnd); //获取numEnd数组字母和数字
numEnd = String1[2]; //设置numEnd 字符串长度为0
zero = String[1].length; //设置zero所有的字符串为0可以加1
start = numStart;
end = numEnd;
var numberCopies=document.getElementById("numberCopies").value; //份数
if(!patrn.test(numStart) && !patrn.test(numEnd) && !patrn.test(numberCopies)){ //如果
alert("请输入合法的数字!!!");
return;
}
var dataNum = numEnd - numStart + 1;
if(dataNum<0){
alert("请输入准确的起止数字!!!");
}
if(numberCopies<=0){
alert("请输入准确的份数!!!");
return;
}
var z=0; //定义一个z为0
for(i = 0; i < dataNum ; i++) { //如果
s = "";
s=s+numStart;
var l = zeroFun(zero,s.length);
for(j = 0; j < numberCopies ; j++) {
myArr[z] = qz + String[0] + l +numStart;
z++ ;
}
numStart++;
}
show("textAll");
repeat();
}
//算出最后一个字母在哪里
function GetLastLetterIndex(Str){ //定义一个获取数组的数字和字母
var StrLen = Str.length; //定义StrLen 的长度,
var AscCode; //定义一个控制代码的变量
var String = new Array; //实例化
for(var i = (StrLen - 1);i >= 0;i--){
AscCode = Str.substr(i,1).charCodeAt();
if(!isNaN(Str)){
String[0]="";
String[1]=Str;
strI = parseInt(Str);
String[2]=strI;
return String;
}else{
if(AscCode <48 || AscCode > 57) { //对应ASCII码表进行判断数字和字母
var str = Str.substring(0,i+1);
String[0]=str;
strT = Str.substring(i+1,Str.length);
String[1]=strT;
strI = parseInt(strT);
String[2]=strI;
return String;
}
}
}
}
//计算有多个0开头
function zeroFun(m,s){
var l="";
for(var i=0;i<m-s;i++){
l+="0";
}
return l;
}
//用于显示数据
function show(cls,dc){
if(dc){
var txt="";
for(var n=0;n<dc.length;n++){
txt = txt + dc[n] + " ";
}
document.getElementById(cls).value=txt;
}else{
var txt="";
for(var n=0;n<myArr.length;n++){
txt = txt + myArr[n] + " ";
}
document.getElementById(cls).value=txt;
}
}
//删除数据
function deleted() {
var arr = document.getElementById("arr").value; //删除
var m = arr.split(",");
for(var i=0;i<m.length;i++){
dc.push(m[i]);
}
var sumCll=0;
var sumEll=0;
var index;
var String = GetLastLetterIndex(myArr[0]);
if(arr){
for(var i=0;i<m.length;i++){
if(String[0]!=GetLastLetterIndex(m[i])[0]){
er ="错误条码:"+m[i]+" "+ er;
sumEll+=1;
}else{
index = myArr.indexOf(m[i]);
console.log(index);
if (index != -1) {
myArr.splice(index,1);
show("textAll");
}else{
cf = "条码:"+m[i]+" " + cf;
sumCll+=1;
}
}
repeat();
show("textBll",dc);
document.getElementById("textCll").value=cf; //不存在条码
document.getElementById("textEll").innerHTML=er; //错误条码
document.getElementById("textFll").innerHTML=cf; //遗漏条码
re("not",sumCll);
re("error",sumEll);
}
}
function newArray() {
var patrn = /^qz[0-9]*$/;
var qz = document.getElementById("prefix").value;//前缀
var numStart = document.getElementById("textst").value; //数量起
var numEnd = document.getElementById("textEnd").value; //数量止
var numberCopies = document.getElementById("numberCopies").value; //份数
if (!patrn.test(numStart>numEnd) && !patrn.test(numberCopies<0)) {
document.getElementById("textCll").value=myArr;
}
var dataNum = numEnd - numStart + 1;
// if (numberCopies < 0) {
// var newData = data.replace(arr, "");
// document.getElementById("textCll").value=myArr;
// }
}
}
//重复
function repeat(){
var s = start; //定义一个S为开始
var count = -1; //计算-1
var qz = document.getElementById("prefix").value;//前缀
var str;
var sum=0; //设置sum从0开始
var m="";
var my ="";
for(var i=0;i<=end-start;i++){
m=m+s;
var l = zeroFun(zero,m.length);
str = qz + qz1 + l + s;
for(var j=0;j<myArr.length;j++){
if(myArr[j]==str){
count+=1;
}
}
if(count==-1)
sum=1;
sum =sum +count;
if(count!=-1)
my = "条码:"+str+" 重复数:"+count + " " + my;
count=-1;
s++;
m="";
}
document.getElementById("textDll").value = my; //重复条码
re("repeat",sum);
}
function re(name,sum){
var re = document.getElementsByClassName(name);
for(var i=0;i<re.length;i++){
re[i].innerHTML=sum;
}
}
</script>
<!--显示时间-->
<script language="javascript">
var t = null;
t = setTimeout(time,1000);//开始执行
function time() {
clearTimeout(t);//清除定时器
dt = new Date();
var h=dt.getHours();
var m=dt.getMinutes();
var s=dt.getSeconds();
var day=dt.getDate();
var month=dt.getMonth() + 1;
var year=dt.getFullYear();
document.getElementById("timeShow").innerHTML = year + "年" + month + "月" + day+"日"+ h +"时"+m+"分"+s+"秒";
t = setTimeout(time,1000); //设定定时器,循环执行
}
</script>
</head>
<body>
<form action="acions">
<div class="cc">
<div class="aa" >
<h6>根据条码规则生成原始数据( 0 )</h6>
<textarea style="300px;height:150px; overflow:scroll; border:1px solid;" id="textAll" >
</textarea></br>
<input type="text" id="prefix" placeholder="前缀"/></br>
<input type="text" id="textst" placeholder="数量起"/></br>
<input type="text" id="textEnd" placeholder="数量止"/></br>
<input type="text" id="numberCopies" placeholder="份数" /></br>
<input type="button" onclick="newArray()" value="数据生成" /></br>
<input type="text" id="arr" placeholder="输入已有的数据" /></br>
<input type="button" onclick="deleted()" value="删除数据" />
</div>
<div class="BB">
<h6>待检测条码( 0 )</h6>
<textarea style="300px; height:150px; overflow:scroll; border:1px solid;" id="textBll" ></textarea></br>
</div>
<div class="DD">
<h6>不存在条码( <span class = "not">0</span> )</h6>
<textarea style="200px; height:250px; overflow:scroll; border:1px solid;" id="textCll" ></textarea></br>
</div>
<div class="EE">
<h6>重复条码( <span class = "repeat">0</span> )</h6>
<textarea style="200px; height:250px; overflow:scroll; border:1px solid;" id="textDll" ></textarea></br>
</div>
<div class="FF">
<h6>错误条码( <span class = "error">0</span> )</h6>
<textarea style="200px; height:250px; overflow:scroll; border:1px solid;" id="textEll" ></textarea></br>
</div>
<div class="HH">
<h6>遗漏条码( <span class = "error2">0</span> )</h6>
<textarea style="200px; height:250px; overflow:scroll; border:1px solid;" id="textFll" ></textarea></br>
</div>
<div class="GG">
<h3 style="size: auto">条码检测报告</h3>
<h6>----------------------------------</h6>
<h5>检测时间: <label id="timeShow"></label></h5>
<h5>检测人:***    </h5>
<h5>不存在条码:( <span class = "not">0</span> )</h5>
<h5>重复条码:( <span class = "repeat">0</span> )</h5>
<h5>错误条码:( <span class = "error">0</span> )</h5>
<input type="button" onclick="newArray()" value="提交" /></br>
</div>
</div>
</form>
</body>
</html>