math-suite/Source/GraphingCalculator/Controls/IntegralWindow.xaml.cs

94 lines
3.1 KiB
C#
Raw Permalink Normal View History

2018-02-05 23:24:46 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace GraphingCalculator.Controls
{
/// <summary>
/// Interaction logic for LimitWindow.xaml
/// </summary>
public partial class IntegralWindow : Window
{
#region Properties
public string ExpressionString
{
get { return inputExpression.Text; }
set { inputExpression.Text = value; }
}
#endregion
#region Constructors
public IntegralWindow()
{
InitializeComponent();
ExpressionString = "";
}
public IntegralWindow(string expression)
{
InitializeComponent();
ExpressionString = expression;
}
#endregion
#region Other routines
private void Result (string result, bool error = false)
{
outputResult.Text = result;
if (error) outputResult.Foreground = Brushes.DarkRed;
else outputResult.Foreground = Brushes.Black;
}
#endregion
#region Button handlers
private void buttonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void buttonCalculate_Click(object sender, RoutedEventArgs e)
{
double val_beg, val_end, integr_res;
Expression beg, end, expr;
beg = new Expression(inputIntervalBeg.Text);
end = new Expression(inputIntervalEnd.Text);
expr = new Expression(inputExpression.Text);
// Try to evaluate interval ends
try { val_beg = beg.Evaluate(); }
catch { Result("Failed to evaluate interval start.", true); return; }
try { val_end = end.Evaluate(); }
catch { Result("Failed to evaluate interval end.", true); return; }
// Try to calculate the integral
try { integr_res = Integrator.Integrate(expr, val_beg, val_end, "x"); }
catch (Exception ex)
{
Result("Failed to evaluate expression. Message: " + ex.Message, true);
Log.LogEvent("Failed to integrate expression: {0}", expr.ExpressionString);
Log.LogEvent("> Interval: [{0}, {1}] = [{2}, {3}]", beg.ExpressionString, end.ExpressionString, val_beg, val_end);
Log.LogEvent("> Message: {0}", ex.Message);
Log.LogEvent("> Stack trace: {0}", ex.StackTrace);
return;
}
// Show result
Result(Math.Round(integr_res, 14).ToString());
Log.LogEvent("Integrated expression: {0}", expr.ExpressionString);
Log.LogEvent("> Interval: [{0}, {1}] = [{2}, {3}]", beg.ExpressionString, end.ExpressionString, val_beg, val_end);
Log.LogEvent("> Result: {0}", integr_res);
}
#endregion
}
}