From 3c04f617ac5d6722c7567c2a8169c80982e01a6b Mon Sep 17 00:00:00 2001 From: sys_27 Date: Sat, 17 Oct 2015 21:01:03 +0300 Subject: [PATCH] Added #106 --- xFunc.Maths/Lexer.cs | 22 ++++++++++++++++------ xFunc.Tests/MathLexerTest.cs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/xFunc.Maths/Lexer.cs b/xFunc.Maths/Lexer.cs index 22bb4a4ed..53c0c8648 100644 --- a/xFunc.Maths/Lexer.cs +++ b/xFunc.Maths/Lexer.cs @@ -368,12 +368,12 @@ public IEnumerable Tokenize(string function) if (letter == '0' && i + 1 < function.Length) { var nextLetter = function[i + 1]; - if (nextLetter == 'x' && i + 2 < function.Length) + if (nextLetter == 'x' && i + 2 < function.Length) // hex { i += 2; length = 1; - for (j = i + 1; j < function.Length && (char.IsDigit(function[j]) || (function[j] >= 97 && function[j] <= 102)); j++) + for (j = i + 1; j < function.Length && (char.IsDigit(function[j]) || (function[j] >= 97 && function[j] <= 102)); j++) // from 'a' to 'f' length++; strNumber = function.Substring(i, length); @@ -383,7 +383,7 @@ public IEnumerable Tokenize(string function) i += length; continue; } - if (nextLetter == 'b' && i + 2 < function.Length) + if (nextLetter == 'b' && i + 2 < function.Length) // bin { i += 2; @@ -398,7 +398,7 @@ public IEnumerable Tokenize(string function) i += length; continue; } - if (char.IsDigit(nextLetter)) + if (char.IsDigit(nextLetter)) // oct { length = 1; for (j = i + 1; j < function.Length && char.IsDigit(function[j]); j++) @@ -413,6 +413,8 @@ public IEnumerable Tokenize(string function) } } + // normal number (dec) + length = 1; for (j = i + 1; j < function.Length && char.IsDigit(function[j]); j++) length++; @@ -424,6 +426,16 @@ public IEnumerable Tokenize(string function) length++; } + if (CheckNextSymbol(function, i + length - 1, 'e')) // exp notation + { + length++; + if (CheckNextSymbol(function, i + length - 1, '-')) + length++; + + for (j = i + length; j < function.Length && char.IsDigit(function[j]); j++) + length++; + } + strNumber = function.Substring(i, length); number = double.Parse(strNumber, CultureInfo.InvariantCulture); tokens.Add(new NumberToken(number)); @@ -432,9 +444,7 @@ public IEnumerable Tokenize(string function) var f = function.Substring(i); if (i < function.Length && char.IsLetter(function[i]) && !notVar.Any(f.StartsWith)) - { tokens.Add(new OperationToken(Operations.Multiplication)); - } continue; } diff --git a/xFunc.Tests/MathLexerTest.cs b/xFunc.Tests/MathLexerTest.cs index 13ba6f796..a2c208685 100644 --- a/xFunc.Tests/MathLexerTest.cs +++ b/xFunc.Tests/MathLexerTest.cs @@ -72,6 +72,41 @@ public void Brackets() Assert.Equal(expected, tokens.ToList()); } + [Fact] + public void ExpNumber1() + { + var tokens = lexer.Tokenize("1.2345E-10"); + var expected = new List + { + new NumberToken(0.00000000012345) + }; + Assert.Equal(expected, tokens.ToList()); + } + + [Fact] + public void ExpNumber2() + { + var tokens = lexer.Tokenize("1.2345E10"); + var expected = new List + { + new NumberToken(12345000000) + }; + Assert.Equal(expected, tokens.ToList()); + } + + [Fact] + public void ExpNumber3() + { + var tokens = lexer.Tokenize("1.2e2 + 2.1e-3"); + var expected = new List + { + new NumberToken(120), + new OperationToken(Operations.Addition), + new NumberToken(0.0021) + }; + Assert.Equal(expected, tokens.ToList()); + } + [Fact] public void Add() {