Advertisement
stanevplamen

02.09.01.04.ConsoleJustification

Jun 19th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class ConsoleJustification
  6. {
  7.     static string WordsExtract(string[] theText)
  8.     {
  9.         StringBuilder sb = new StringBuilder();
  10.         for (int i = 0; i < theText.Length; i++)
  11.         {
  12.             string[] wordsArray = theText[i].Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.             foreach (string word in wordsArray)
  15.             {
  16.                 sb.Append(word.Trim());
  17.                 sb.Append(' ');
  18.             }
  19.         }
  20.         string result = sb.ToString();
  21.         return result;
  22.     }
  23.  
  24.     static List<string> JustifyText(string allWords, int stringLength)
  25.     {
  26.         //string[] wordsArray = allWords.Split();
  27.         string[] wordsArray = allWords.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  28.         List<string> tempWords = new List<string>();
  29.         List<string> finalWords = new List<string>();
  30.         StringBuilder sb = new StringBuilder();
  31.  
  32.         int counterLetters = 0;
  33.         int counterWords = 0;
  34.         int localCount = 0;
  35.         bool sign = false;
  36.  
  37.         for (int i = 0; i < wordsArray.Length; i++)
  38.         {
  39.             counterWords ++;
  40.             tempWords.Add(wordsArray[i]);
  41.             foreach (char letter in wordsArray[i])
  42.             {
  43.                 counterLetters++;
  44.  
  45.                 if (counterWords - 1 + counterLetters > stringLength)
  46.                 {
  47.                     sign = true;
  48.                     tempWords.RemoveAt(tempWords.Count - 1);
  49.                     //tempWords.Remove(wordsArray[i]);
  50.  
  51.                     for (int w = 0; w < tempWords.Count; w++)
  52.                     {
  53.                         foreach (char ch in tempWords[w])
  54.                         {
  55.                             localCount++;
  56.                         }
  57.                     }
  58.                     break;
  59.                 }
  60.             }
  61.             if (sign == true)
  62.             {
  63.                 int currentWords = counterWords - 1;
  64.                 int currentSpaces = counterWords - 1 - 1;
  65.                 int difference = 0;
  66.                 int remainder = 0;
  67.                 if (currentSpaces > 0)
  68.                 {
  69.                     difference = (stringLength - (localCount + currentSpaces)) / currentSpaces;
  70.                     remainder = (stringLength - (localCount + currentSpaces)) % currentSpaces;
  71.                 }
  72.                 else
  73.                 {
  74.                     difference = (stringLength - (localCount + currentSpaces)) / currentWords;
  75.                     remainder = (stringLength - (localCount + currentSpaces)) % currentWords;
  76.                 }
  77.  
  78.  
  79.                 for (int w = 0; w < tempWords.Count; w++)
  80.                 {
  81.                     if (w < tempWords.Count - 1)
  82.                     {
  83.                         sb.Append(tempWords[w]);
  84.                         sb.Append(' ');
  85.                         if (difference > 0)
  86.                         {
  87.                             for (int d = 1; d <= difference; d++)
  88.                             {
  89.                                 sb.Append(' ');
  90.                             }
  91.                         }
  92.                         if (remainder > 0)
  93.                         {
  94.                             sb.Append(' ');
  95.                             remainder--;
  96.                         }
  97.                     }
  98.                     else if (w == tempWords.Count - 1)
  99.                     {
  100.                         sb.Append(tempWords[w]);
  101.                     }
  102.  
  103.  
  104.                 }
  105.                 i--;
  106.                 finalWords.Add(sb.ToString());
  107.                 counterLetters = 0;
  108.                 counterWords = 0;
  109.                 localCount = 0;
  110.                 tempWords = new List<string>();
  111.                 sb = new StringBuilder();
  112.                 sign = false;          
  113.             }          
  114.         }
  115.  
  116.         if (sign == false)
  117.         {
  118.             int currentWords = counterWords ;
  119.             int currentSpaces = counterWords - 1;
  120.             for (int w = 0; w < tempWords.Count; w++)
  121.             {
  122.                 foreach (char ch in tempWords[w])
  123.                 {
  124.                     localCount++;
  125.                 }
  126.             }
  127.  
  128.             int difference = 0;
  129.             int remainder = 0;
  130.             if (currentSpaces > 0)
  131.             {
  132.                 difference = (stringLength - (localCount + currentSpaces)) / currentSpaces;
  133.                 remainder = (stringLength - (localCount + currentSpaces)) % currentSpaces;
  134.             }
  135.             else
  136.             {
  137.                 difference = (stringLength - (localCount + currentSpaces)) / currentWords;
  138.                 remainder = (stringLength - (localCount + currentSpaces)) % currentWords;
  139.             }
  140.  
  141.             if (currentWords == 1)
  142.             {
  143.                 finalWords.Add(tempWords[0]);
  144.             }
  145.             else
  146.             {
  147.                 for (int w = 0; w < tempWords.Count; w++)
  148.                 {
  149.                     if (w < tempWords.Count - 1)
  150.                     {
  151.                         sb.Append(tempWords[w]);
  152.                         sb.Append(' ');
  153.                         if (difference > 0)
  154.                         {
  155.                             for (int d = 1; d <= difference; d++)
  156.                             {
  157.                                 sb.Append(' ');
  158.                             }
  159.                         }
  160.                         if (remainder > 0)
  161.                         {
  162.                             sb.Append(' ');
  163.                             remainder--;
  164.                         }
  165.                     }
  166.                     else if (w == tempWords.Count - 1)
  167.                     {
  168.                         sb.Append(tempWords[w]);
  169.                     }
  170.                 }
  171.             }
  172.             finalWords.Add(sb.ToString());
  173.         }
  174.  
  175.         return finalWords;
  176.  
  177.     }
  178.  
  179.     static void Main()
  180.     {
  181.         int lines = int.Parse(Console.ReadLine()); //10;
  182.         int stringLength = int.Parse(Console.ReadLine()); //18;
  183.         string[] theText = new string[lines];
  184.  
  185.         for (int i = 0; i < theText.Length; i++)
  186.         {
  187.             theText[i] = Console.ReadLine();
  188.         }
  189.  
  190.         //string[] theText = new string[lines];
  191.         //theText[0] = "can Joro needs";
  192.         //theText[1] = "that functionality";
  193.         //theText[2] = "and";
  194.         //theText[3] = "he";
  195.         //theText[4] = "needs";
  196.         //theText[5] = "it";
  197.         //theText[6] = "fast";
  198.  
  199.  
  200.  
  201.         // първо разделяме и обединяваме всички думи в един стринг
  202.         string allWords = WordsExtract(theText);
  203.  
  204.         // форматиране на изxода в justify
  205.         List<string> formatedSents  = JustifyText(allWords, stringLength);
  206.  
  207.         foreach (var item in formatedSents)
  208.         {
  209.             Console.WriteLine(item);
  210.         }
  211.  
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement