oop курсовая. Исследование вычислительной эффективности объектноориентированных приложений
Скачать 0.82 Mb.
|
Файл MainWindow.xamlxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:oop_kurs_procedur" mc:Ignorable="d" Title="ProcedurMonteCarlo" Height="231.196" Width="580.5" Loaded="WindowLoaded"> Файл MainWindow.xaml.cs (объектно-ориентированное приложение)using System; using System.Windows; using System.Data; namespace Application2 { /// /// Логика взаимодействия для MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Start_Btn_Click(object sender, RoutedEventArgs e) { if (!(string.IsNullOrEmpty(textBox_Ax.Text) || string.IsNullOrWhiteSpace(textBox_Ax.Text) || string.IsNullOrEmpty(textBox_Cx.Text) || string.IsNullOrWhiteSpace(textBox_Cx.Text) || string.IsNullOrEmpty(textBox_Kx.Text) || string.IsNullOrWhiteSpace(textBox_Kx.Text) || string.IsNullOrEmpty(textBox_Ay.Text) || string.IsNullOrWhiteSpace(textBox_Ay.Text) || string.IsNullOrEmpty(textBox_Cy.Text) || string.IsNullOrWhiteSpace(textBox_Cy.Text) || string.IsNullOrEmpty(textBox_Ky.Text) || string.IsNullOrWhiteSpace(textBox_Ky.Text))) { Point a = new Point(double.Parse(textBox_Ax.Text), double.Parse(textBox_Ay.Text)); Point c = new Point(double.Parse(textBox_Cx.Text), double.Parse(textBox_Cy.Text)); Point k = new Point(double.Parse(textBox_Kx.Text), double.Parse(textBox_Ky.Text)); if (a.X < c.X && a.Y < c.Y && k.X < c.X && k.X > a.X && k.Y < c.Y && k.Y > a.Y) { int N = 1000; string[] str = new string[5]; DataTable dt = new DataTable(); str[0] = "#"; str[1] = "N"; str[2] = "Площадь"; str[3] = "Погрешность, %"; str[4] = "Время, мс"; for (int j = 0; j < str.Length; j++) { dt.Columns.Add(str[j]); } Result_dataGrid.Columns.Clear(); MonteCarlo mc = new MonteCarlo(a, c, k); label_square.Content = "Площадь фигуры: " + Math.Round(mc.S_real, 3); for (int i = 1; i <= 5; i++) { mc.Start(N); str = mc.Print(); str[0] = i.ToString(); str[1] = N.ToString(); dt.Rows.Add(str); N = N * 10; } Result_dataGrid.ItemsSource = dt.DefaultView; GC.Collect(); } else { MessageBox.Show("Точки заданы неверно", "Ошибка координат", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Заданы не все координаты", "Ошибка координат", MessageBoxButton.OK, MessageBoxImage.Error); } } private void Clear_Btn_Click(object sender, RoutedEventArgs e) { textBox_Ax.Clear(); textBox_Ay.Clear(); textBox_Cx.Clear(); textBox_Cy.Clear(); textBox_Kx.Clear(); textBox_Ky.Clear(); Result_dataGrid.Columns.Clear(); label_square.Content = "Площадь фигуры: "; Enter_CheckBox.IsChecked = false; Example_CheckBox.IsChecked = false; Start_Btn.IsEnabled = false; Clear_Btn.IsEnabled = false; } private void Example_click(object sender, RoutedEventArgs e) { if (Example_CheckBox.IsChecked == true) { Enter_CheckBox.IsChecked = false; textBox_Ax.Text = "0"; textBox_Ay.Text = "0"; textBox_Cx.Text = "400"; textBox_Cy.Text = "200"; textBox_Kx.Text = "300"; textBox_Ky.Text = "150"; textBox_Ax.IsEnabled = false; textBox_Ay.IsEnabled = false; textBox_Cx.IsEnabled = false; textBox_Cy.IsEnabled = false; textBox_Kx.IsEnabled = false; textBox_Ky.IsEnabled = false; Start_Btn.IsEnabled = true; Clear_Btn.IsEnabled = true; } if (Example_CheckBox.IsChecked == false) { Enter_CheckBox.IsChecked = true; Example_CheckBox.IsChecked = false; textBox_Ax.Clear(); textBox_Ay.Clear(); textBox_Cx.Clear(); textBox_Cy.Clear(); textBox_Kx.Clear(); textBox_Ky.Clear(); textBox_Ax.IsEnabled = true; textBox_Ay.IsEnabled = true; textBox_Cx.IsEnabled = true; textBox_Cy.IsEnabled = true; textBox_Kx.IsEnabled = true; textBox_Ky.IsEnabled = true; } } private void Enter_click(object sender, RoutedEventArgs e) { if (Enter_CheckBox.IsChecked == true) { Example_CheckBox.IsChecked = false; textBox_Ax.Clear(); textBox_Ay.Clear(); textBox_Cx.Clear(); textBox_Cy.Clear(); textBox_Kx.Clear(); textBox_Ky.Clear(); textBox_Ax.IsEnabled = true; textBox_Ay.IsEnabled = true; textBox_Cx.IsEnabled = true; textBox_Cy.IsEnabled = true; textBox_Kx.IsEnabled = true; textBox_Ky.IsEnabled = true; Start_Btn.IsEnabled = true; Clear_Btn.IsEnabled = true; } if (Enter_CheckBox.IsChecked == false) { Example_CheckBox.IsChecked = true; Enter_CheckBox.IsChecked = false; textBox_Ax.Text = "0"; textBox_Ay.Text = "0"; textBox_Cx.Text = "400"; textBox_Cy.Text = "200"; textBox_Kx.Text = "300"; textBox_Ky.Text = "150"; textBox_Ax.IsEnabled = false; textBox_Ay.IsEnabled = false; textBox_Cx.IsEnabled = false; textBox_Cy.IsEnabled = false; textBox_Kx.IsEnabled = false; textBox_Ky.IsEnabled = false; } } private void Window_Loaded(object sender, RoutedEventArgs e) { Result_dataGrid.ColumnWidth = Result_dataGrid.Width / 5 - 2; Result_dataGrid.CanUserAddRows = false; Start_Btn.IsEnabled = false; Clear_Btn.IsEnabled = false; } } } Файл MainWindow.xamlxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:oop_kurs_oop" mc:Ignorable="d" Title="OOPMC" Height="292" Width="580.5" Loaded="Window_Loaded"> Файл Figure.csusing System; using System.Windows; namespace Application2 { abstract public class MyFigure { abstract public double Square(); abstract public bool IsInFigure(Point point); } } Файл Rectangle.csusing System; using System.Windows; namespace Application2 { public class MyRectangle : MyFigure { private Point a, c; public MyRectangle() { a = new Point(0, 0); c = new Point(1, 1); } public MyRectangle(Point a, Point c) { this.a = a; this.c = c; } public MyRectangle(MyRectangle rect) { a = rect.a; c = rect.c; } override public double Square() { double sq; sq = Math.Abs((a.X - c.X) * (a.Y - c.Y)); return sq; } override public bool IsInFigure(Point Point) { if (Point.X <= c.X && Point.X >= a.X && Point.Y >= a.Y && Point.Y <= c.Y) return true; else return false; } } } Файл Triangle.csusing System; using System.Windows; namespace Application2 { public class MyTriangle : MyFigure { private Point c, k, d; public MyTriangle() { c = new Point(0, 0); k = new Point(0, 1); d = new Point(1, 0); } public MyTriangle(Point c, Point k, Point d) { this.c = c; this.k = k; this.d = d; } public MyTriangle(MyTriangle triangle) { c = triangle.c; k = triangle.k; d = triangle.d; } override public bool IsInFigure(Point point) { double y_kc = (point.X - k.X) * ((c.Y - k.Y) / (c.X - k.X)) + k.Y; double y_kd = (point.X - k.X) * ((d.Y - k.Y) / (d.X - k.X)) + k.Y; if (point.Y < y_kc && point.Y > y_kd) return true; else return false; } override public double Square() { double square = Math.Abs((c.X - k.X) * (c.Y - d.Y)) / 2; return square; } } } Файл MonteCarlo.csusing System; using System.Collections.Generic; using System.Windows; using System.Diagnostics; namespace Application2 { public class MonteCarlo { private MyFigure abcd, obmf, oaf, ckd; private List points; private Point a, c, k, f; private double s_find, s_rect; private double time, err; public double S_real { get; } public MonteCarlo(Point a, Point c, Point k) { s_find = 0; time = 0; err = 0; this.a = a; this.c = c; this.k = k; f = new Point(a.X + Math.Abs(a.Y - c.Y) / 2, a.Y + Math.Abs(a.Y - c.Y) / 2); abcd = new MyRectangle(a, c); oaf = new MyCircle(new Point(a.X, a.Y + Math.Abs(a.Y - c.Y) / 2), Math.Abs(a.Y - c.Y) / 2); obmf = new MyRectangle(new Point(a.X, a.Y + Math.Abs(a.Y - c.Y) / 2), new Point(a.X + Math.Abs(a.Y - c.Y) / 2, c.Y)); ckd = new MyTriangle(c, k, new Point(c.X, a.Y)); points = new List (); s_rect = abcd.Square(); S_real = s_rect - (obmf.Square() + ckd.Square() + oaf.Square() / 4.0); } private void Distribution(int N) { Random Rand = new Random(); for (int i = 0; i < N; i++) { Point point1 = new Point(a.X + (c.X - a.X) * Rand.NextDouble(), a.Y + (c.Y - a.Y) * Rand.NextDouble()); points.Add(point1); } } private bool IsInFigure(Point p) { if (p.X > k.X) { if (ckd.IsInFigure(p)) return false; return true; } if (p.X < f.X) { if (p.Y >= f.Y) { return false; } if (p.Y < f.Y) { if (oaf.IsInFigure(p)) return false; } } return true; } private void MonteCarloSquare(int N) { int M = 0; for (int i = 0; i < N; i++) { if (IsInFigure(points[i])) M++; } s_find = s_rect * ((double)M / N); } public void Start(int N) { Stopwatch Watch = new Stopwatch(); Watch.Restart(); Distribution(N); MonteCarloSquare(N); Watch.Stop(); time = Watch.ElapsedMilliseconds; err = Error(); } private double Error() { return (Math.Abs(S_real - s_find) / S_real) * 100; } public string[] Print() { string[] str = new string[5]; str[2] = Math.Round(s_find, 3).ToString(); str[3] = Math.Round(err,3).ToString(); str[4] = Math.Round(time, 3).ToString(); return str; } } } |