计算百分比明智的统计使用C sharp

本文关键字:sharp 统计 百分比 计算 | 更新日期: 2023-09-27 18:02:09

我想找到统计数据,将计算百分比,并给出以下格式的结果:WX GSA搜索% = GSA搜索次数/GSA搜索次数+搜索次数+ ADVSearch搜索次数* 100,即在这种情况下,它应该给出如下结果:3/3 + 2 + 2 * 100 = 42.8

我已经尝试了代码,将给出每次搜索的出现:

我的代码在这里:

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

计算百分比明智的统计使用C sharp

我真的不明白这个问题。你的意思是计算公式并将其显示在主机屏幕上吗?如果你是这个意思,这里是你需要的代码:

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

你可以在。net中使用String。格式方法。

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