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

本文关键字:变量 存储 计算 我的 文本 文件 读取 | 更新日期: 2023-09-27 18:02:28

       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);
       }
    }