算法求教!
假如:
开始货位 1-1-1-1
结束货位 3-3-3-3生成货位有:
1-1-1-1
1-1-1-2
1-1-1-3
1-1-2-1
1-1-2-2
1-1-2-3
1-1-3-1
1-1-3-2
1-1-3-3
。
2-1-2-1
。
3-3-3-1
3-3-3-2
3-3-3-3具体要求是这样的首先
1-1-1-1 我们把每一位看做是一个输入框 他可以输入 数字(0-9)或者字母(A-Z)每一个输入框内最大是5位 说下自己的思路 和 具体代码 我现在做的 分析 + 组织 + 插入数据库 6W条 需要40分钟!
感觉有点慢 想提高效率! 欢迎大家指点!最好有事例代码!
假如:
开始货位 1-1-1-1
结束货位 3-3-3-3生成货位有:
1-1-1-1
1-1-1-2
1-1-1-3
1-1-2-1
1-1-2-2
1-1-2-3
1-1-3-1
1-1-3-2
1-1-3-3
。
2-1-2-1
。
3-3-3-1
3-3-3-2
3-3-3-3具体要求是这样的首先
1-1-1-1 我们把每一位看做是一个输入框 他可以输入 数字(0-9)或者字母(A-Z)每一个输入框内最大是5位 说下自己的思路 和 具体代码 我现在做的 分析 + 组织 + 插入数据库 6W条 需要40分钟!
感觉有点慢 想提高效率! 欢迎大家指点!最好有事例代码!
我处理的方式
string[] Strarray = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "D", "H", "I", "G", "K", "M", "L", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };//定义一个string[] 传入分解后的单个字符 然后 返回 index /// <summary>
/// 吧值 根据数组对比 转换成index
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private int ConvertToIntByValue(string value)
{
for (int i = 0; Strarray.Length > i; i++)
{
if (Strarray[i] == value)
{
return i;
}
}
return 0;
}
/// <summary>
/// 传入index 去数组中查找对应的值
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
private string ConvertToStringByIndex(int Index)
{
return Strarray[Index].ToString();
}
开始货位和结束货位 是用户输入的啊 ! 这个模块是 货位自动生成 ! 根据输入的起始货位和结束货位自动生成
也有可能是 开始 货位 :1-A-1-A 结束货位 9-Z-9-Z
开始 货位 :111-111-AAA 结束货位 999-999-ZZZ开始 货位 :11-AA-33 结束货位 33-CC-55
是不是从a遍历到z
1a1a1-a1a1a-1a1a1-a1a1a 是合法格式吧
结束b-8-b-8会出现这样吗?
不会 ! 开始货位 aa-11-aa
结束货位 zz-99-zz
首先 开始如果是字母 那么结束对应的位数也肯定是字母 如果是数字 那结束的也肯定是数字 长度也肯定是相同的 !这些都是限制过的 !
ZZZZZ-ZZZZZ-ZZZZZ-ZZZZZ
如果这样的话是25的20次方,应该限制循环次数了吧?
<title>算法</title>
</head>
<body>
<script type="text/javascript">
for(a=1;a<4;a++){
for(b=1;b<4;b++){
for(c=1;c<4;c++){
for(d=1;d<4;d++){
document.write(a+'-'+b+'-'+c+'-'+d+'<br />');
}
}
}
}
</script>
</body>
</html>
这个不行的。。
a=1
b=1
c=1
d=1
假如是 开始22-22 结束 55-55 或者 开始aa-66-22 结束 zz-99-33
A可理解为1,Z可理解为26
AA 就是Z后面那个数字27
AAAAA-AAAAA-AAAAA-AAAAA
ZZZZZ-ZZZZZ-ZZZZZ-ZZZZZ
如果这样是20层
ZZZZZ就是25的5次方
<html>
<title>算法</title>
</head>
<body>
<input type="text" id="a" maxlength="5" size="5" value="1" />-
<input type="text" id="b" maxlength="5" size="5" value="1" />-
<input type="text" id="c" maxlength="5" size="5" value="1" />-
<input type="text" id="d" maxlength="5" size="5" value="1" /><br />
<input type="text" id="e" maxlength="5" size="5" value="3" />-
<input type="text" id="f" maxlength="5" size="5" value="3" />-
<input type="text" id="g" maxlength="5" size="5" value="3" />-
<input type="text" id="h" maxlength="5" size="5" value="3" /><br />
<input type="button" id="bu1" value="计算" onclick="javascript:printit();">
<input type="button" id="bu2" value="测试" onclick="javascript:printtest();">
<div id="d1"></div>
<script type="text/javascript">
function printit(){
var a,b,c,d;
var ina=document.getElementById('a').value;
var inb=document.getElementById('b').value;
var inc=document.getElementById('c').value;
var ind=document.getElementById('d').value;
var ine=document.getElementById('e').value;
var inf=document.getElementById('f').value;
var ing=document.getElementById('g').value;
var inh=document.getElementById('h').value;
var a1=ina.length;
var b1=inb.length;
var c1=inc.length;
var d1=ind.length;
document.getElementById('d1').innerHTML='';
for(a=ina.charCodeAt(0);a<=ine.charCodeAt(0);a++){
for(b=inb.charCodeAt(0);b<=inf.charCodeAt(0);b++){
for(c=inc.charCodeAt(0);c<=ing.charCodeAt(0);c++){
for(d=ind.charCodeAt(0);d<=inh.charCodeAt(0);d++){
for(a2=0;a2<a1;a2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(a));
document.getElementById('d1').innerHTML+=('-');
for(b2=0;b2<b1;b2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(b));
document.getElementById('d1').innerHTML+=('-');
for(c2=0;c2<c1;c2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(c));
document.getElementById('d1').innerHTML+=('-');
for(d2=0;d2<d1;d2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(d));
document.getElementById('d1').innerHTML+=('<br />');
}
}
}
}
}
function printtest(){
alert(String.fromCharCode(97));
alert("a".charCodeAt(0));
}
</script>
</body>
</html>
我已经头晕了
插入System.Data.SqlClient.SqlBulkCopy 具体的我也还在看! 呵呵 !
public string[] Strarray;
/// <summary>
/// 根据货位模板生成货位
/// </summary>
/// <returns></returns>
public string CreatedLocation(CreatedLocationinfo CreateLocation,LocationInfo Location)
{
ILocationDA locationDA = DataAccess.CreateLocationDA();
DataBase dataBase = new DataBase();
DbConnection conn = dataBase.connection;
conn.Open();
DbTransaction tran =conn.BeginTransaction();
try
{
//创建数组
Strarray = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "D", "H", "I", "G", "K", "M", "L", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
List<string> listValue = new List<string>();
List<int> listLength = CreateLocation.LocationValueLength;
string StrStartValue = CreateLocation.LoactionStarteValue;
string StrEndValue = CreateLocation.LoactionEndValue;
int iStartVlaue = 0;
int iEndValue = 0;
int iCount = 0;
iCount = CreateLocation.LocationLength;
iStartVlaue = ConvertToIntByValue(StrStartValue[iCount - 1].ToString());
iEndValue = ConvertToIntByValue(StrEndValue[iCount - 1].ToString());
for (int i = iStartVlaue; iEndValue >= i; i++) //横向坐标 个位
{
listValue = CreateFunction(iCount - 2, StrStartValue, StrEndValue);
for (int w = 0; listValue.Count > w; w++)
{
listValue[w] = listValue[w].Replace("@_@", ConvertToStringByIndex(i)); if (!string.IsNullOrEmpty(CreateLocation.SeperatorChar))
{
int index = 0;
for (int n = 0; listLength.Count > n; n++)
{
if (listLength[n] > 0)
{
listValue[w] = listValue[w].Insert(listLength[n] + index, CreateLocation.SeperatorChar);
index += listLength[n] + 1;
}
else
{
listValue[w] = listValue[w].Substring(0, listValue[w].Length - 1);
break;
}
}
} LocationInfo locationEntity = Location;
locationEntity.Loc = listValue[w];
//判断locationID是否已存在
if (locationDA.CheckLocationIDUnique(dataBase, tran, locationEntity.Loc, dataBaseID[0]))
{
throw new Exception("货位ID'" + locationEntity.Loc + "'已经存在! 请删除后重新生成!");
} string[] strLoc = locationEntity.Loc.Split(CreateLocation.SeperatorChar.ToCharArray()); for (int t = 0; strLoc.Length > t; t++)
{
switch (t)
{
case 0:
locationEntity.TemplateField1 = strLoc[t];
break;
case 1:
locationEntity.TemplateField2 = strLoc[t];
break;
case 2:
locationEntity.TemplateField3 = strLoc[t];
break;
case 3:
locationEntity.TemplateField4 = strLoc[t];
break;
case 4:
locationEntity.TemplateField5 = strLoc[t];
break;
}
}
locationEntity.UpdatedBy = locationEntity.CreatedBy;
locationDA.InsertLocation(dataBase, tran, locationEntity, dataBaseID[0]); //插入这里有问题! }
}
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
CommonErrorHandler.ErrorHandler(ex, LogType.Error);
}
finally
{
conn.Close();
}
return ""; //返回 空 代表成功
} /// <summary>
/// 创建模板货位
/// </summary>
/// <param name="length">字符长度</param>
/// <param name="StrStartValue">开始字符</param>
/// <param name="StrEndValue">结束字符</param>
/// <returns></returns>
private List<string> CreateFunction(int length, string StrStartValue, string StrEndValue)
{
List<string> listValue = new List<string>();
int iStartvlaue = 0;
int iEndvalue = 0;
iStartvlaue = ConvertToIntByValue(StrStartValue[length].ToString());
iEndvalue = ConvertToIntByValue(StrEndValue[length].ToString());
for (int n = iStartvlaue; iEndvalue >= n; n++) //纵向坐标 十位
{
if (length > 0)
{
List<string> listValue1 = new List<string>();
listValue1 = CreateFunction(length - 1, StrStartValue, StrEndValue);
for (int i = 0; listValue1.Count > i; i++)
{
listValue.Add(listValue1[i].Replace("@_@", ConvertToStringByIndex(n)) + "@_@");
}
}
else
{
listValue.Add(ConvertToStringByIndex(n) + "@_@");
}
} return listValue;
}
/// <summary>
/// 吧值 根据数组对比 转换成index
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private int ConvertToIntByValue(string value)
{
for (int i = 0; Strarray.Length > i; i++)
{
if (Strarray[i] == value)
{
return i;
}
}
return 0;
}
/// <summary>
/// 传入index 去数组中查找对应的值
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
private string ConvertToStringByIndex(int Index)
{
return Strarray[Index].ToString();
}
using System.Collections.Generic;
using System.Text;namespace Test.Test
{
class Test
{ //{
// if (Start.Length != End.Length)
// throw new Exception("Warn start string or end string.");
// //List<String> slist = new List<String>();
// //for (int i=0;i<Start.Length;i++)
// //{
// // StringBuilder sb = new StringBuilder();
// // if (char.IsDigit(Start[i]))
// // {
// // int end = int.Parse(End[i]); // // for (int a = 0; a < 9; a++)
// // {
// // if (a > end)
// // break;
// // sb.Append(a.ToString());
// // }
// // }
// // else
// // {
// // int end = End[i];
// // for (int a = 0; a < 26; a++)
// // {
// // char c = Start[i] + a;
// // if (c > end)
// // break;
// // sb.Append(c);
// // }
// // }
// // slist.Add(sb.ToString());
// //}
// //List<String> result = new List<string>(); // //foreach (String s in slist)
// //{
// // foreach (char c in s)
// // { // // }
// //} // return null;
//} public interface NoItem
{
/// <summary>
/// 获取所有级
/// </summary>
/// <returns></returns>
String[] GetAllItems();
} public class NoDigitItem : NoItem
{
public NoDigitItem(int start, int end)
{
Start = start;
End = end;
}
public int Start;
public int End;
#region NoItem Members public string[] GetAllItems()
{
List<String> result = new List<string>(); for (int i = Start; i <= End; i++)
{
result.Add(i.ToString());
}
return result.ToArray();
} #endregion
} public class NoStringItem : NoItem
{
public NoStringItem(String start, String end)
{
Start = start;
End = end;
}
public String Start;
public String End; #region NoItem Members
private int Getnn(int n)
{
switch (n)
{
case 0:
return 1;
case 1:
return 100;
case 2:
return 10000;
case 3:
return 1000000;
}
return 0;
}
public string[] GetAllItems()
{
long tempStrat = 0;
int n = 0;
for (int i = Start.Length - 1; i >= 0; i--)
{
tempStrat += (Start[i]) * Getnn(n);
n++;
}
long tempEnd = 0;
n = 0;
for (int i = End.Length - 1; i >= 0; i--)
{
tempEnd += (End[i]) * Getnn(n);
n++;
}
int s = 0;
List<String> result = new List<string>();
for (long t = tempStrat; t <= tempEnd; t++)
{
StringBuilder sb = new StringBuilder();
String temps = t.ToString();
for (s = 0; s < temps.Length; s++)
{
int tempc = int.Parse(temps.Substring(s, 2));
if (tempc < 'A' | tempc > 'Z')
break;
sb.Append((char)(tempc));
s++;
}
String rs = sb.ToString();
if (rs.Length == Start.Length)
result.Add(rs);
}
return result.ToArray();
} #endregion
} public class NoString
{
public List<NoItem> NoItems = new List<NoItem>(); /// <summary>
/// 获取所有编号
/// </summary>
/// <returns></returns>
public String[] GetAllNos()
{
List<String[]> itList = new List<string[]>();
foreach (NoItem n in NoItems)
{
//foreach (string s in n.GetAllItems())
// Console.WriteLine(s);
itList.Add(n.GetAllItems());
}
//这里穷举itList列表
return null;
}
}
public void DoTest()
{
NoString ns = new NoString();
ns.NoItems.Add(new NoStringItem("ABC", "DCA"));
ns.NoItems.Add(new NoDigitItem(1, 9));
ns.NoItems.Add(new NoStringItem("CCC", "DDD"));
ns.NoItems.Add(new NoDigitItem(23, 54));
ns.GetAllNos();
}
}
}
{
for (int i = 0; Strarray.Length > i; i++)
{
if (Strarray[i] == value)
{
return i;
}
}
return 0;
}你的这段代码有问题吧,当Strarray[0] == value的时候和没找到 return 0;都返回0??
你所指的异常是什么情况呢? 首先在页面已经做过很多限制了!
比如 只能输入数字或者字母(不能同时存在)
开始货位 比结束货位小 而且这里也只是一个 转义的过程! 并不是主逻辑! 其实还有问题是 现在里面的数字类型是 int 也就是int32 在生成大量的货位的时候 会有异常! 应该改为int64