Ejemplo n.º 1
0
        static LinkedList <String> GenerateBinaryRepresentationList(int n)
        {
            LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>();

            LinkedList <String> output = new System.Collections.Generic.LinkedList <String>();

            if (n < 1)
            {
                return(output);
            }

            q.Push(new StringBuilder("1"));

            while (n-- > 0)
            {
                StringBuilder sb = q.Pop();
                //add to output list  "not sure Addafter or Addlast"
                output.AddLast(sb.ToString());

                //copy
                StringBuilder sbc = new StringBuilder(sb.ToString());

                //left
                sb.Append('0');
                q.Push(sb);
                //right
                sbc.Append('1');
                q.Push(sbc);
            }
            return(output);
        }
Ejemplo n.º 2
0
        static LinkedList <string> GenerateBinaryRepresentationList(int n)
        {
            LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>();

            LinkedList <String> output = new LinkedList <String>();

            if (n < 1)
            {
                return(output);
            }

            q.Push(new StringBuilder("1"));

            while (n-- > 0)
            {
                StringBuilder sb = q.Pop();
                output.AddLast(sb.ToString());

                StringBuilder sbc = new StringBuilder(sb.ToString());

                sb.Append('0');
                q.Push(sb);

                sbc.Append('1');
                q.Push(sbc);
            }
            return(output);
        }
Ejemplo n.º 3
0
        static LinkedList <string> GenerateBinaryRepresentationList(int n)
        {
            ///<value>
            ///Create an empty queue of strings with which to perform the traversal
            ///</value>
            LinkedQueue <StringBuilder> q = new LinkedQueue <StringBuilder>();
            ///<value>
            ///A list for returning the binary values
            ///</value>
            LinkedList <String> output = new LinkedList <String>();

            if (n < 1)
            {
                ///<return>
                /// binary representation of negative values is not supported return an empty list
                ///</return>
                return(output);
            }
            /// Enqueue the first binary number.  Use a dynamic string to avoid string concat
            q.Push(new StringBuilder("1"));

            /// BFS
            while (n-- > 0)
            {
                /// print the front of queue
                StringBuilder sb = q.Pop();
                output.AddLast(sb.ToString());

                /// Make a copy
                StringBuilder sbc = new StringBuilder(sb.ToString());
                /// Left child
                sb.Append('0');
                q.Push(sb);
                /// Right child
                sbc.Append('1');
                q.Push(sbc);
            }
            return(output);
        }
Ejemplo n.º 4
0
        static LinkedList <string> generateBinaryRepList(int n)
        {
            ///Creates an empty queue of strings
            LinkedQueue <StringBuilder> x = new LinkedQueue <StringBuilder>();
            ///The list is returning the binary values
            LinkedList <string> output = new LinkedList <string>();

            if (n < 1)
            {
                /// binary values and representation do not support negative values
                /// return an empty list
                return(output);
            }

            ///This will add the first binary number. This uses a dynamic string
            ///to avoid string concat.
            x.Push(new StringBuilder("1"));

            ///BFS
            while (n > 0)
            {
                ///print the first part of the queue
                StringBuilder sb = x.Pop();
                output.AddLast(sb.ToString());

                ///Make a copy
                StringBuilder sbc = new StringBuilder(sb.ToString());

                ///Left Child
                sb.Append("0");
                x.Push(sb);
                ///Right Child
                sbc.Append("1");
                x.Push(sbc);
                n--;
            }
            return(output);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter wrap width: ");
            string C = Console.ReadLine();

            string directory = Directory.GetCurrentDirectory();

            Console.WriteLine("Please enter the input file name: ");
            Console.WriteLine("Automatically uses current directory of exe for path");
            string inputFilename = Console.ReadLine();

            inputFilename = Path.Combine(directory, inputFilename);

            Console.WriteLine("Automatically uses current directory of exe for path");
            Console.WriteLine("Please enter the output file name: ");
            string outputFilename = Console.ReadLine();

            outputFilename = Path.Combine(directory, outputFilename);

            //Console.WriteLine(C);

            //string inputFilename = Path.Combine(Directory.GetCurrentDirectory(), "WarOfTheWorlds.txt");
            //Console.WriteLine(inputFilename);
            //string outputFilename = Path.Combine(Directory.GetCurrentDirectory(), "output.txt");
            //Console.WriteLine(outputFilename);
            int C2 = int.Parse(C);

            args    = new string[3];
            args[0] = C;
            args[1] = inputFilename;
            args[2] = outputFilename;

            LinkedQueue <string> words = new LinkedQueue <string>();

            if (args.Length != 3)
            {
                PrintUsage();
                Environment.Exit(1);
            }
            try
            {
                using (StreamReader reads = new StreamReader(inputFilename))
                {
                    string book = reads.ReadToEnd();
                    foreach (string word in book.Split(' '))
                    {
                        words.Push(word);
                    }
                    reads.Close();
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Could not find the input file.");
                PrintUsage();
                Environment.Exit(1);
            }
            catch (Exception)
            {
                Console.WriteLine("Something is wrong with the input.");
                PrintUsage();
                Environment.Exit(1);
            }

            int spacesRemaining = WrapSimply(words, C2, outputFilename);

            Console.WriteLine("Total spaces remaining (Greedy): " + spacesRemaining);
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }