/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2009 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.IO; namespace Logofatu { class P03Integral { private static String FileInputName = "integral.in"; private static String FileOutputName = "integral.out"; private static IFormatProvider formatProvider = System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat; private static double EPS = 0.0001; private static double f(double x) { return 1 / (1 + x * x); } private static double integral(double a, double b) { if ( b - a > EPS ) { double c = (a + b) / 2; return integral(a, c) + integral(c, b); } else { return (b - a) * (f(b) + f(a)) / 2; } } static void Main(string[] args) { StreamReader sr = null; StreamWriter sw = null; try { sr = new StreamReader(FileInputName); sw = new StreamWriter(FileOutputName); while ( !sr.EndOfStream ) { string[] sArray = sr.ReadLine().Split(' '); double a = double.Parse(sArray[0], formatProvider); double b = double.Parse(sArray[1], formatProvider); sw.Write("I("); sw.Write(a.ToString(formatProvider)); sw.Write(", "); sw.Write(b.ToString(formatProvider)); sw.Write(") = "); sw.WriteLine(integral(a, b).ToString("F8",formatProvider)); } } catch ( IOException ex ) { Console.WriteLine("Fehler bei der Dateiverarbeitung!\n" + ex); } finally { if ( sr != null ) { sr.Close(); } if ( sw != null ) { sw.Close(); } } } } }