Advertisement
duehoa1211

Exercise

Apr 9th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //CHU' Y' !!!!
  6. namespace Mang1Chieu //mo^~i sinh vien viet code hoan chinh bai` nay` roi nop vao` dia chi email votandung@yahoo.com
  7. {
  8.     class Mang
  9.     {
  10.         private int[] a;
  11.         private int n;
  12.  
  13.         public Mang()
  14.         {
  15.             n = 0;
  16.             a = new int[n];
  17.         }
  18.  
  19.         public Mang(int nn)
  20.         {
  21.             Random rd = new Random();
  22.             n = nn;
  23.             a = new int[n];
  24.             for (int i = 0; i < n; i++)
  25.             {
  26.                 a[i] = rd.Next(-10,10);
  27.             }
  28.         }
  29.  
  30.         public Mang(Mang arr)
  31.         {
  32.             n = arr.n;
  33.             a = new int[n];
  34.             for (int i = 0; i < n; i++)
  35.             {
  36.                 a[i] = arr.a[i];
  37.             }
  38.         }
  39.  
  40.         public Mang(int []aa, int nn)
  41.         {
  42.             n = nn;
  43.             a = new int[n];
  44.             for (int i = 0; i < n; i++)
  45.             {
  46.                 a[i] = aa[i];
  47.             }
  48.         }
  49.  
  50.         public override string ToString()
  51.         {
  52.             string s="[";
  53.             for (int i = 0; i < n-1; i++)
  54.             {
  55.                 s = s + a[i] + " ";
  56.             }
  57.             s = s + "]";
  58.             return s;
  59.         }
  60.         public int N
  61.         {
  62.             get { return this.n; }
  63.             set { if (value > 0) this.n = value; }
  64.         }
  65.         public int[] A
  66.         {
  67.             get { return this.a; }
  68.             set { this.a = value; }
  69.         }
  70.         public Mang Cong(Mang arr)
  71.         {
  72.             Mang tam=new Mang();
  73.             int n;
  74.             if (this.N > arr.N)
  75.                 n = arr.n;
  76.             else
  77.                 n = this.n;
  78.             tam.N = n;
  79.             for (int i = 0; i < n; i++)
  80.                 tam.a[i] = this.a[i] + arr.a[i];
  81.             return tam; //phai sua lai cho dung
  82.         }
  83.  
  84.         public Mang Cong(Mang arr1, Mang arr2)
  85.         {
  86.             Mang tam = new Mang();
  87.             if (arr1.N > arr2.N)
  88.                 tam.N = arr2.N = arr1.N;
  89.             else
  90.                 tam.N = arr1.N = arr2.N;
  91.             for (int i = 0; i < tam.N; i++)
  92.                 tam.a[i] = arr1.A[i] + arr2.A[i];
  93.             return tam; //phai sua lai cho dung
  94.         }
  95.  
  96.         public int TimViTriMax()
  97.         {
  98.             int pos=0;
  99.             for (int i = 0; i < this.n; i++)
  100.                 if (this.a[pos] < this.a[i])
  101.                     pos = i;
  102.             return pos;
  103.         }
  104.  
  105.         public int TimViTriMax(Mang arr)
  106.         {
  107.             int pos = 0;
  108.             for (int i = 0; i < arr.N; i++)
  109.                 if (arr.A[pos] < arr.A[i])
  110.                     pos = i;
  111.             return pos;
  112.         }
  113.  
  114.         public int TongMang()
  115.         {
  116.             int sum=0;
  117.             for (int i = 0; i < this.n; i++)
  118.                 sum += this.a[i];
  119.             return sum;
  120.         }
  121.  
  122.         public int TimViTriMin()
  123.         {
  124.             int pos = 0;
  125.             for (int i = 0; i < this.n; i++)
  126.                 if (this.a[pos] > this.a[i])
  127.                     pos = i;
  128.             return pos;
  129.         }
  130.  
  131.         public int TimViTriMin(Mang arr)
  132.         {
  133.             int pos = 0;
  134.             for (int i = 0; i < arr.N; i++)
  135.                 if (arr.A[pos] > arr.A[i])
  136.                     pos = i;
  137.             return pos;
  138.         }
  139.  
  140.         public int TongMang(Mang arr)
  141.         {
  142.             int sum = 0;
  143.             for (int i = 0; i < arr.N; i++)
  144.                 sum += arr.A[i];
  145.             return sum;
  146.         }
  147.     }
  148.  
  149.     class Program
  150.     {
  151.         static void Main(string[] args)
  152.         {
  153.             Mang a, b, c;
  154.             a = new Mang();
  155.             b = new Mang(4);
  156.             int[] p = { 1, 2, 3, 4, 5 };
  157.             c = new Mang(p, 5);
  158.             Console.WriteLine(b.Cong(c));
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement