コード例 #1
0
ファイル: graph.cs プロジェクト: ningit/vaed
    public static string ToText(this xreal x)
    {
        if (x.is_Infty)
        {
            return("∞");
        }

        Dafny.BigRational val = x.dtor_value;

        return(val.ToString().Replace("(", "").Replace(")", "").Replace("/ 1.0", "").Replace(".0 ", ""));
    }
コード例 #2
0
ファイル: graph.cs プロジェクト: ningit/vaed
    public xreal[,] Parse(StreamReader sr)
    {
        string line;

        Dictionary <string, int>         vertices = new Dictionary <string, int>();
        Queue <Tuple <int, int, xreal> > values   = new Queue <Tuple <int, int, xreal> >();

        Regex linefmt = new Regex(@"([^\s]+)\s+([^\s]+)\s+(-?\d+)(\s+-)?");

        while ((line = sr.ReadLine()) != null)
        {
            // Lines starting with colon are comments
            if (line.StartsWith(":"))
            {
                continue;
            }

            var mat = linefmt.Match(line);

            if (!mat.Success)
            {
                throw new FormatException("wrong line format \"" + line + "\"");
            }

            var origin = mat.Groups[1].Value;
            var dest   = mat.Groups[2].Value;

            if (!vertices.ContainsKey(origin))
            {
                vertices[origin] = vertices.Count;
            }

            if (!vertices.ContainsKey(dest))
            {
                vertices[dest] = vertices.Count;
            }

            int val;

            if (!Int32.TryParse(mat.Groups[3].Value, out val))
            {
                throw new FormatException("not a number \"" + val + "\"");
            }

            values.Enqueue(Tuple.Create(vertices[origin], vertices[dest], XFromInt(val)));

            if (mat.Groups[4].Value != "")
            {
                values.Enqueue(Tuple.Create(vertices[dest], vertices[origin], XFromInt(val)));
            }
        }

        xreal[,] ret = new xreal[vertices.Count, vertices.Count];


        // Initializes the matrix as default

        for (int i = 0; i < vertices.Count; i++)
        {
            for (int j = 0; j < vertices.Count; j++)
            {
                if (i == j)
                {
                    ret[i, j] = XFromInt(0);
                }
                else
                {
                    ret[i, j] = Infty();
                }
            }
        }

        // Initializes the matrix with the given values

        foreach (var val in values)
        {
            ret[val.Item1, val.Item2] = val.Item3;
        }

        VertNames = new List <string>(vertices.Keys);

        return(ret);
    }