Advertisement
mrAnderson33

линейная интерполяция (версия 1)

Mar 27th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace razrabotka
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             string[] buf = new string[256];
  14.             var x = new List<float>();
  15.             var f = new List<float>();
  16.  
  17.             System.IO.StreamReader file =
  18.                 new System.IO.StreamReader(args[0]);
  19.             while ((  buf[0] = file.ReadLine()) != null)
  20.             {
  21.                 buf = buf[0].Split(' ');
  22.                 foreach (var s in buf)
  23.                 {
  24.  
  25.                     string temp = s;
  26.                     if(temp.Contains('f'))
  27.                     {
  28.                         temp = temp.Remove(temp.IndexOf('f'), 6);
  29.                         temp = temp.Remove(temp.IndexOf(';'), 1);
  30.                         float tmpf = 0;
  31.                         float.TryParse(temp, out tmpf);
  32.                         f.Add(tmpf);
  33.                     }
  34.                     else  if (temp.Contains('x'))
  35.                     {
  36.                         temp =  temp.Remove(temp.IndexOf('x'), 3);
  37.                         temp = temp.Remove(temp.IndexOf(';'), 1);
  38.                         float tmpx = 0;
  39.                         float.TryParse(temp, out tmpx );
  40.                         x.Add(tmpx);
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             file.Close();
  46.  
  47.             float f2 = f[0] + ((f[1] - f[0]) * (x[1] - x[0])) / (x[2] - x[0]);
  48.             double k = (f[1] - f[0]) / (x[2] - x[0]);
  49.             double b = (x[2] * f[0] - f[1] * x[0]) / (x[2] - x[0]);
  50.  
  51.             Console.WriteLine("функия : f(x) = ({0})*x+({1});",k,b);
  52.             Console.WriteLine("f({0})= {1}", x[1], f2);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement