Advertisement
G_Burlakova

IsoscelesTriangle

Mar 9th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.     class IsoscelesTriangle
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //This program is changed a bit. It allows the user to choose the height of the triangle.
  12.             //For output with exactly 9 symbols - as it is in the task, you can enter 4 for height.
  13.             //The code is devided in three logical parts - first one prints the edge, the second one
  14.             //shows the lines between the edge and the base and the last one displays the base of the triangle.
  15.             try
  16.             {
  17.                 Console.OutputEncoding = Encoding.UTF8; // This code allows correct output for symbols
  18.                 char symbol = '\x00A9';
  19.                 byte triangleHeight;
  20.                 //The following loop guarantees minimal height because it has to be at least 3
  21.                 do
  22.                 {
  23.                     Console.Write("Enter height of the triangle: ");
  24.                     triangleHeight = byte.Parse(Console.ReadLine());
  25.                 } while (triangleHeight <= 2);
  26.  
  27.                 int numberSymbolsBase = triangleHeight; // This is the number of symbols for the base without spaces
  28.  
  29.                 if (triangleHeight * 2 > Console.BufferWidth || triangleHeight > Console.BufferHeight)
  30.                 {
  31.                     Console.SetBufferSize(2 * triangleHeight, triangleHeight + 5);
  32.                     //This code sets the size of the buffer according to the dimensions of the triangle,
  33.                     //but only if needed
  34.                 }
  35.                
  36.                 // Edge of the triangle
  37.                 string spacesBeforeEdge = new string(' ', triangleHeight - 1);
  38.                 string edge = spacesBeforeEdge + symbol;
  39.                 Console.WriteLine(edge);
  40.  
  41.                 // Lines Between the edge and the base of the triangle
  42.                 int spacesBetweenCount = 1; // This variable is used for the speces in the triangle
  43.                 int spacesBeforeCount = triangleHeight - 2; //Spaces before each line of symbols
  44.                 for (int i = 1; i <= triangleHeight - 2; i++)
  45.                 {
  46.                     string spacesBefore = new string(' ', spacesBeforeCount);
  47.                     spacesBeforeCount--;
  48.                     string spacesBetween = new string(' ', spacesBetweenCount);
  49.                     spacesBetweenCount = spacesBetweenCount + 2;
  50.                     string currentLine = spacesBefore + symbol + spacesBetween + symbol;
  51.                     Console.WriteLine(currentLine);
  52.                 }
  53.  
  54.                 // Base of the triangle
  55.                 for (int i = 1; i <= numberSymbolsBase; i++)
  56.                 {
  57.                     Console.Write(symbol + " ");
  58.                 }
  59.                 Console.WriteLine();
  60.             }
  61.             catch (FormatException)
  62.             {
  63.  
  64.                 Console.WriteLine("Wrong format for height."); ;
  65.             }
  66.             catch (OverflowException)
  67.             {
  68.                 Console.WriteLine("You have entered too big or negative number for height.");
  69.             }
  70.            
  71.         }
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement