当前位置:网站首页>String self generated code within a given range
String self generated code within a given range
2022-04-23 07:54:00 【Dake mountain man】
For project reasons , You need to convert a string within a range , Self generated correlation code . such as :
string topLeft1ColorsString = "(3-16, 0)";
string topLeft2ColorsString = "(0,16-3)";
string topRight1ColorsString = "(17-30, 0)";
string topRight2ColorsString = "(33,3-16)";
string bottomRight1ColorsString = "(33,17-30)";
string bottomRight2ColorsString = "(30-17,33)";
string bottomLeft1ColorsString = "(16-3,33)";
string bottomLeft2ColorsString = "(0,30-17)";
As per topLeft1ColorsString Generate similar :(3,0),(4,0)....(16,0) Code for , Expand , Is to generate the following code :
Point[] topLeft1Points = new Point[] { new Point(3, 0), new Point(4, 0), new Point(5, 0), new Point(6, 0), new Point(7, 0), new Point(8, 0), new Point(9, 0), new Point(10, 0), new Point(11, 0), new Point(12, 0), new Point(13, 0), new Point(14, 0), new Point(15, 0), new Point(16, 0) };
Empathy :"(30-17,33)" Generate :(30,33),(29,33),(28,33).....(17,33) wait .
If the number of strings is large , The range is large , More time saving through program generation .
Post code directly :
public static string GetPointArray(string colorsString, string colorsName)
{
StringBuilder sb = new StringBuilder();
sb.Append("Point[] " + colorsName + "= new Point[] {");
List<string> leftStringList = new List<string>();
List<string> rightStringList = new List<string>();
int cc = 0;
bool isMinusSignAtX = false; // Whether the minus sign is in X In coordinates
bool isMinusSignAtY = false; // Whether the minus sign is in Y In coordinates
string[] sArray = colorsString.Replace("(", "").Replace(")", "").Split(',');
// obtain 3-16 0 perhaps : 0 16-3 Similar array
if (sArray[0].IndexOf("-") >= 0) isMinusSignAtX = true;
if (sArray[1].IndexOf("-") >= 0) isMinusSignAtY = true;
if (isMinusSignAtX) //X Coordinates contain "-" when
{
string[] ssArray = sArray[0].Split('-'); // obtain 3 16 It's like an array
int number1 = int.Parse(ssArray[0]);
int number2 = int.Parse(ssArray[1]);
int diff = number2 - number1;
int count = diff > 0 ? diff + 1 : diff - 1;
cc = Math.Abs(count);
List<string> tmpList = new List<string>();
if (count > 0)
{
// The latter is greater than the former , Ascending
for (int j = 0; j < count; j++)
{
sb.Append("new Point(" + (number1 + j).ToString() + ",");
sb.Append(sArray[1] + ")");
if (j < count - 1) sb.Append(",");
}
}
else // The former is greater than the latter , Descending , similar 16 - 3
{
for (int j = 0; j < cc; j++)
{
sb.Append("new Point(" + (number1 - j).ToString() + ",");
sb.Append(sArray[1] + ")");
if (j < cc - 1) sb.Append(",");
}
}
}
if (isMinusSignAtY) //Y Coordinates contain "-" when
{
string[] ssArray = sArray[1].Split('-'); // obtain 3 16 It's like an array
int number1 = int.Parse(ssArray[0]);
int number2 = int.Parse(ssArray[1]);
int diff = number2 - number1;
int count = diff > 0 ? diff + 1 : diff - 1;
cc = Math.Abs(count);
List<string> tmpList = new List<string>();
if (diff > 0)
{
// The latter is greater than the former , Ascending
for (int j = 0; j < cc; j++)
{
sb.Append("new Point(" + sArray[0] + "," + (number1 + j).ToString() + ")");
if (j < count - 1) sb.Append(",");
}
}
else // The former is greater than the latter , Descending , similar 16 - 3
{
for (int j = 0; j < cc; j++)
{
sb.Append("new Point(" + sArray[0] + "," + (number1 - j).ToString() + ")");
if (j < cc - 1) sb.Append(",");
}
}
}
sb.Append("};");
return sb.ToString();
}
Call mode :
StringBuilder sb = new StringBuilder();
sb.AppendLine(GetPointArray(topLeft1ColorsString, "topLeft1Points"));
sb.AppendLine(GetPointArray(topLeft2ColorsString, "topLeft2Points"));
sb.AppendLine(GetPointArray(topRight1ColorsString, "topRight1Points"));
sb.AppendLine(GetPointArray(topRight2ColorsString, "topRight2Points"));
sb.AppendLine(GetPointArray(bottomRight1ColorsString, "bottomRight1Points"));
sb.AppendLine(GetPointArray(bottomRight2ColorsString, "bottomRight2Points"));
sb.AppendLine(GetPointArray(bottomLeft1ColorsString, "bottomLeft1Points"));
sb.AppendLine(GetPointArray(bottomLeft2ColorsString, "bottomLeft2Points"));
string res = sb.ToString();
OK.
版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378601.html
边栏推荐
猜你喜欢
常用Markdown语法学习
VBA calls SAP RFC to read & write data
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
Unity C# 单例模式 学习复习笔记
Dropping Pixels for Adversarial Robustness
MySQL8. 0 installation / uninstallation tutorial [window10 version]
Houdini流体>>粒子流体导出到unity笔记
About USB flash drive data prompt raw, need to format, data recovery notes
Understanding the role of individual units in a deep neural networks
Unity获取真实地理地图应用Terrain笔记
随机推荐
C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)
MySQL8.0 安装/卸载 教程【Window10版】
KCD_EXCEL_OLE_TO_INT_CONVERT报错SY-subrc = 2
将单行文字自动适应到目标矩形框内
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题
C#操作注册表全攻略
Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism
Dropping Pixels for Adversarial Robustness
Houdini>流体,刚体导出学习过程笔记
UnityShader基础
Quick sort
Event system (II) multicast events
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
保研准备经验贴——18届(2021年)中南计科推免到浙大工院
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
The projection vector of a vector to a plane
三分钟教你用Houdini流体>>解算粒子流体水滴
Index locked data cannot be written to es problem handling
Use of command line parameter passing library argparse
Mongodb 启动警告信息处理