Esempio n. 1
0
        /// <summary>Initializes a graph from an initialized <see cref="TextInput"/> stream
        /// The format is the number of vertices <c>V</c>,
        /// followed by the number of edges <c>E</c>,
        /// followed by <c>E</c> pairs of vertices, with each entry separated by whitespace.</summary>
        /// <param name="input"> in the input stream</param>
        /// <exception cref="IndexOutOfRangeException">if the endpoints of any edge are not in prescribed range</exception>
        /// <exception cref="ArgumentException">if the number of vertices or edges is negative</exception>
        /// <exception cref="FormatException">if the edges in the file are in an invalid input format</exception>
        ///
        public Graph(TextInput input) : this(input.ReadInt())
        {
            int E = input.ReadInt();

            if (E < 0)
            {
                throw new ArgumentException("Number of edges must be nonnegative");
            }
            for (int i = 0; i < E; i++)
            {
                int v = input.ReadInt();
                int w = input.ReadInt();
                AddEdge(v, w);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Demo test for the <c>TextInput</c> data type. The test shows
        /// the methods' behavior and how to use them.
        /// </summary>
        /// <param name="args">Place holder for user arguments</param>
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            Console.Write("Type 3 char, 1 integer, a few strings: ");
            char[] c = { StdIn.ReadChar(), StdIn.ReadChar(), StdIn.ReadChar() };
            int    a = StdIn.ReadInt();
            string s = StdIn.ReadLine();

            Console.WriteLine("3 char: {0}, 1 int: {1}, new line: {2}", new string(c), a, s);

            Console.Write("Type a string: ");
            s = StdIn.ReadString();
            Console.WriteLine("Your string was: " + s);
            Console.WriteLine();

            Console.Write("Type an int: ");
            a = StdIn.ReadInt();
            Console.WriteLine("Your int was: " + a);
            Console.WriteLine();

            Console.Write("Type a bool: ");
            bool b = StdIn.ReadBool();

            Console.WriteLine("Your bool was: " + b);
            Console.WriteLine();

            Console.Write("Type a double: ");
            double d = StdIn.ReadDouble();

            Console.WriteLine("Your double was: " + d);
            Console.WriteLine();

            Console.WriteLine("Enter a line:");
            s = StdIn.ReadLine();
            Console.WriteLine("Your line was: " + s);
            Console.WriteLine();

            Console.Write("Type any thing you like, enter Ctrl-Z to finish: ");
            string[] all = StdIn.ReadAllStrings();
            Console.WriteLine("Your remaining input was:");
            foreach (var str in all)
            {
                Console.Write(str + " ");
            }
            Console.WriteLine();
        }