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

扶醉桌前 提交于 2019-12-06 09:17:21

问题


The project is based on Eye Tracker. Let me brief the idea behind the project to understand my problem better.

I have the hardware of Tobii C eye tracker. This eye tracker will be able to give out coordinates of the X, Y of where I am looking at. But this device is very sensitive. When I look at 1 point, the eye tracker will send out many different data of coordinates but within ± 100 range which I found out. Even though you are staring at 1 point, your eyes keep moving, therefore giving out many data. This many data (float numbers) are then saved in a text file. Now I only need 1 data (X coordinate) which signifies the 1 point I am staring instead of the many data which are within the ± 100 range and move it to a new text file.

I have no idea how I should code to do that.

These are the float numbers in the text file.

200
201
198
202
250
278
310
315
360
389
500
568
579
590

When I stare at point 1, the data are 200-300, which are within the ± 100 range. I wanna set the 200 as reference point subtracts itself with the next number and check if the resultant value within 100, if it is, remove them. The reference point should keep doing that to the following numbers until it reaches outside the ± 100 range. Once outside the 100 range, now the number is 310, then now this number is the next reference point and do the same and subtract with the following numbers below and check if the resultant value within 100. Once outside the 100 range, the next number is 500, now, that is the new reference point, and do the same. That is my objective. To put it to simpler terms, The reference points should be moved into a new file.

This is my code so far which get the gaze coordinates and stores them in a text file.

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
 using Tobii.Interaction;

namespace ConsoleApp1
{

class Program
{

    private static void programintro()
    {
        Console.WriteLine("Press Any Keys To Start");
    }
    public static void Main(string[] args)
    {

        programintro();
        Console.ReadKey();
        double currentX = 0.0;
        double currentY = 0.0;
        double timeStampCurrent = 0.0;
        double diffX = 0.0;
        double diffY = 0.0;
        int counter = 0;
        var host = new Host();
        host.EnableConnection();
        var gazePointDataStream = host.Streams.CreateGazePointDataStream();
        gazePointDataStream.GazePoint((gazePointX, gazePointY, timestamp) =>

        {
            diffX = gazePointX - currentX;
            diffY = gazePointY - currentY;
            currentX = gazePointX;
            currentY = gazePointY;
            timeStampCurrent = timestamp;
            if (diffX > 100 || diffX <= -100 || diffY >= 100 || diffY <= -100)
            {
                counter++;
                using (StreamWriter writer = new StreamWriter("C: \\Users\\Student\\Desktop\\FYP 2019\\ConsoleApp1\\ConsoleApp1\\Data\\TextFile1.txt", true))
                {
                    writer.WriteLine("Recorded Data " + counter + "\n=================================================================================================================\nX: {0} Y:{1}\nData collected at {2}", currentX, currentY, timeStampCurrent);
                    writer.WriteLine("=================================================================================================================");
                }
                Console.WriteLine("Recorded Data " + counter + "\n=================================================================================================================\nX: {0} Y:{1}\nData collected at {2}", currentX, currentY, timeStampCurrent);
                Console.WriteLine("=================================================================================================================");
            }
        });
        //host.DisableConnection();
        while (true)
        {
            if (counter <  10)
            {
                continue;
            }
            else
            {

                Environment.Exit(0);

            }
        }

Now my Question is how do I code to read the text file and set a reference number and subtracts itself with the next number and check if the resultant value within 100 and have a new reference number if it outside the ± 100 range. Those reference numbers are then stored in a new text file.


回答1:


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



来源:https://stackoverflow.com/questions/56625136/setting-a-reference-number-and-comparing-that-to-other-data-in-textfile

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