} // 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(IQueueInterface <String> words, int columnLength, String outputFilename) { StreamWriter sw = null; int col = 1; int spacesRemaining = 0; try { sw = new StreamWriter(outputFilename); } catch (FileNotFoundException) { Console.WriteLine("Cannot create or open " + outputFilename + " for writing. Using standard output instead."); sw = new StreamWriter(Console.OpenStandardOutput()); } // 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) { sw.Write(str); col += len; words.Pop(); } else if ((col + len) >= columnLength) { // go to the next line sw.WriteLine(); spacesRemaining += (columnLength - col) + 1; col = 1; } else { // Typical case of printing the next word on the same line sw.Write(" "); sw.Write(str); col += (len + 1); words.Pop(); } } sw.WriteLine(); sw.Flush(); sw.Close(); return(spacesRemaining); Console.ReadKey(); } // end wrapSimply
} // End main() /*----------------------------------------------------------------------- * Greedy Algorithm (Non-optimal i.e. approximate or heuristic solution) * -----------------------------------------------------------------------*/ private static int WrapSimply(IQueueInterface <String> Words, int ColumnLength, String OutputFilename) { StreamWriter Outp; Outp = null; try { Outp = new StreamWriter(OutputFilename); } catch (FileNotFoundException) { System.Console.WriteLine("Cannot create or " + "open " + OutputFilename + " for writing. Using standard output instead."); } 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) { Outp.Write(str); Col += Len; Words.Pop(); } else if ((Col + Len) >= ColumnLength) { // go to the next line Outp.WriteLine(); spacesRemaining += (ColumnLength - Col) + 1; Col = 1; } else { // Typical case of printing the next word on the same line Outp.Write(" "); Outp.Write(str); Col += (Len + 1); Words.Pop(); } } Outp.WriteLine(); Outp.Close(); return(spacesRemaining); } // end wrapSimply
} // 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(IQueueInterface <String> words, int columnLength, String outputFilename) { //Writing into file, setting writeFile to null System.IO.TextWriter writeFile = null; //Testing for errors try { writeFile = new StreamWriter(outputFilename); } catch (FileNotFoundException e) { Console.Write("Cannot create or open " + outputFilename + " for writing. Using standard output instead."); } 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) { writeFile.Write(str); col += len; words.Pop(); } else if ((col + len) >= columnLength) { // go to the next line writeFile.Write(Environment.NewLine); spacesRemaining += (columnLength - col) + 1; col = 1; } else { // Typical case of printing the next word on the same line writeFile.Write(" "); writeFile.Write(str); col += (len + 1); words.Pop(); } } writeFile.Write(Environment.NewLine); writeFile.Flush(); writeFile.Close(); return(spacesRemaining); } // end wrapSimply
private static int WrapSimply(IQueueInterface <string> words, int columnLength, string outputFilename) { StreamWriter writerOut = new StreamWriter(outputFilename); try { writerOut.Flush(); writerOut.Close(); StreamWriter streamWriter = writerOut = new StreamWriter(outputFilename); } catch (FileNotFoundException) { Console.WriteLine("Cannon create or open " + outputFilename + " for writing. Using standard output instead."); } int col = 1; int spacesRemaining = 0; while (!words.IsEmpty()) { String str = words.Peek(); int len = str.Length; //Console.WriteLine(len); if (col == 1) { writerOut.Write(str); col += len; words.Pop(); } else if ((col + len) >= columnLength) { writerOut.WriteLine(); spacesRemaining += (columnLength - col + 1); col = 1; } else { writerOut.Write(" "); writerOut.Write(str); col += (len + 1); words.Pop(); } } writerOut.WriteLine(); writerOut.Flush(); writerOut.Close(); return(spacesRemaining); }
} // END OF MAIN() //=============================================================================== // +++++++++ //=============================================================================== // our Greedy Word-Wrapping Algorithm // it's non-optimal -- i.e., it's an approximate or heuristic solution //------------------------------------------------------------------------------- /// <summary> /// this takes the words in our LinkedQueue, called 'words', /// and word-wraps them to the given width (col-width) /// </summary> /// <param name="words"> /// our LinkedQueue with all the words from our inputFileName /// file, tokenized by whitespace /// </param> /// <param name="columnLength"> /// how wide do we want our text? at what point do we wrap? /// </param> /// <param name="outputFileName"> /// the file that we're writing our word-wrapped results to /// </param> /// <returns></returns> ///------------------------------------------------------------ private static int WrapSimply(IQueueInterface <string> words, int columnLength, string outputFileName) { StreamWriter output; try { // this is the one we'll be using in the big while-loop output = new StreamWriter(outputFileName); } catch (System.IO.FileNotFoundException ex) { Console.WriteLine("can't create or open " + outputFileName + " to write to..... using standard output instead!"); using (output = new StreamWriter(Console.OpenStandardOutput())) { Console.WriteLine("\nhere is your wrapped text:\n"); } throw ex; } //--------------------------------------------------------- // the vertical character location/space int col = 1; // running count of spaces left at the ends of the lines int spacesRemaining = 0; // as long as we've still got words in the LinkedQueue, // keep right on popping & wrapping..... while (!words.IsEmpty()) { // assign the next word in the LinkedQueue to 'str' string str = words.Peek(); // get the length of that word int strLength = str.Length; // do we need to wrap to next line? if (col == 1) { output.Write(str); col += strLength; words.Pop(); } // go to the next line..... else if ((col + strLength) >= columnLength) { output.WriteLine(" "); // drop to next line spacesRemaining += (columnLength - col) + 1; col = 1; // reset this! } // the usual case of printing next word on same line: else { output.Write(" "); // put a space between each word output.Write(str); col += (strLength + 1); words.Pop(); } } // end while-loop! //--------------------------------------------------------- output.WriteLine(); output.Flush(); output.Close(); // since we didn't use 'using' for this, we have to // 'flush' and close Streamwriter ourselves return(spacesRemaining); } // end of wrapSimply() function