using System; using System.Collections.Generic; using System.IO; 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.Navigation; using System.Windows.Shapes; using Microsoft.Win32; using System.Xml.Linq; using System.Diagnostics; namespace HradlovaSit { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { OpenFileDialog ofdSit, ofdIn; Mode mode; public static Net net; public Window1() { InitializeComponent(); ofdSit = new OpenFileDialog(); ofdSit.Title = "Vyberte soubor s popisem sítě v XML"; ofdIn = new OpenFileDialog(); ofdIn.Title = "Vyberte soubor se vstupem."; mode = Mode.Evaluate; } public static void LoadFromXML(string path) { XDocument doc = XDocument.Load(path); var elem = (from c in doc.Descendants() where c.NodeType == System.Xml.XmlNodeType.Element && c.Name.LocalName.ToLower() == "net" select c).First(); net = new Net(); if (!net.LoadFromXML(elem)) throw new Exception(); } private void SaveToXML(string path) { } private void Edit_Checked(object sender, RoutedEventArgs e) { if (this.InteractiveMode == null) return; this.InteractiveMode.IsChecked = false; this.EvaluateMode.IsChecked = false; this.mode = Mode.Edit; } private void Interactive_Checked(object sender, RoutedEventArgs e) { if (this.InteractiveMode == null) return; this.EditMode.IsChecked = false; this.EvaluateMode.IsChecked = false; this.mode = Mode.Interactive; } private void Evaluate_Checked(object sender, RoutedEventArgs e) { if (this.InteractiveMode == null) return; this.InteractiveMode.IsChecked = false; this.EditMode.IsChecked = false; this.mode = Mode.Evaluate; } private void Close_Click(object sender, RoutedEventArgs e) { this.Close(); } private void Draw() { } private void Open_Click(object sender, RoutedEventArgs e) { if (ofdSit.ShowDialog().Value) { if (mode == Mode.Edit || ofdIn.ShowDialog().Value) { try { LoadFromXML(ofdSit.FileName); } catch (Exception) { MessageBox.Show("Nepodařilo se otevřít soubor s popisem.", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (mode != Mode.Edit) Evaluate(ofdIn.FileName, true); } } } public static string Evaluate(string inFile, bool showOutput) { StreamReader sr = null; StreamWriter sw = null; try { sr = File.OpenText(inFile); string outfile = inFile; if (outfile.Contains(".in")) { outfile = outfile.Replace(".in", ".out"); } try { sw = File.CreateText(outfile); } catch (Exception) { outfile = "C:" + outfile.Substring(outfile.LastIndexOf('\\')); sw = File.CreateText(outfile); } string line; while (!sr.EndOfStream && (line = sr.ReadLine()) != null) { if (net.InsertInputs(line.Trim())) { if (net.Evaluate()) { sw.WriteLine(net.OutputsToString()); } else MessageBox.Show("Při vyhodnocování nastala chyba.", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } else MessageBox.Show("Špatný formát souboru se vstupními daty.", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } if(showOutput) Process.Start(outfile); return outfile; } catch (Exception) { MessageBox.Show("Nepodařilo se otevřít soubor(y).", "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); return null; } finally { if (sw != null) sw.Close(); if (sr != null) sr.Close(); } } public enum Mode { Evaluate, Interactive, Edit } } }