JBTEE


# SOME C# EXERCISES
===================

Exercise 1. Calculate n + n , where { n | n (- 0..10 }

  using System;

  class Program {
    public static void Main(string[] args) {
      // https://docs.microsoft.com/en-us/dotnet/api
      //   /system.convert?view=netframework-4.8
      int n = 0;
      try {
          Console.Write("Enter an integer:");
          n = System.Convert.ToInt32(System.Console.ReadLine());
          if (n<0 || n>9) {
            Console.WriteLine("Out of bounds..");
          }
          else {
            Console.WriteLine("{0}+{0} = {1}", n, n+n);
          }
      }
      catch (System.ArgumentNullException) {
          Console.WriteLine("String is null.");
      }
      catch (System.FormatException) {
          Console.WriteLine("String can't be converted to integer");
      } 
      catch (System.OverflowException) {
          Console.WriteLine(
          "Overflow in string to int conversion.");
      }
      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
  }

========================================================================

Exercise 2. Print entered digit as word (choose any language you like).

  using System;

  class Program
  {
    public static void Main(string[] args)
    {
      string[] numbers = {
        "zero","uno","due","tre","quattro",
        "cinque","sei","sette","otto","nove"
      };
      int n = 0;
      try {
          Console.Write("Enter an integer: ");
          n = System.Convert.ToInt32(System.Console.ReadLine());
          if (n<0 || n>9) {
            Console.WriteLine("Out of bounds..");
          }
          else {
            Console.WriteLine("Italiano: " + numbers[n]);
          }
      }
      catch (System.ArgumentNullException) {
          Console.WriteLine("String is null.");
      }
      catch (System.FormatException) {
          Console.WriteLine("String can't be converted to integer");
      } 
      catch (System.OverflowException) {
          Console.WriteLine(
          "Overflow in string to int conversion.");
      }
        
      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
  }

========================================================================

Exercise 3. If entered n is less than 5 print "YES", else print "NO" 

  using System;

  class Program {
    public static void Main(string[] args) {
      // https://docs.microsoft.com/en-us/dotnet/api
      //   /system.convert?view=netframework-4.8
      int n = 0;
      try {
          Console.Write("Enter an integer: ");
          n = System.Convert.ToInt32(System.Console.ReadLine());
          if (n<0 || n>9) {
            Console.WriteLine("Out of bounds..");
          }
          else {
            Console.WriteLine("{0}<5? {1}", n, n<5?"YES":"NO");
          }
      }
      catch (System.ArgumentNullException) {
          Console.WriteLine("String is null.");
      }
      catch (System.FormatException) {
          Console.WriteLine("String can't be converted to integer");
      } 
      catch (System.OverflowException) {
          Console.WriteLine(
          "Overflow in string to int conversion.");
      }
      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
  }

========================================================================

Exercise 4. "The quick brown fox jumps over the lazy dog" 
            How many words?

  using System;

  class Program {
    public static void Main(string[] args)	{

      Console.Write("Enter sentence: ");
      string s = Console.ReadLine();
      
      Console.WriteLine( 
        Convert.ToInt32( 
          s.Split ( new string[] { " " }, 
          StringSplitOptions.RemoveEmptyEntries).Length
        ) + " words detected"
      );
        
      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
  }

========================================================================

Exercise 5. sin(n), angle in degrees 0..90 

  using System;

  class Program
  {
    public static void Main(string[] args)
    {
      int n = 0;
      try {
          Console.Write("Enter an integer: ");
          n = Convert.ToInt32(Console.ReadLine());
          if (n<0 || n>90) {
            Console.WriteLine("Out of bounds..");
          }
          else {
            Console.WriteLine("n = {0} degrees, sin({0}) = {1}",
              n,                            
              Math.Sin(n*Math.PI/180.0));
          }
      }
      catch (System.ArgumentNullException) {
          Console.WriteLine("String is null.");
      }
      catch (System.FormatException) {
          Console.WriteLine("String can't be converted to integer");
      } 
      catch (System.OverflowException) {
          Console.WriteLine(
          "Overflow in string to int conversion.");
      }
        
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
  }

========================================================================

Exercise 6. sqrt(n), where { n | n (- 0..100 }

  using System;

  class Program
  {
    public static void Main(string[] args)
    {
      int n = 0;
      try {
          Console.Write("Enter an integer: ");
          n = System.Convert.ToInt32(System.Console.ReadLine());
          if (n<0 || n>90) {
            Console.WriteLine("Out of bounds..");
          }
          else {
            Console.WriteLine(
              "n = {0}, sqrt({0}) = {1}",
              n,                            
              Math.Sqrt(n)
            );
          }
      }
      catch (System.ArgumentNullException) {
          Console.WriteLine("String is null.");
      }
      catch (System.FormatException) {
          Console.WriteLine("String can't be converted to integer");
      } 
      catch (System.OverflowException) {
          Console.WriteLine(
          "Overflow in string to int conversion.");
      }
        
      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
  }
         
========================================================================
         
Exercise 7. "Hello"

  using System;

  namespace hello {
    class Program {
      public static void Main(string[] args) {
        Console.WriteLine("Hello World!");
        
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
      }
    }
  }

========================================================================

Exercise 8. "Work with file"

using System;

namespace Files
{
  class ReadFromFile {
    static void Main(){
      string[] lines = System.IO.File.ReadAllLines(
        @"C:\Users\opilane\Desktop\20191128\alice_in_wonderland.txt"
      );
  
      System.Console.WriteLine("Contents of file line by line = ");
      
      foreach (string line in lines) {
        if (line.Trim().Length > 0)
          Console.WriteLine(
            line.Trim().PadRight(40).Substring(0, 39)
          );
      }
  
      Console.WriteLine("Press any key to exit.");
      System.Console.ReadKey();
    }
  }
}

========================================================================

Exercise 9. "Filter" (Read until "Kuupäev")

using System;
using System.Text.RegularExpressions;

namespace Files
{
  class ReadFromFile {
    static void Main(){
      string[] lines = System.IO.File.ReadAllLines(
        @"C:\Users\opilane\Desktop\20191128\it-18e.txt"
      );
      string [] outlines;
  
      foreach (string line in lines) {
        if (line.Length > 7)
          if (Equals(line.Substring(0,7), "Kuupäev")) break;
        Console.WriteLine( Regex.Replace(line, @".+,", "" ));
      }
  
      Console.WriteLine("Press any key to exit.");
      System.Console.ReadKey();
    }
  }
}

========================================================================

Exercise 10. "LINQ"

//  https://www.tutorialsteacher.com/linq/what-is-linq

using System;
using System.Linq;

public class Program {
  public static void Main(){
    string[] lines = System.IO.File.ReadAllLines(
      "C:\\Users\\opilane\\Desktop\\K0K\\people.txt"
    );

    // LINQ Query 
    var myLinqQuery = from line in lines
                      where line.Contains(" IT-18V") && 
                            line.Contains(" L")
                      select line;

    // Query execution
    foreach (var line in myLinqQuery) {
      string[] items = line.Split(
        new string[] { " " }, 
        StringSplitOptions.RemoveEmptyEntries
      );
      foreach (var item in items)
        Console.Write(item.Trim().PadRight(20).Substring(0, 19));
      Console.WriteLine("");
    }
    Console.ReadKey();
  }
}

