Created
October 6, 2018 15:49
-
-
Save nevack/177895a71de4dc2b3b478026fa9bd04a to your computer and use it in GitHub Desktop.
Metrologia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace Metrologia | |
{ | |
internal static class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var filename = args[0]; | |
var code = File.ReadAllText(filename); | |
var operators = new List<string>() | |
{ | |
"+", | |
"-", | |
"*", | |
"/", | |
"%", | |
"++", | |
"--", | |
">", | |
"<", | |
"<=", | |
">=", | |
"==", | |
"!=", | |
"&", | |
"|", | |
"^", | |
"~", | |
"<<", | |
">>", | |
">>>", | |
"===", | |
"!==", | |
"=", | |
"+=", | |
"-=", | |
"*=", | |
"/=", | |
"instanceof", | |
"in", | |
"function", | |
"delete", | |
"new", | |
"this", | |
"typeof", | |
"void", | |
"eval", | |
".", | |
"[]", | |
",", | |
"&&", | |
"||", | |
"!", | |
"while", | |
"do", | |
">>=", | |
"<<=", | |
">>>=", | |
"&=", | |
"|=" | |
}; | |
var dictionary = new Dictionary<string, int>(); | |
foreach (var i in operators) | |
{ | |
var reg = $"\\s+{Regex.Escape(i)}\\s+"; | |
var exp = new Regex(reg, RegexOptions.Compiled); | |
var count = exp.Matches(code).Count; | |
dictionary.Add(i, count); | |
} | |
dictionary.Add("?:", Regex.Matches(code, @"\w+\s+\?\s+\w+\s+\:\s+\w+").Count); | |
PrintTableOfOperators(dictionary); | |
} | |
public static void PrintTableOfOperators(Dictionary<string, int> arraydic) | |
{ | |
int all = 0, unique = 0; | |
const string title = " j | Оператор | F1j "; | |
Console.WriteLine(title); | |
Console.WriteLine(new string('-', title.Length)); | |
foreach (var it in arraydic.Where(x => x.Value > 0)) | |
{ | |
var op = it.Key; | |
var val = it.Value; | |
switch (it.Key) | |
{ | |
case "do": op = "do {} while ()"; | |
break; | |
case "while": | |
val -= arraydic["do"]; | |
break; | |
} | |
all += it.Value; | |
Console.WriteLine("{0} | {1} | {2}", (++unique).ToString().PadLeft(3), op.PadRight(15), val.ToString().PadLeft(5)); | |
} | |
Console.WriteLine("\n Общее число операторов, N₁: {0}", all); | |
Console.WriteLine("Число уникальных операторов, η₁: {0}", unique); | |
} | |
public static void PrintTableOfOperands(Dictionary<string, int> arraydic) | |
{ | |
int N1 = 0, number = 1; | |
Console.WriteLine("j\tОперанд\tF2j "); | |
foreach (var it in arraydic) | |
{ | |
Console.WriteLine("{0}\t{1}\t\t{2}", number++.ToString(), it.Key, it.Value); | |
N1 += it.Value; | |
} | |
Console.WriteLine("Общее число операндов в программе N2 = {0}", N1); | |
Console.WriteLine("Число уникальных операндов = {0}\n", --number); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment