如何从我的文本文件中读取值并将其存储在变量中以进行数学计算?

how to read values from my text file and store in variables to do mathematical calculations?

       int a=6378137;
       int A0=6367449;
       double B0=16038.43;
       double C0=16.83261;
       double D0=0.021984;
       double E0= 0.000313;
       double k0=0.9996; 
       double e1=0.081819;
       double e1sq = 0.006739497;
       double m = -120;
       double l = 60;
       int n = 11;
       int o = 6 *n- 183;
       double p = ((m - o) * 0.01744444);
       double q = l * 0.0174444;
       double r = m * 0.0174444;
       double s = a * (1 - e1 * e1) / (1 - ((e1 * Math.Sin(q)) * (e1 * Math.Sin(q))*(e1 * Math.Sin(q))));
       double t = a / ((1 - (e1 * Math.Sin(q))));
       double v = A0 * q - B0 * Math.Sin(2 * q) + C0 * Math.Sin(4 * q) - D0 * Math.Sin(6 * q) + E0 * Math.Sin(8 * q);
       double x = v * k0;
       double y = t * Math.Sin(q) * Math.Cos(q) / 2;
       double z = (t * Math.Sin(q) * (Math.Cos(q)* Math.Cos(q)*Math.Cos(q)) / 24) * (5 - (Math.Tan(q)* Math.Tan(q)) + 9 * e1sq * (Math.Cos(q) *Math.Cos(q)) + 4 * e1sq *e1sq * (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) ) * k0;
       double AA = t * Math.Cos(q) * k0;
       double AB = (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) * (t / 6) * (1 - (Math.Tan(q) *Math.Tan(q)) + e1sq * (Math.Cos(q) *Math.Cos(q))) * k0;
       double AC = ((p * p * p * p * p * p) * t * Math.Sin(q) * (Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q)) / 720) * (61 - 58 * (Math.Tan(q) * Math.Tan(q)) + (Math.Tan(q) * Math.Tan(q) * Math.Tan(q) * Math.Tan(q)) + 270 * e1sq * (Math.Cos(q) * Math.Cos(q)) - 330 * e1sq * (Math.Sin(q) * Math.Sin(q))) * k0;
       double AD = (x+y*p*p+z*(p*p*p*p));
       double AE;
       double AF;
       if (AD < 0)
        {
           AE = (10000000 + AD);
        }
       else            {
           AE = AD;
       }
       AF = 500000 + (AA * p + AB * (p * p * p));

       using (StreamWriter writer = new StreamWriter(@"C:\Users\dcf\Desktop\out.txt"))
        {
            writer.WriteLine("north:{0}",AE);
            writer.WriteLine("east:{0}",AF);
        }

这是我的代码,其中 m 和 l 值应具有来自文本文件的值。我的文本文件看起来像

m,l

56536716.534,43843327.880

56584832.507,43501825.240

56669161.226,43199470.844

56715913.464,42856275.950

56782842.247,42435886.070

56810019.308,42071222.859

56836462.600,41706208.191

56900872.591,41282609.754

我需要直接从我的文本文件中读取 m 和 l 值来进行计算。

假设 m 和 l 的值在您的文本文件中交替出现。

首先您应该读入文件,用定界符(例如“,”)拆分值并将它们分配给变量 m 和 l。然后你可以为每个

调用你的计算函数
   private void readFile(pathToFile)
   {
       var textfilecontent= File.ReadAllLines(pathToFile);

       for (int i = 1; i < textfilecontent.Length; i++)
       {
         // splits the values at delimiter ,
         var valPerLine = textfilecontent[i].Split(',');    

         // only works if you have only two numbers per line
         calculate(Convert.ToDouble(valPerLine[0]), Convert.ToDouble(valPerLine[1]))

       }
   }
    private void calculate(double m, double l)
    {
        // your formulas, m and l should now be inserted as parameter
        int a=6378137;
        int A0=6367449;
        double B0=16038.43;
        double C0=16.83261;
        double D0=0.021984;
        double E0= 0.000313;
        double k0=0.9996; 
        double e1=0.081819;
        double e1sq = 0.006739497;

        //double m = -120; 
        //double l = 60;

        int n = 11;
        int o = 6 *n- 183;
        double p = ((m - o) * 0.01744444);
        double q = l * 0.0174444;
        double r = m * 0.0174444;
        double s = a * (1 - e1 * e1) / (1 - ((e1 * Math.Sin(q)) * (e1 * Math.Sin(q))*(e1 * Math.Sin(q))));
        double t = a / ((1 - (e1 * Math.Sin(q))));
        double v = A0 * q - B0 * Math.Sin(2 * q) + C0 * Math.Sin(4 * q) - D0 * Math.Sin(6 * q) + E0 * Math.Sin(8 * q);
        double x = v * k0;
        double y = t * Math.Sin(q) * Math.Cos(q) / 2;
        double z = (t * Math.Sin(q) * (Math.Cos(q)* Math.Cos(q)*Math.Cos(q)) / 24) * (5 - (Math.Tan(q)* Math.Tan(q)) + 9 * e1sq * (Math.Cos(q) *Math.Cos(q)) + 4 * e1sq *e1sq * (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) ) * k0;
        double AA = t * Math.Cos(q) * k0;
        double AB = (Math.Cos(q)*Math.Cos(q)*Math.Cos(q)) * (t / 6) * (1 - (Math.Tan(q) *Math.Tan(q)) + e1sq * (Math.Cos(q) *Math.Cos(q))) * k0;
        double AC = ((p * p * p * p * p * p) * t * Math.Sin(q) * (Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q) * Math.Cos(q)) / 720) * (61 - 58 * (Math.Tan(q) * Math.Tan(q)) + (Math.Tan(q) * Math.Tan(q) * Math.Tan(q) * Math.Tan(q)) + 270 * e1sq * (Math.Cos(q) * Math.Cos(q)) - 330 * e1sq * (Math.Sin(q) * Math.Sin(q))) * k0;
        double AD = (x+y*p*p+z*(p*p*p*p));
        double AE;
        double AF;

        if (AD < 0)
        {
          AE = (10000000 + AD);
        }
        else           
        {
         AE = AD;
        }

       AF = 500000 + (AA * p + AB * (p * p * p));
       using (StreamWriter writer = new StreamWriter(@"C:\Users\dcf\Desktop\out.txt"))
       {
         writer.WriteLine("north:{0}",AE);
         writer.WriteLine("east:{0}",AF);
       }
    }