2015-01-26 5 views
0

입력을받는 프로그램이 있습니다. 첫 번째 줄은 주어진 좌표 수이고, 나머지 줄은 좌표입니다. 모든 좌표를 읽고 두 점 사이의 최단 거리 (두 개의 가장 가까운 좌표 사이의 거리)를 찾은 다음 해답을 찾는 데 걸린 거리와 시간을 표시합니다.C에서 ReadLine 메서드를 사용합니다.

내 문제는 프로그램을 실행할 때 키를 입력하여 "답변을 찾는 데 걸린 시간"을 계산합니다. 나는 을 콘솔에 붙여 넣어야한다. 그런 다음 즉시 입력하여 너무 많은 시간이 경과하지 않도록한다.

내 질문은 : 프로그램의 Stopwatch 객체가 키를 눌러야하는 입력을 기다릴 필요가 없도록 ReadLine 메서드를 소비하는 가장 간단한 방법은 무엇입니까. 나는이 문제를 파악할 수 없다. 나는 문제가 ReadLine 방법 누를 수를 입력 기다리고 있음을 믿습니다 http://hastebin.com/zulowuvuyu.lisp

:

여기
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Diagnostics; 
using System.Numerics; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double smallestDistance = 0.0; 
      int numberOfPoints = 0; 
      String userInput = ""; 
      String coordinate = ""; 
      int coordinatesEntered = 0; 

      //Grab the number of points that will be used. 
      userInput = Console.ReadLine(); 

      //Try and convert and store the input to an integer. 
      numberOfPoints = Convert.ToInt32(userInput); 

      int[] xcoords = new int[numberOfPoints]; 
      int[] ycoords = new int[numberOfPoints]; 

      //Create a new stopwatch in order to time program. 
      Stopwatch sw = Stopwatch.StartNew(); 

      //Get all the points. 
      while(coordinatesEntered < numberOfPoints) 
      { 
       coordinate = Console.ReadLine(); 
       string[] numbers = coordinate.Split(' '); 

       xcoords[coordinatesEntered] = Convert.ToInt32(numbers[0]); 
       ycoords[coordinatesEntered] = Convert.ToInt32(numbers[1]); 

       coordinatesEntered++; 
      }//end while 

      smallestDistance = getShortestDistance(xcoords, ycoords, numberOfPoints); 
      sw.Stop (); 
      Console.Write ("\n"); 
      Console.WriteLine(string.Format("{0:N6}", Math.Sqrt(smallestDistance))); 
      Console.WriteLine ("Time used: {0} secs", sw.Elapsed.TotalMilliseconds/1000); 
      Console.ReadKey (); 
     } 

     public static double getShortestDistance(int[] x, int[] y, int numOfCoords) 
     { 
      int i; 
      int j; 

      double shortestDistance = 0; 

      for (i = 0; i<numOfCoords-1 ; i++) 
      { 
       for (j= i+1 ; j<numOfCoords ; j++) 
       { 
        double xresultsqrd = Math.Pow((x[j] - x[i]), 2.0); 
        double yresultsqrd = Math.Pow((y[j] - y[i]), 2.0); 

        double finalResult = xresultsqrd + yresultsqrd; 

        if(i == 0) 
        { 
         shortestDistance = finalResult; 
        }else if(finalResult < shortestDistance){ 
         shortestDistance = finalResult; 
        }//end elseif 
       }//end inner-for 
      }//end outer-for 

      return shortestDistance; 
     }//end getShortestDistance; 

    }//end class Program 
}//end namespace ConsoleApplication2 

입력에 필요한 TXT 파일입니다

여기 내 프로그램입니다 하지만 이후 나는 붙여 넣기 아니요 Enter 키를 읽은 적이 있습니다. ReadLine이 Enter 키를 누르는 것을 멈추게하려면 어떻게해야합니까 (일명 메서드 사용).

+0

당신이 시간을 측정하려고 사용하여 스트림으로 입력을 읽고 (파일과 같은) 다른 소스에서 읽기 :

더 나은 솔루션을하는 것입니다 좌표를 입력하는 데 걸리는 시간은? 아니면 계산을 수행하는 데 걸리는 시간은 얼마입니까? –

+1

또한 데이터가 이미 파일에 있으면 (어딘가에서 복사하는 중) 파일을 읽지 않는 이유는 무엇입니까? –

+0

가장 좋은 것은 파일에서 입력 한 내용을 @Rufus L이 언급 한대로 읽는 것입니다. –

답변

0

여러 가지 방법이 있지만 입력 목록에 캐리지 리턴을 추가하는 것이 트릭입니다.

  • Console.In
+0

답변보다는 댓글로 추가하는 것이 더 낫다. –

+0

왜? 질문에 대답하지 않습니까? 그는 가장 간단한 해결책을 묻습니다 – slvnperron

+0

수정 된 답변이 더 나은 것 같습니다. 코드 스 니펫도 좋습니다. –

관련 문제