当前位置:网站首页>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
边栏推荐
- Understanding the role of individual units in a deep neural networks
- Unity ugui determines the solution of clicking on the UI and 3D objects
- SQL sorts string numbers
- Plane definition - plane equation
- SQL user-defined scalar value function that looks up relevant column values n times forward or backward according to a specified table name, column name and column value
- Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
- Towords Open World Object Detection
- SAP STO With Billing流程与配置
- VBA调用SAP RFC实现数据读取&写入
- Configure NPM
猜你喜欢

Common markdown grammar learning

Unity C single case mode learning review notes

The page displays the current time in real time

linux下mysql数据库备份与恢复(全量+增量)

MySQL8.0 安装/卸载 教程【Window10版】

Use of command line parameter passing library argparse

VBA调用SAP RFC实现数据读取&写入

Houdini>建筑道路可变,学习过程笔记

向量到一个平面的投影向量

SQL针对字符串型数字进行排序
随机推荐
ES6 uses recursion to implement deep copy
H5 local storage data sessionstorage, localstorage
05数组的使用
给定区段范围内字符串自生成代码
自己封装unity的Debug函数
系统与软件安全研究(四)
The projection vector of a vector to a plane
IT高薪者所具备的人格魅力
Page dynamic display time (upgraded version)
保研准备经验贴——18届(2021年)中南计科推免到浙大工院
每天工作4小时的程序员
05 use of array
庄懂的TA笔记(零)<铺垫与学习方法>
Install and configure Taobao image NPM (cnpm)
Unity获取真实地理地图应用Terrain笔记
Unity screen adaptation
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
webflux文件上传下载
Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism
Nodejs (VI) sub process operation