/*
* Author: MANIKANDAN S
* Email: manig99@gmail.com
* Date: 12 April 2018
*/
using NCalc;
using System;
using System.Linq;
namespace Mehroz
{
/// <summary>
/// Classes Contained:
/// Application
/// Fraction
/// FractionException
/// </summary>
/// <summary>
/// The class demonstrates the Fraction class
/// </summary>
public class Application
{
static decimal Fraction(string fs)
{
fs = fs.Replace('+', 'a').Replace('-', 'b').Replace('*', 'c');
var fs1 = fs.Split('a', 'b', 'c');
fs1 = fs1.OrderByDescending(x => x.Length).ToArray();
foreach (var number in fs1)
{
fs = fs.Replace(number, GetResult(number).ToString());
}
fs = fs.Replace('a', '+').Replace('b', '-').Replace('c', '*');
var expression = new Expression(fs);
var result = expression.Evaluate();
return Convert.ToDecimal(result);
}
public static decimal GetResult(string value)
{
var numbers = value.Split(' ');
decimal temp, result = 0.0m;
decimal numerator, denominator;
foreach (var str in numbers)
{
if (decimal.TryParse(str, out temp))
{
result += temp;
}
else if (str.Contains("/"))
{
var frac = str.Split('/');
decimal.TryParse(frac[0], out numerator);
decimal.TryParse(frac[1], out denominator);
result += numerator / denominator;
}
}
return result;
}
public static void Main()
{
try
{
decimal d = Fraction("2 5/7 + 5/7 * 1 1/4");
Console.WriteLine(string.Format("The Result is ...... {0}", d));
} //end try
catch (Exception exp)
{
Console.WriteLine("\nSystem error: " + exp.Message);
}
Console.Read();
} //end main
}//end class Application
} //end namespace
* Author: MANIKANDAN S
* Email: manig99@gmail.com
* Date: 12 April 2018
*/
using NCalc;
using System;
using System.Linq;
namespace Mehroz
{
/// <summary>
/// Classes Contained:
/// Application
/// Fraction
/// FractionException
/// </summary>
/// <summary>
/// The class demonstrates the Fraction class
/// </summary>
public class Application
{
static decimal Fraction(string fs)
{
fs = fs.Replace('+', 'a').Replace('-', 'b').Replace('*', 'c');
var fs1 = fs.Split('a', 'b', 'c');
fs1 = fs1.OrderByDescending(x => x.Length).ToArray();
foreach (var number in fs1)
{
fs = fs.Replace(number, GetResult(number).ToString());
}
fs = fs.Replace('a', '+').Replace('b', '-').Replace('c', '*');
var expression = new Expression(fs);
var result = expression.Evaluate();
return Convert.ToDecimal(result);
}
public static decimal GetResult(string value)
{
var numbers = value.Split(' ');
decimal temp, result = 0.0m;
decimal numerator, denominator;
foreach (var str in numbers)
{
if (decimal.TryParse(str, out temp))
{
result += temp;
}
else if (str.Contains("/"))
{
var frac = str.Split('/');
decimal.TryParse(frac[0], out numerator);
decimal.TryParse(frac[1], out denominator);
result += numerator / denominator;
}
}
return result;
}
public static void Main()
{
try
{
decimal d = Fraction("2 5/7 + 5/7 * 1 1/4");
Console.WriteLine(string.Format("The Result is ...... {0}", d));
} //end try
catch (Exception exp)
{
Console.WriteLine("\nSystem error: " + exp.Message);
}
Console.Read();
} //end main
}//end class Application
} //end namespace