Advertisement
VictoriaLodochkina

lab 4 sharp

Sep 29th, 2021
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2.  
  3. namespace lab_4
  4. {
  5.     class Safearray
  6. {
  7.     readonly int LIMIT = 12;
  8.     protected int[] Arr;
  9.     public int getLimit()
  10.     {
  11.         return LIMIT;
  12.     }
  13.     public Safearray()
  14. {
  15.     Arr = new int[LIMIT];
  16. }
  17. public int this[int n]
  18. {
  19.     get
  20.         {
  21.             if ((n < 0) || (n > LIMIT))
  22.             {
  23.                 Console.Write("\nОшибочный индекс!");
  24.                 return 0;
  25.             }
  26.             else
  27.             {
  28.                 return Arr[n];
  29.             }
  30.         }  
  31.     set
  32.     {
  33.         if ((n < 0) || (n > LIMIT))
  34.         {
  35.             Console.Write("\nОшибочный индекс!");
  36.         }
  37.         else
  38.         {
  39.             Arr[n] = value;
  40.         }
  41. }}
  42. class Safehilo : Safearray {
  43.     private int low;
  44.     private int high;
  45.     public Safehilo(int low, int high):base() {
  46.         this.low=low;
  47.         this.high=high;
  48.         Arr=new int[high-low];
  49.     }
  50.     public int this[int n]
  51. {
  52.     get
  53.         {
  54.             if ((n < low) || (n >= high))
  55.             {
  56.                 Console.Write("\nОшибочный индекс!");
  57.                 return 0;
  58.             }
  59.             else
  60.             {
  61.                 return Arr[n-low];
  62.             }
  63.         }  
  64.     set
  65.     {
  66.         if ((n < low) || (n >= high))
  67.         {
  68.             Console.Write("\nОшибочный индекс!");
  69.         }
  70.         else
  71.         {
  72.             Arr[n-low] = value;
  73.         }
  74. }
  75. }}
  76.     class Program
  77.     {
  78.         static void Main(string[] args)
  79.         {
  80.             //Console.WriteLine("Hello World!");
  81.             string low, high;
  82.             Console.WriteLine("Enter low: ");
  83.             low=Console.ReadLine();
  84.             Console.WriteLine("Enter high: ");
  85.             high=Console.ReadLine();
  86.             int l=Convert.ToInt32(low);
  87.             int h=Convert.ToInt32(high);
  88.             Safehilo mas=new Safehilo(l, h);
  89.             for (int i=0; i<(h-l); i++){
  90.                     mas[l+i]=i;
  91.                     Console.WriteLine("mas[{0}]: {1}", l+i, mas[l+i]);
  92.             }
  93.             Console.WriteLine(mas[l+3]);
  94.         }
  95.     }
  96. }}
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement