stoyanov_gs

CSharp - PDTaV - 9 task

May 19th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.09 KB | None | 0 0
  1. using System;
  2.  
  3. class IsoscelesTriangle
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Hello. You are starting a program which will try to make a triangle with given number of symbols. If you are interested, please continue");
  8.         Console.WriteLine("Please enter a number which will represent how many symbols to use to form a triangle");
  9.         Console.WriteLine("P.S. Does not work for numbers 3, 5, 8, 11");
  10.  
  11.         //Defining variables
  12.         int p = Convert.ToInt32(Console.ReadLine()); //This is the number of symbols we will use
  13.         int maxRows = 0; //The maximum of rows that we will use
  14.         int sum = 0;
  15.         //Making a quick check how many rows to use for the triangle
  16.         do
  17.         {
  18.             if ((sum >= p) && (maxRows < p-1 / 2))
  19.             {
  20.                 break;
  21.             }
  22.             maxRows++;
  23.             sum += maxRows;
  24.         }
  25.         while ((sum <= p) && (maxRows < p / 2));
  26.  
  27.         int maxCols = maxRows*2 - 1; //The maximum of cols that we will use
  28.         int[,] charsCoord = new int[maxRows, maxCols]; //Here we make the two-dimensional array which will hold the values and their positions
  29.         bool nextSymbolIsSpace = false; //This will be used to know were we will need a space and where a symbol
  30.         int forFillIn = p - (maxRows + 1 + (maxRows - 2) * 2); //This is the number of symbols we will put into the triangle
  31.         int remainForFillIn = forFillIn; //This is the remaining symbols to fill-in inside the triangle
  32.         int rem = p; //These are the remaining symbols we can use
  33.         int spaceBeforeAfter = 0; //Here we will hold the number of spaces before and after each side on one row
  34.         int remainSpaceBefore = 0; //Here are the remaining spaces before the triangle on one row
  35.         int remainSpaceAfter = 0; //Here are the remaining spaces after the triangle on one row
  36.  
  37.         //Adding values to the array
  38.         for (int r = maxRows; r >= 1; r--)
  39.         {
  40.             for (int c = 1; c <= maxCols; c++)
  41.             {
  42.                 //Checking if there are remaining symbols to use
  43.                 if (rem != 0)
  44.                 {
  45.  
  46.                     //Condition for the rest of the symbols which wont be on the last row
  47.                     if (r != maxRows)
  48.                     {
  49.  
  50.                         //First we have to make the spaces before each row
  51.                         if (remainSpaceBefore != 0)
  52.                         {
  53.                             charsCoord[r - 1, c - 1] = 32;
  54.                             remainSpaceBefore--;
  55.                         }
  56.                         //Here we check if we can already make spaces after the triangle on each row
  57.                         else if (remainSpaceAfter != 0 && c > maxCols - spaceBeforeAfter)
  58.                         {
  59.                             if (c == maxCols)
  60.                             {
  61.                                 charsCoord[r - 1, c - 1] = 32;
  62.                                 spaceBeforeAfter++;
  63.                                 remainSpaceBefore = spaceBeforeAfter;
  64.                                 remainSpaceAfter = spaceBeforeAfter;
  65.                             }
  66.                             else
  67.                             {
  68.                                 charsCoord[r - 1, c - 1] = 32;
  69.                                 remainSpaceAfter--;
  70.                             }
  71.                         }
  72.  
  73.                         //Now we make the symbols on this row
  74.                         else if (remainSpaceBefore == 0 && remainSpaceAfter != 0)
  75.                         {                            
  76.                             //If the remaining symbols for inside the triangle are zero, just add symbol at the start and at the end of the triangle
  77.                             if (remainForFillIn == 0 || (remainForFillIn != 0 && (c == maxCols - spaceBeforeAfter || c == spaceBeforeAfter + 1)))
  78.                             {
  79.                                 if ((c == maxCols - spaceBeforeAfter || c == spaceBeforeAfter + 1) && nextSymbolIsSpace == true)
  80.                                 {
  81.                                     charsCoord[r - 1, c - 1] = 0169;
  82.                                     rem--;
  83.                                     nextSymbolIsSpace = true;
  84.                                 }
  85.                                 else if ((c == maxCols - spaceBeforeAfter || c == spaceBeforeAfter + 1) && nextSymbolIsSpace == false)
  86.                                 {
  87.                                     charsCoord[r - 1, c - 1] = 0169;
  88.                                     rem--;
  89.                                     nextSymbolIsSpace = true;
  90.                                 }
  91.                                 else
  92.                                 {
  93.                                     charsCoord[r - 1, c - 1] = 32;
  94.                                 }
  95.                             }
  96.  
  97.                             //But if there are still remaining symbols, we will try to place them in the triangle
  98.                             else
  99.                             {
  100.                                 //If the remaining symbols can be placed on the next row, they will be placed as followed
  101.                                 if (r <= 2 + forFillIn)
  102.                                 {
  103.                                     if (c == maxCols - spaceBeforeAfter)
  104.                                     {
  105.                                         charsCoord[r - 1, c - 1] = 0169;
  106.                                         rem--;
  107.                                         nextSymbolIsSpace = false;
  108.                                         remainForFillIn--;
  109.                                     }
  110.                                     else if (nextSymbolIsSpace == false)
  111.                                     {
  112.                                         if (remainForFillIn == 1)
  113.                                         {
  114.                                             charsCoord[r - 1, c - 1] = 0169;
  115.                                             rem--;
  116.                                             nextSymbolIsSpace = false;
  117.                                             remainForFillIn--;
  118.                                         }
  119.                                         else
  120.                                         {
  121.                                             charsCoord[r - 1, c - 1] = 0169;
  122.                                             rem--;
  123.                                             nextSymbolIsSpace = true;
  124.                                             remainForFillIn--;
  125.                                         }
  126.                                     }
  127.                                     else if (nextSymbolIsSpace == true)
  128.                                     {
  129.                                         charsCoord[r - 1, c - 1] = 32;
  130.                                         nextSymbolIsSpace = false;
  131.                                     }
  132.                                 }
  133.                                 //But if there are too many to be placed on one row, we will place only one part of them
  134.                                    
  135.                             }                            
  136.                         }
  137.                     }
  138.  
  139.                     //The array will be added with values from the last row to the first one
  140.                     //Because of that we make a special condition only for the last row
  141.                     else if (r == maxRows)
  142.                     {
  143.                         if (c == maxCols)
  144.                         {
  145.                             charsCoord[r - 1, c - 1] = 0169;
  146.                             rem--;
  147.                             nextSymbolIsSpace = false;
  148.                             spaceBeforeAfter++;
  149.                             remainSpaceBefore++;
  150.                             remainSpaceAfter++;
  151.                         }
  152.                         else
  153.                         {
  154.                             if (nextSymbolIsSpace == false)
  155.                             {
  156.                                 charsCoord[r - 1, c - 1] = 0169;
  157.                                 rem--;
  158.                                 nextSymbolIsSpace = true;
  159.                             }
  160.                             else
  161.                             {
  162.                                 charsCoord[r - 1, c - 1] = 32;
  163.                                 nextSymbolIsSpace = false;
  164.                             }
  165.                         }
  166.                     }
  167.                 }
  168.  
  169.                 //If there are left any remaining symbol it will execute only spaces
  170.                 else
  171.                 {
  172.                     charsCoord[r - 1, c - 1] = 32;
  173.                 }
  174.             }
  175.         }
  176.  
  177.         //Executing the result
  178.         for (int r = 1; r <= maxRows; r++)
  179.         {
  180.             for (int c = 1; c <= maxCols; c++)
  181.             {
  182.                 if (c == maxCols)
  183.                 {
  184.                     Console.WriteLine("{0:X}", (char)charsCoord[r - 1, c - 1]);
  185.                 }
  186.                 else
  187.                 {
  188.                     Console.Write("{0:X}", (char)charsCoord[r - 1, c - 1]);
  189.                 }
  190.             }
  191.         }
  192.  
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment