我想查找将按以下格式计算百分比并给出结果的统计数据: 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;
}
}
答案 0 :(得分:0)
您可以使用String.Format方法在.NET中格式化字符串。
int score = 50;
string formattedString = String.Format("My Score : {0}", score);
Console.WriteLine(formattedString);
//My Score : 50
答案 1 :(得分: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();
}