Setting a reference number and comparing that to other data in textfile

那年仲夏 提交于 2019-12-04 17:20:24

Based on your sample data here is the code to get only numbers which has 100+ difference.

static void Main(string[] args)
{
  string filename = @"C:\PowershellScripts\test.txt"; // INPUT FILE
  String outputFile = @"C:\PowershellScripts\result.txt"; // OUTPUT FILE

  string[] data = File.ReadAllLines(filename); // READING FORM FILE
  int TotalLine = data.Length; // COUNT TOTAL NO OF ROWS
  List<string> FinalList = new List<string>(); // INITIALIZE LIST FOR FINAL RESULT

  if (TotalLine <= 0) // CHECK IF FILE HAS NO DATA
  {
      Console.WriteLine("No Data found !");
      return;
  }

  double CurrentNumber = double.Parse(data[0]), NextNumber = 0, diff = 0; // INITIALIZE OF LOCAL VARIABLES, CURRENT NUMBER = FIRST NO FROM FILE

  for (int cntr = 1; cntr < TotalLine; cntr++) // FOR LOOP FOR EACH LINE
  {
      NextNumber = double.Parse(data[cntr]); //PARSING NEXT NO
      diff = CurrentNumber - NextNumber; // GETTING DIFFERENCE

      if (diff <= 100 && diff >= -100) // MATCH THE DIFFERENCE
      {
          continue; // SKIP THE LOGIC IF DIFF IS LESS THEN 100
      }
      else
      {
          FinalList.Add(CurrentNumber.ToString()); // ADDING THE NO TO LIST
          CurrentNumber = NextNumber; // POINTING TO NEXT NO
      }

  }

  FinalList.Add(CurrentNumber.ToString()); // ADDING LAST NO.
  foreach (string d in FinalList) // FOR EACH LOOP TO PRINT THE FINAL LIST
      Console.WriteLine(d);

  File.WriteAllLines(outputFile, FinalList); // SAVING TO THE FILE

}

The above program will generate the output is :

200
310
500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!