for(int i = 0; i < Rect.rectangles.Count; i++)
{
if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "")
{
string firstNumber = Rect.rectangles[i].transform.Substring(18, 10);
string secondNumber = Rect.rectangles[i].transform.Substring(7, 10);
double a = Convert.ToDouble(firstNumber);
double b = Convert.ToDouble(secondNumber);
}
}
The first number is 0.49052715 and the Convert.ToDouble of a is fine.
But when it's getting to the line:
double b = Convert.ToDouble(secondNumber);
I'm getting exception:
FormatException: Unknown char: ,
But there is no any char/s at all the string in secondNumber is: 0.87142591
This is the whole string in transform: matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)
And i'm extracting the first two numbers: 0.49052715 and 0.87142591 and then trying to convert them to double. But getting the exception.
The code including the Rect.rectangles definition:
private void ParseSvgMap()
{
XDocument document = XDocument.Load(#"C:\Users\mysvg\Documents\my.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
}).ToList();
for(int i = 0; i < Rect.rectangles.Count; i++)
{
if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "")
{
string firstNumber = Rect.rectangles[i].transform.Substring(18, 10);
string secondNumber = Rect.rectangles[i].transform.Substring(7, 10);
double a = Convert.ToDouble(firstNumber);
double b = Convert.ToDouble(secondNumber);
float degree = FindDegree(a, b);
}
}
}
public static float FindDegree(double a, double b)
{
float value = (float)((System.Math.Atan2(a, b) / System.Math.PI) * 180f);
if (value < 0) value += 360f;
return value;
}
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public string transform { get; set; }
public double degree { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = #"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
}
Code like .Substring(18, 10) is hopelessly brittle and (as you have discovered) is an accident waiting to happen. Numbers aren't guaranteed to be the same length. Just look at the sample data you provided.
You should use something like a regex to extract the numbers from the strings before attempting to parse them.
var regex = new Regex(#"matrix\((?<num>-?(?:\d|\.)+)(?:,(?<num>-?(?:\d|\.)+))*\)");
var data = "matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)";
var values = regex
.Matches(data)
.Cast<Match>()
.SelectMany(m => m.Groups["num"]
.Captures
.Cast<Capture>()
.Select(c=>c.Value))
.Select(double.Parse)
.ToList();
Then pick out the numbers you want with values[0] and values[1].
Its doomed to fail.
I would use following regex:
string test = "matrix(0.1234,0.23233433,0.34,-3343,-3.33)";
Regex reg = new Regex("[-]?[0-9]+(\\.[0-9]+)?");
MatchCollection coll = reg.Matches(test);
You can write a method along the lines of :
public double fixString(string incoming)
{
Double a;
if(incoming.Contains(','))
{
string fixedNum = incoming.Remove(incoming.IndexOf(','));
a = Convert.ToDouble(fixedNum);
return a;
}
else
{
a = Convert.ToDouble(incoming);
return a;
}
}
Related
I need to create a list containing Device objects. These objects have properties that describe their Name, Unit and Conversion. Conversion is a bit special as this is supposed to be a function. For example: If I had a temperature sensor measuing in farenheit, its conversion method should calculate and return the value in celsius. And if I have a moisture sensor, its conversion will be different etc. Below is an example of how I have tried doing this but it doesn't work. I get errors saying that only assignment is allowed on Conversion.
private class Device
{
public string Name { get; set; }
public Action Conversion { get; set; }
public string Unit { get; set; }
}
public static object ParseDecentLab(byte[] bytes)
{
List<Device> deviceList = new List<Device>()
{
new Device()
{
Name = "battery-voltage",
Conversion = (x) => x / 1000,
Unit = "V"
},
new Device()
{
Name = "air-temperature",
Conversion = (x) => (175 * x / 65535) - 45,
Unit = "°C"
}
};
Try this code:
use Func instead of Action.
Func can return value where as Action Can't.
private class Device
{
public string Name { get; set; }
public Func<double,double> Conversion { get; set; }
public string Unit { get; set; }
}
public static object ParseDecentLab(byte[] bytes)
{
List<Device> deviceList = new List<Device>()
{
new Device()
{
Name = "battery-voltage",
Conversion = (x) => x / 1000,
Unit = "V"
},
new Device()
{
Name = "air-temperature",
Conversion = (x) => (175 * x / 65535) - 45,
Unit = "°C"
}
};
}
You want Func<double, double> instead of Action; given double (e.g. 4.5 Volt) return double.
x => x / 1000
On the contrary, Action takes no arguments and returns nothing: () => {...}
Code:
// Let's implement immutable class (assigned once, never change)
// in order to prevent occasional errors like device.Name = ...
private class Device {
public string Name { get; }
public Func<double, double> Conversion { get; }
public string Unit { get; }
// Let's validate the input (at least, for null)
public Device(string name, Func<double, double> conversion, string unit) {
if (null == name)
throw new ArgumentNullException(nameof(name));
else if (null == conversion)
throw new ArgumentNullException(nameof(conversion));
else if (null == unit)
throw new ArgumentNullException(nameof(unit));
Name = name;
Conversion = conversion;
Unit = unit;
}
}
...
List<Device> deviceList = new List<Device>() {
new Device("battery-voltage", x => x / 1000, "V"),
new Device("air-temperature", x => (175 * x / 65535) - 45, "°C"),
};
Possible usage:
// What device should we use if we want °C unit?
Device temperature = deviceList
.FirstOrDefault(item => item.Unit == "°C");
byte[] dataToConvert = new byte[] {123, 45, 79};
// Device found
if (temperature != null) {
// Converting: for each value in dataToConvert we obtain corresponding t value
foreach (var value in dataToConvert) {
double t = temperature.Conversion(value);
...
}
}
Or you can even have an array of converted values (double[]) with a help of Linq:
byte[] dataToConvert = new byte[] {123, 45, 79};
// Let's throw exception if device has not been found
Device temperature = deviceList
.First(item => item.Unit == "°C");
double[] results = dataToConvert
.Select(v => temperature.Convert(v))
.ToArray();
I want that in Rect.rectangles under the transform property instead to have the string "matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)" to have 2 properties like transform and transform1 and each one will have a number for example: transform "0.12345678"
transform1 "1.12345678"
private void Parse()
{
XDocument document = XDocument.Load(#"C:\Users\mysvg\Documents\my.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform".Substring(18, 43)) == null ? "" : (string)x.Attribute("transform".Substring(18, 43))
}).ToList();
}
And the Rect class:
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public string transform { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = #"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
}
I'm trying to use substring but getting out of range exception.
ArgumentOutOfRangeException: Cannot exceed length of string
On the line:
transform = x.Attribute("transform".Substring(18, 43)) == null ? "" : (string)x.Attribute("transform".Substring(18, 43))
The string is:
transform="matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)"
And i want to get only the first two numbers: 0.40438612,-0.91458836
Update:
I found also a way to do it using foreach:
private void Parse()
{
XDocument document = XDocument.Load(#"C:\Users\mysvg\Documents\my.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
}).ToList();
string t = null;
foreach(Rect rect in Rect.rectangles)
{
if (rect.transform != null && rect.transform != "")
{
t = rect.transform.Substring(7, 21);
}
}
}
But i don't want to do it after the new Rect() {
I want to do the first two numbers extraction on the line:
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
The reason is that i have in the bottom of the code a class:
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public string transform { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = #"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
}
And i want that the property transform to hold/contain the two numbers already.
And another small problem how can i extract the first two numbers if for example one of the numbers have -(minus) in the start for example if transform contain the string: "matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)"
If one of the two numbers have minus in the start or both of them have minus in the start the Substring(7, 21) will not work. I need somehow to get in any case the first two numbers. But the main issue is how to extract the numbers directly from the attribute already.
Are you looking for regular expressions?
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
...
string transform = "matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)";
double[] result = Regex
.Matches(transform, #"-?[0-9]*\.?[0-9]+")
.OfType<Match>()
.Take(2)
.Select(match => double.Parse(match.Value, CultureInfo.InvariantCulture))
.ToArray();
Without regular expression you can get the part of the string starting right after the first parenthesis and then split it using the comma:
var transform="matrix(0.40438612,-0.91458836,0.92071162,0.39024365,0,0)";
var numbers = transform.Substring(transform.IndexOf('(') + 1).Split(',');
Console.WriteLine(double.Parse(numbers[0]));
Console.WriteLine(double.Parse(numbers[1]));
Please add error handling.
<== Fiddle Me ==>
Solution:
private void Parse()
{
XDocument document = XDocument.Load(#"C:\Users\mysvg\Documents\my.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
}).ToList();
for(int i = 0; i < Rect.rectangles.Count; i++)
{
if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "")
{
Rect.rectangles[i].transform = Rect.rectangles[i].transform.Substring(7, 21);
}
}
}
Here is the best answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Rect.rectangles = doc.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform") == null ? null : (object)Rect.GetTransform((string)x.Attribute("transform"))
}).ToList();
}
}
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public object transform { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = #"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
public static KeyValuePair<double, double> GetTransform(string matrix)
{
string pattern = #"[-+]?\d+\.\d+";
MatchCollection matches = Regex.Matches(matrix, pattern);
KeyValuePair<double, double> kp = new KeyValuePair<double, double>(
double.Parse(matches[0].Value),
double.Parse(matches[0].Value)
);
return kp;
}
}
}
I want to find a specific value from a List with the method select.
My code :
public class Calc
{
public int IdCalc { get; set; }
public double Result { get; set; }
public int Number { get; set; }
}
public class Program
{
static void Main()
{
Calc myC1 = new Calc();
List<Calc> liCalc = new List<Calc>();
myC1.IdCalc = -1;
myC1.Result = 20.2;
myC1.Number = 1;
Calc myC2 = new Calc();
myC2.IdCalc = 22;
myC2.Result = 20.2;
myC2.Number = 2;
liCalc.Add(myC1);
liCalc.Add(myC2);
double getResult = ((Calc)(liCalc.Select(Calc => Calc.IdCalc = 22 && Calc.Number = 2))).Result;
Console.ReadKey();
}
}
As you can see my List contains two objects: myC1 and myC2.
I just want to find the value of Result when IdCalc = 22 and Number = 2 thats why I tried to use Select but it's not working with two parameters.
You could use Where, which lets you filter results based on some criteria, however that will return an IEnumerable<Calc>. Since you are only looking for a single result, you should use First which also takes a predicate and only returns the first Calc:
Calc myCalc = liCalc.First(c => c.IdCalc == 22 && c.Number == 2);
double result = myCalc.Result;
This will throw an exception if there is nothing that matches the filter, though. If you're worried about that, use FirstOrDefault which will return null if there is no match.
public class Calc
{
public int IdCalc { get; set; }
public double Result { get; set; }
public int Number { get; set; }
}
public class Program
{
static void Main()
{
Calc myC1 = new Calc();
List<Calc> liCalc = new List<Calc>();
myC1.IdCalc = -1;
myC1.Result = 20.2;
myC1.Number = 1;
Calc myC2 = new Calc();
myC2.IdCalc = 22;
myC2.Result = 20.2;
myC2.Number = 2;
liCalc.Add(myC1);
liCalc.Add(myC2);
double getResult = liCalc.First(item => item.IdCalc == 22 && item.Number == 2).Result; //Note that this will throw an exception if no item in the list satisfies the condition.
Console.ReadKey();
}
You could use the following statement
double getResult = liCalc.Where(Calc => Calc.IdCalc = 22 && Calc.Number = 2))).Select(y=>y.Result).FirstOrDefault();
Essentially using Where() followed by Select().
I have inputs like following:
"10+18+12+13"
"10+18-12+13"
"2-5"
e.g. number followed by a "+" or "-"
I created class MathOper
public class MathOper
{
public int Num { get; set; }
public string Oper { get; set; } //this display the number will be operated.
}
I want to return list of MathOper as following
"10+18-12+13" will return
new MathOper(){Num=10,"+"}
new MathOper(){Num=18,"+"}
new MathOper(){Num=12,"-"}
new MathOper(){Num=13,"+"}
I tried to code that by this way:
public class MathOperCreator
{
private readonly string _mathString;//8+2-3
public MathOperCreator(string mathString)
{
this._mathString = mathString.Trim();
}
public List<MathOper> Create()
{
var lMo = new List<MathOper>();
int l = this._mathString.Length;//5
for (int i = 0; i < l; i = i + 2)
{
char n = _mathString[i];
int n1 = int.Parse(n.ToString(CultureInfo.InvariantCulture));
string o1 = "+";
if (i > 0)
{
o1 = _mathString[i - 1].ToString(CultureInfo.InvariantCulture);
}
var mo = new MathOper { Num = n1, Oper = o1 };
lMo.Add(mo);
}
return lMo;
}
}
I found that it is only for number with one char, if the number is two char ,such as 18 , it doesn't work.
Please advice how implement described functionality?
This is a tested Solution
//Model class
public class MathOperation
{
public Int32 Num { get; set; }
public String Operation { get; set; }
}
String testData = "10+18+12-13";
String[] GetNumbers = testData.Split(new Char[] { '+', '-' });
String[] GetOperators = Regex.Split(testData, "[0-9]+");
//remove empty entries in operator
GetOperators = GetOperators.Where(x => !string.IsNullOrEmpty(x)).ToArray();
List<MathOperation> list = new List<MathOperation>();
MathOperation mathOperation = new MathOperation();
for (int i = 0; i < GetNumbers.Count(); i++)
{
mathOperation.Num = Convert.ToInt32(GetNumbers[i]);
mathOperation.Operation = (i>GetOperators.Length)? GetOperators[i] : null;
list.Add(mathOperation);
}
This will give a list like
{Num=10,"+"}
{Num=18,"+"}
{Num=12,"-"}
{Num=13,"null"} //as in my test string there is no char after 13
if you dont want a null always a + then
mathOperation.Operation = (i>GetOperators.Length)? GetOperators[i] : "+";
then This will give a list like
{Num=10,"+"}
{Num=18,"+"}
{Num=12,"-"}
{Num=13,"+"} //as in my test string there is no char after 13
This works for me.
//you can change in to MathOper
List<Tuple<int, char>> result = new List<Tuple<int, char>>();
string _mathString = "2-2+10-13"; //Test
char sign = '-';
if (_mathString[0] != '-') //checking the first sign
{
sign = '+';
}
while (_mathString.Length > 0)
{
int nextPl = _mathString.IndexOf('+');
int nextMn = _mathString.IndexOf('-');
if (nextPl == -1 && nextMn == -1) //condition when _mathString contains only number
{
result.Add(new Tuple<int, char>(int.Parse(_mathString), sign));
break;
}
else
{
//getting the end position of first number
int end = nextPl == -1 ? nextMn : (nextMn == -1 ? nextPl : (Math.Min(nextPl, nextMn)));
//retrieving first number
result.Add(new Tuple<int, char>(int.Parse(_mathString.Substring(0, end)), sign));
_mathString = _mathString.Remove(0, end);
//retrieving next sign
sign = _mathString[0];
_mathString = _mathString.Remove(0, 1);
}
}
Try this, I think it works the way you wanted: (Easy to understand solution but not optimal)
public class MathOper
{
public int Num { get; set; }
public string Oper { get; set; } //this display the number will be operated.
}
public class MathOperCreator
{
public readonly string _mathString;//8+2-3
public MathOperCreator(string mathString)
{
this._mathString = mathString.Trim();
}
public List<MathOper> Create()
{
var lMo = new List<MathOper>();
int l = this._mathString.Length;//5
string _mathStringTemp;
char[] charArr = _mathString.ToCharArray();
if (charArr[0] != '+' || charArr[0] != '-')
{
_mathStringTemp = "+"+_mathString;
} else
{
_mathStringTemp = _mathString;
}
char[] delimitersNumb = new char[] { '+', '-' };
string[] numbers = _mathStringTemp.Split(delimitersNumb,
StringSplitOptions.RemoveEmptyEntries);
string[] operators = new string[numbers.Length];
int count = 0;
foreach (char c in _mathStringTemp)
{
if (c == '+' || c == '-')
{
operators[count] = c.ToString();
count++;
}
}
for(int i=0; i < numbers.Length; i++)
{
lMo.Add(new MathOper(){Num = int.Parse(numbers[i]), Oper = operators[i]});
Console.WriteLine(operators[i]+" "+numbers[i]);
}
return lMo;
}
}
I'm trying to remove all references of list using LINQ where the combination of two attributes equals a string.
For instance: I have the object
class obj
{
string a;
string b;
}
and I have a separate string x
so I want to remove where (a+b) == x
Below is the example of what I want to do:
void Main()
{
List<telefone> phones = new List<telefone>()
{
new telefone()
{
ddd = "21", numero="1234"
},
new telefone()
{
ddd = "22",
numero="1234"
}
};
List<string> newPhones = new List<string>(){"1151814088", "11996081170", "098", "890", "99988", "6533"};
for(int i = 0; i < newPhones.Count; i++)
{
phones.Select(x => x.ddd + x.numero).ToList().RemoveAll(x => (x == phones[i]));
}
phones.Dump();
}
public class telefone
{
//[System.Xml.Serialization.XmlIgnore]
internal string hash = String.Empty;
public String ddd { get; set; }
public String numero { get; set; }
public telefone()
{
ddd = String.Empty;
numero = String.Empty;
}
}
You can use Where + Any
var removed = phones.Where(p => !newPhones.Any(np => np == p.ddd + p.numero))
.ToList();
or even better, List.RemoveAll since it doesn't create a new list:
phones.RemoveAll(p => newPhones.Any(np => np == p.ddd + p.numero));
var filteredList = initialList.Where(obj => (obj.a + obj.b) != x);
you may want to check for null strings.