示例#1
0
	} // End main()
	
	/*-----------------------------------------------------------------------
		Greedy Algorithm (Non-optimal i.e. approximate or heuristic solution)
	  -----------------------------------------------------------------------*/
	
	/**
		As an example only, write out the file directly to the output with _simple_ wrapping,
		i.e. adding spaces between words and moving to the next line if a word would go past the
		indicated column number C.  This will fail if any word length exceeds the column limit C,
		but it still goes ahead and just puts one word on that line.
	*/
	private static int wrapSimply( QueueInterface<string> words, int columnLength, string outputFilename )
	{
        
		StreamWriter output;

		try
		{
			 output = new StreamWriter( outputFilename );
		}
		catch( NullReferenceException e )
		{
			Console.WriteLine("Cannot create or open " + outputFilename +
						" for writing.  Using standard output instead." );
			 output = new StreamWriter(Console.OpenStandardOutput());
		}

		int col = 1;
		int spacesRemaining = 0;			// Running count of spaces left at the end of lines
		while( !words.IsEmpty() )
		{
			string str = words.Peek();
			int len = str.Length;
			// See if we need to wrap to the next line
			if( col == 1 )
			{
				output.Write(str);
				col += len;
				words.Pop();
			}
			else if( (col + len) >= columnLength )
			{
				// go to the next line
				output.WriteLine();
				spacesRemaining += ( columnLength - col ) + 1;
				col = 1;
			}
			else
			{	// Typical case of printing the next word on the same line
				output.Write(" ");
				output.Write(str);
				col += (len + 1);
				words.Pop();
			}
			
		}
		output.WriteLine();
		output.Flush();
		output.Close();
		return spacesRemaining;
	} // end wrapSimply
示例#2
0
        // End main()

        /*-----------------------------------------------------------------------
        *   Greedy Algorithm (Non-optimal i.e. approximate or heuristic solution)
        *  -----------------------------------------------------------------------*/

        /**
         *  As an example only, write out the file directly to the output with _simple_ wrapping,
         *  i.e. adding spaces between words and moving to the next line if a word would go past the
         *  indicated column number C.  This will fail if any word length exceeds the column limit C,
         *  but it still goes ahead and just puts one word on that line.
         */

        private static int wrapSimply(QueueInterface <String> words, int columnLength, String outputFilename)
        {
            StreamWriter Outer = null;

            try
            {
                Outer = new StreamWriter(outputFilename);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("Cannot create or open " + outputFilename +
                                  " for writing.  Using standard output instead.");
                //Outer = new StringReader(Console);
            }

            int col             = 1;
            int spacesRemaining = 0;            // Running count of spaces left at the end of lines

            while (!words.isEmpty())
            {
                String str = words.peek();
                int    len = str.Length;
                // See if we need to wrap to the next line
                if (col == 1)
                {
                    Outer.Write(str);
                    col += len;
                    words.pop();
                }
                else if ((col + len) >= columnLength)
                {
                    // go to the next line
                    Outer.WriteLine();
                    spacesRemaining += (columnLength - col) + 1;
                    col              = 1;
                }
                else
                {   // Typical case of printing the next word on the same line
                    Outer.Write(" ");
                    Outer.Write(str);
                    col += (len + 1);
                    words.pop();
                }
            }
            Outer.WriteLine();
            Outer.Flush();
            Outer.Close();
            return(spacesRemaining);
        }