Advertisement
Wolvenspud

task3

Jun 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. Start:
  2.     Console.WriteLine("Enter corresponding number to select shape");
  3. Console.WriteLine("Square: 1 Rectangle: 2 Triangle: 3 Circle: 4");
  4. int n = 0;
  5. while(!int.TryParse(Console.ReadLine(), out n) || n < 1 || n > 4)
  6. {
  7.     Console.WriteLine("Error, enter again");
  8. }
  9. switch(n)
  10. {
  11.     case 1:
  12.     float sh;
  13.     Console.WriteLine("Enter the height");
  14.     while(!float.TryParse(Console.ReadLine(), out sh))
  15.     {
  16.         Console.WriteLine("Error, enter again");
  17.     }
  18.     Console.WriteLine(Square(sh));
  19.     break;
  20.    
  21.     case 2:
  22.     float rh;
  23.     float rw;
  24.     Console.WriteLine("Enter height and width separated by a space");
  25.     string[] s = Console.ReadLine().Split(' ');
  26.     while(s.Length != 2 || !float.TryParse(s[0], out rh) || !float.TryParse(s[2], out rw))
  27.     {
  28.         Console.WriteLine("Error, enter again");
  29.         s = Console.ReadLine().Split(' ');
  30.     }
  31.     Console.WriteLine(Rectangle(float.Parse(s[0]), float.Parse(s[1])));
  32.     break;
  33.  
  34.     case 3:
  35.     float tb;
  36.     float th;
  37.     Console.WriteLine("Enter base and height separated by a space");
  38.     string[] t = Console.ReadLine().Split(' ');
  39.     while(t.Length!= 2 || !float.TryParse(t[0], out tb) || !float.TryParse(t[2], out th))
  40.     {
  41.         Console.WriteLine("Error, enter again");
  42.         t = Console.ReadLine().Split(' ');
  43.     }
  44.     Console.WriteLine(Triangle(tb, th));
  45.     break;
  46.  
  47.     case 4:
  48.     Console.WriteLine("Enter the radius");
  49.     float r;
  50.     while(!float.TryParse(Console.ReadLine(), out r))
  51.     {
  52.         Console.WriteLine("Error, enter again");
  53.     }
  54.     Console.WriteLine(Circle(r));
  55.     break;
  56. }
  57. Console.WriteLine("Press any key to continue...");
  58. Console.ReadKey();
  59. goto Start;
  60.  
  61.  
  62. private float Square(float w)
  63. {
  64.     return w * w;
  65. }
  66.  
  67. private float Rectangle(float w, float h)
  68. {
  69.     return w * h;
  70. }
  71. private float Triangle(float b, float h)
  72. {
  73.     return 0.5f * b * h;
  74. }
  75. private float Circle(float r)
  76. {
  77.     return (float)Math.PI * r * r;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement