3. Предефиниране на оператори
public struct Complex {
public int real;
public int imaginary;
public Complex(int real, int imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2) {
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public override string ToString() {
return(String.Format("{0} + {1}i", real, imaginary));
}
}
4. Предефиниране на оператори
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);
Complex sum = num1 + num2;
Console.WriteLine("First complex number: {0}",num1);
Console.WriteLine("Second complex number: {0}",num2);
Console.WriteLine("The sum of the two numbers: {0}",sum);
6. Намиране на минимален елемент от
масив
int[] arr = {6,3,7,5,1,6,10};
int jMin = 0;
for (int j=1;j<arr.Length;++j) {
if (arr[j]<arr[jMin]) {
jMin = j;
}
}
Console.WriteLine(“{0} na pozicia {1}”, arr[jMin], jMin);
7. Сортиране – selection sort
int[] arr = {6,3,7,5,1,6,10};
for (int i=0;i<arr.Length-1;++i) {
int jMin = i;
for (int j=i+1;j<arr.Length;++j) {
if (arr[j]<arr[jMin]) {
jMin = j;
}
}
int t = arr[i];
arr[i] = arr[jMin];
arr[jMin] = t;
}
foreach (int a in arr) Console.WriteLine(a);