2013-03-05 3 views
0

%를 계산하고 다음 형식으로 결과를 제공하려는 통계를 찾고 싶습니다. WX GSA 검색 % = GSA 발생 수/GSA 발생 수 + 검색 발생 수 + ADVSearch 발생 수 * 100 즉이 경우 3/3과 같아야합니다. + 2 + 2 * 100 = 나는 모든 검색의 발생을 줄 것이다 코드를 시도 42.8C sharp를 사용하여 현명한 통계를 계산 하시겠습니까?

:

내 코드는 여기에 있습니다 :

class Program 
{ 
    static void Main() 
    { 
     System.IO.StreamReader myFile = 
     new System.IO.StreamReader(@"C:\Users\karansha\Desktop\sample log.txt"); 
     string myString = myFile.ReadToEnd(); 
     Console.WriteLine(TextTool.CountStringOccurrences(myString, "WX Search")); // WX Rule Based Search. 
     Console.WriteLine(TextTool.CountStringOccurrences(myString, "WX GSA Search")); // WX GSA Search. 
     Console.WriteLine(TextTool.CountStringOccurrences(myString, "WX ADVSearch")); //WX Form Based Search. 
     // keep screen from going away 
     // when run from VS.NET 
     Console.ReadLine(); 
    } 
} 
public static class TextTool 
{ 
    public static int CountStringOccurrences(string text, string pattern) 
    { 
     int count = 0; 
     int i = 0; 
     while ((i = text.IndexOf(pattern, i)) != -1) 
     { 
      i += pattern.Length; 
      count++; 
     } 
     return count; 
    } 
} 
+0

나는 다음과 같은 형식으로 로그가 : WX GSA 검색 = 서버 : yukon.corp.wx.com WX GSA 검색 = 서버 : yukon.corp.wx합니다. com WX GSA 검색 = 서버 : yukon.corp.wx.com WX 검색 = 서버 : yukon.corp.wx.com WX 검색 = 서버 : yukon.corp.wx.comWX ADVSearch = 서버 : yukon.corp.wx. com WX ADVSearch = 서버 : yukon.corp.wx.com –

+0

[무엇을 시도 했습니까?] (http://whathaveyoutried.com) 또한 의견으로 추가하는 대신 질문에 추가 정보를 편집하십시오. – Nuffin

+0

내가 찾고 싶습니다. 연령을 계산하고 다음 형식으로 결과를 제공하는 통계 : WX GSA 검색 % = GSA 발생 수/GSA 발생 수 + 검색 발생 수 + ADVSearch 발생 수 * 100 즉,이 경우 3/3 + 2 + 2 * 100 = 42.8과 같아야합니다. –

답변

0

정말 질문을 이해하지 못합니다. 수식을 계산하고 콘솔 화면에 표시한다는 의미일까요? 여기이 의미하는 경우 코드는 당신이 필요로된다

static void Main() 
{ 
    System.IO.StreamReader myFile = 
    new System.IO.StreamReader(@"C:\Users\karansha\Desktop\sample log.txt"); 
    string myString = myFile.ReadToEnd(); 
    int wxSearchCount = TextTool.CountStringOccurrences(myString, "WX Search"); // WX Rule Based Search. 
    int wxGsaSearchCount = TextTool.CountStringOccurrences(myString, "WX GSA Search"); // WX GSA Search. 
    int wxAdvSearchCount = TextTool.CountStringOccurrences(myString, "WX ADVSearch"); //WX Form Based Search. 

      double result = Convert.ToDouble(wxGsaSearchCount * 100)/Convert.ToDouble((wxGsaSearchCount + wxSearchCount + wxAdvSearchCount)); 

      Console.WriteLine("WX Search: {0}\nWX GSA Search: {1}\nWX ADVSearch: {2}\nResult: {3}", 
           wxSearchCount, wxGsaSearchCount, wxAdvSearchCount, result.ToString("0.00")); 

    Console.ReadLine(); 
} 
+0

선생님이 코드는 이미 당신과 공유 한 것과 똑같습니다. 이제 우리는 모든 통계치를 얻었습니다. 나는 현명한 통계치를 찾고 싶습니다. 예 : GSA 검색 % stats = Gsa 검색/Gsa 검색 + 검색 + ADV 검색 및 그 비율 * 100 –

+0

같을 수 있습니다 : 당신이 필요로하는 것 같아요 : double result = wxGsaSearchCount/(wxGsaSearchCount + wxSearchCount + wxAdvSearchCount) * 100. Am 내가 잘못 생각한거야? –

+0

네, 맞습니다. 하지만 여기서 "결과", 콘솔 창 0을 표시 할 때 한 싸움이 있지만 "wxGsaSearchCount"인쇄 할 때 적절한 값을 제공합니다. 두 번째 결과 = wxGsaSearchCount/(wxGsaSearchCount + wxSearchCount + wxAdvSearchCount) * 100; –

0

당신은 String.Format를 사용하여 .NET에서 문자열을 포맷 할 수 있습니다 방법.

int score = 50; 
string formattedString = String.Format("My Score : {0}", score); 
Console.WriteLine(formattedString); 
//My Score : 50 
+0

내 기록을 보았습니까? 또한 코드에서 구현해야하는 공식을 언급했습니다. –

+0

링크에서 String.Format 문서를 읽으십시오. 읽은 후에해야 할 일에 대해 잘 모르는 경우 기꺼이 도와 드리겠습니다. – Yeonho

관련 문제