You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
12 KiB
334 lines
12 KiB
6 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text;
|
||
|
using static System.Console;
|
||
|
|
||
|
|
||
|
namespace test_1st_chapter_
|
||
|
{
|
||
|
|
||
|
// КОНСТРУКТОР
|
||
|
public class konstruktor
|
||
|
{
|
||
|
string str = string.Empty;
|
||
|
int chislo = 0;
|
||
|
List<char> bykvi = new List<char>();
|
||
|
public konstruktor (string text) // САМ КОНСТРУКТОР
|
||
|
{
|
||
|
str = text;
|
||
|
chislo = text.Length;
|
||
|
|
||
|
for (int i = 0; i < text.Length; i++)
|
||
|
bykvi.Add(str[i]);
|
||
|
bykvi.Sort();
|
||
|
}
|
||
|
public int BykviChislo() => str.Length; // МЕТОД КОНСТРУКТОРА (с лямбдой выражением - заменяет return и скобки)
|
||
|
|
||
|
public string Bykvi() // МЕТОД КОНСТРУКТОРА
|
||
|
{
|
||
|
string strn = string.Empty;
|
||
|
for(int i = 0; i < bykvi.Count; i++)
|
||
|
strn += bykvi[i] + " ";
|
||
|
return strn;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// СТРУКТУРА (может содержать конструкторы, методы и тд)
|
||
|
public struct Struktura
|
||
|
{
|
||
|
public string Name;
|
||
|
public int Number;
|
||
|
public Struktura (string n, int nu)
|
||
|
{
|
||
|
Name = n;
|
||
|
Number = nu;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ССЫЛОЧНЫЙ ТИП
|
||
|
public class Znach
|
||
|
{
|
||
|
public int x, y;
|
||
|
}
|
||
|
|
||
|
class Program
|
||
|
{
|
||
|
static void Main(string[] args)
|
||
|
{
|
||
|
konstruktor Fraza1 = new konstruktor("fraza");
|
||
|
konstruktor Fraza2 = new konstruktor("Eche chto-to");
|
||
|
|
||
|
WriteLine("Использование конструктора\n{0} {1}", Fraza1.BykviChislo(), Fraza2.Bykvi());
|
||
|
|
||
|
Struktura str1;
|
||
|
str1.Name = "str1";
|
||
|
str1.Number = 1;
|
||
|
|
||
|
Struktura str2 = new Struktura("str2", 2);
|
||
|
|
||
|
WriteLine("\n\nИспользование структуры\n{0} {1} \n{2} {3} \n\n", str1.Number, str1.Name, str2.Number, str2.Name);
|
||
|
|
||
|
Znach z1 = new Znach();
|
||
|
z1.x = 10;
|
||
|
Znach z2 = z1;
|
||
|
WriteLine("Ссылочные типы \n{0} {1}\t{2} {3}", z1.x, z1.y, z2.x, z2.y);
|
||
|
z1.x = 15;
|
||
|
WriteLine("{0} {1}\t{2} {3}", z1.x, z1.y, z2.x, z2.y);
|
||
|
z2.y = 20;
|
||
|
WriteLine("{0} {1}\t{2} {3}", z1.x, z1.y, z2.x, z2.y);
|
||
|
Znach z3 = new Znach();
|
||
|
z3.x = 10; z3.y = 1;
|
||
|
WriteLine("{0} {1}\t{2} {3}",z3.x, z3.y, z1.x, z1.y);
|
||
|
|
||
|
|
||
|
// ЯВНОЕ И НЕЯВНОЕ ПРЕОБРАЗОВАНИЕ
|
||
|
const string Format = "int {0} во float {1} и обратно в int {2} или конверт {3}";
|
||
|
int i1 = int.MaxValue;
|
||
|
float f1 = i1;
|
||
|
int i2 = (int)f1;
|
||
|
bool exept = false;
|
||
|
WriteLine("\n\nЯвное и неявное преобразование");
|
||
|
try
|
||
|
{
|
||
|
int i21;
|
||
|
unchecked { i21 = Convert.ToInt32(f1); }
|
||
|
exept = true;
|
||
|
WriteLine(Format, i1, f1, i2, i21);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
}
|
||
|
if (exept == false) { WriteLine("int {0} во float {1} и обратно в int {2}", i1, f1, i2); }
|
||
|
int i3 = 2000000111;
|
||
|
float f2 = i3;
|
||
|
int i4 = (int)f2;
|
||
|
int i41 = Convert.ToInt32(f2);
|
||
|
WriteLine(Format, i3, f2, i4, i41);
|
||
|
|
||
|
float fl = float.MaxValue;
|
||
|
decimal de = decimal.MaxValue;
|
||
|
WriteLine("\n\nfloat: {0}\ndecimal: {1}\n\n", fl, de);
|
||
|
|
||
|
|
||
|
|
||
|
WriteLine(UseUmbrella(false, true, true) + " Зонт если нет ветра, есть солнце и дождь? \n\n");
|
||
|
|
||
|
WriteLine(Sravnenie(10, 15) + " 10 и 15");
|
||
|
WriteLine(Sravnenie(100, 20) + " 100 и 20");
|
||
|
|
||
|
// ДОСЛОВНАЯ (@) И ИНТЕРПОЛИРОВАННАЯ ($) СТРОКИ
|
||
|
int qwer = 10;
|
||
|
WriteLine("\n\nОдин слэш - \\ \nновая строка, любая переменная {0}", qwer);
|
||
|
WriteLine($"Один слэш - \\ \nновая строка, любая переменная {qwer}");
|
||
|
WriteLine(@"один слэш \
|
||
|
новая строка, любая переменная " + qwer);
|
||
|
WriteLine($@"один слэш \
|
||
|
новая строка, любая переменная {qwer}" + "\n\n");
|
||
|
|
||
|
// МАССИВЫ И ИХ ЗАПОЛНЕНИЕ
|
||
|
Struktura[] MassivStruktur = new Struktura[5]; //МАССИВ СТРУКТУР
|
||
|
WriteLine(MassivStruktur[1].Number);
|
||
|
|
||
|
Znach[] MassivSsilok = new Znach[5]; //МАССИВ КЛАССОВ
|
||
|
for (int i = 0; i < MassivSsilok.Length; i++)
|
||
|
MassivSsilok[i] = new Znach();
|
||
|
WriteLine(MassivSsilok[1].x + "\n\n");
|
||
|
|
||
|
int[,] Pryamoyg = new int[3, 3]; //ПРЯМОУГОЛЬНЫЙ МАССИВ
|
||
|
int[,] Pryamoyg2 = new int[,] { { 0, 1, 2, 10 }, { 3, 4, 5, 20 }, { 6, 7, 8, 90 } };
|
||
|
for(int i = 0; i < Pryamoyg2.Rank + 1; i++)
|
||
|
{
|
||
|
for(int j = 0; j < 4; j++)
|
||
|
Write(Pryamoyg2[i,j] + " ");
|
||
|
|
||
|
WriteLine();
|
||
|
}
|
||
|
WriteLine("\n\n");
|
||
|
|
||
|
int[][] Zybchati = new int[3][]; //ЗУБЧАТЫЙ МАССИВ
|
||
|
for(int i = 0; i < Zybchati.Length; i++)
|
||
|
{
|
||
|
Zybchati[i] = new int[i + 3];
|
||
|
for (int j = 0; j < Zybchati[i].Length; j++)
|
||
|
{
|
||
|
Zybchati[i][j] = i * 3 + j;
|
||
|
Write(Zybchati[i][j] + " ");
|
||
|
}
|
||
|
WriteLine();
|
||
|
}
|
||
|
Otstyp();
|
||
|
|
||
|
|
||
|
int[][] Zybchatii = new int[][]
|
||
|
{
|
||
|
new int[] { 1, 2, 3 },
|
||
|
new int[] { 4, 5, 6 },
|
||
|
new int[] { 6, 7, 8, 9 }
|
||
|
};
|
||
|
|
||
|
for (int i = 0; i < Zybchatii.Length; i++)
|
||
|
{
|
||
|
for (int j = 0; j < Zybchatii[i].Length; j++)
|
||
|
Write(Zybchatii[i][j] + " ");
|
||
|
WriteLine();
|
||
|
}
|
||
|
WriteLine("\n");
|
||
|
//НЕЯВНАЯ ИНИЦИАЛИЗАЦИЯ МАССИВА
|
||
|
var x = new[] { "a", "10", "phrase" };
|
||
|
WriteLine(x.GetType() + "\n\n\n");
|
||
|
|
||
|
|
||
|
//ИСПОЛЬЗОВАНИЕ REF И OUT
|
||
|
int fooX = 8;
|
||
|
Write("Переменная {0} метод ", fooX);
|
||
|
FooDef(fooX);
|
||
|
WriteLine($"\nПеременная после метода: {fooX}");
|
||
|
|
||
|
Write("Переменная {0} метод ", fooX);
|
||
|
FooRef(ref fooX);
|
||
|
WriteLine($"\nПеременная после метода: {fooX}");
|
||
|
|
||
|
string imya, familiya;
|
||
|
FooOut("Valentin Suntsev", out imya, out familiya);
|
||
|
WriteLine("\n{0} {1}\n\n", familiya, imya);
|
||
|
|
||
|
|
||
|
//ИСПОЛЬЗОВАНИЕ PARAMS
|
||
|
int sum = Summa(12, 32, 14, 34, 12, 4);
|
||
|
WriteLine("Использование params {0}", sum);
|
||
|
Otstyp();
|
||
|
|
||
|
//ИСПОЛЬЗОВАНИЕ НЕОБЯЗАТЕЛЬНЫХ ПАРАМЕТРОВ
|
||
|
Neobyaz();
|
||
|
Neobyaz(5);
|
||
|
Neobyaz(5, 10);
|
||
|
Neobyaz(y: 10, x: 5);
|
||
|
Neobyaz(y: 10);
|
||
|
Otstyp();
|
||
|
|
||
|
//ОПЕРАЦИИ С NULL
|
||
|
string s1 = null, s2 = "str", s3 = "str2";
|
||
|
string s10 = s1 ?? s2; //проверка на null, если левое значение НЕ null, то оно присваивается, если же нет, то присвоение второго
|
||
|
string s11 = s3 ?? s2;
|
||
|
string s12 = s1 ?? s1;
|
||
|
WriteLine("{0} {1} {2}", s10, s11, s12);
|
||
|
|
||
|
StringBuilder sb = null;
|
||
|
string st1 = sb?.ToString(); //проверка на null, если выражение НЕ null, то оно переводится в string, если null, то присваивается null
|
||
|
string st2 = (sb == null ? null : sb.ToString()); //эквивалентное выражение верхнему
|
||
|
|
||
|
Otstyp();
|
||
|
for(int i = 0, prevFib = 1, curFib = 1; i < 10; i++)
|
||
|
{
|
||
|
Write(prevFib + " ");
|
||
|
int newFib = prevFib + curFib;
|
||
|
prevFib = curFib; curFib = newFib;
|
||
|
}
|
||
|
|
||
|
Otstyp();
|
||
|
string stroka = "stroka";
|
||
|
foreach(char c in stroka)
|
||
|
Write(c + " ");
|
||
|
|
||
|
Otstyp();
|
||
|
int switcher = 0;
|
||
|
Loop:
|
||
|
for(; switcher < 6; switcher++)
|
||
|
{
|
||
|
switch (switcher)
|
||
|
{
|
||
|
case 1:
|
||
|
WriteLine("case 1");
|
||
|
continue;
|
||
|
case 2:
|
||
|
WriteLine("case 2");
|
||
|
switcher++;
|
||
|
goto Loop;
|
||
|
case 3:
|
||
|
WriteLine("case 3");
|
||
|
break;
|
||
|
default: WriteLine("lul"); break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
testfortest.fortest fortest = new testfortest.fortest("stroka");
|
||
|
WriteLine("{0}", fortest.dlina());
|
||
|
Otstyp();
|
||
|
|
||
|
|
||
|
float ff1 = 1.0f;
|
||
|
float ff2 = 0.0f;
|
||
|
float ff3 = -0.0f;
|
||
|
float ff4 = -1.0f;
|
||
|
WriteLine("{0} {1} {2} {3}\n{4}", ff1 / ff2, ff1 / ff3, ff4 / ff2, ff4 / ff3, ff2 / ff2);
|
||
|
<<<<<<< HEAD
|
||
|
WriteLine("line for master");
|
||
|
WriteLine("line for master2");
|
||
|
=======
|
||
|
WriteLine("test line fot t1");
|
||
|
WriteLine("test line2 fot t1");
|
||
|
>>>>>>> testline
|
||
|
|
||
|
ReadKey();
|
||
|
}
|
||
|
|
||
|
static bool UseUmbrella(bool wind, bool sun, bool rain) => !wind && (sun || rain); // ПРОСТОЙ БУЛЕВЫЙ МЕТОД
|
||
|
|
||
|
static int Sravnenie(int a, int b) => (a < b) ? a : b; // ТЕРНАРНАЯ ОПЕРАЦИЯ
|
||
|
|
||
|
static void FooDef(int x) //ПРИМЕНЕНИЕ REF И OUT
|
||
|
{
|
||
|
x++;
|
||
|
Write(x);
|
||
|
}
|
||
|
|
||
|
static void FooRef (ref int x)
|
||
|
{
|
||
|
x++;
|
||
|
Write(x);
|
||
|
}
|
||
|
|
||
|
static void FooOut (string name, out string imya, out string familiya)
|
||
|
{
|
||
|
int i = name.LastIndexOf(' ');
|
||
|
imya = name.Substring(0, i);
|
||
|
familiya = name.Substring(i + 1);
|
||
|
}
|
||
|
|
||
|
static int Summa (params int[] ints) //ИСПОЛЬЗОВАНИЕ PARAMS
|
||
|
{
|
||
|
int sum = 0;
|
||
|
for (int i = 0; i < ints.Length; i++)
|
||
|
sum += ints[i];
|
||
|
return sum;
|
||
|
}
|
||
|
|
||
|
static void Neobyaz (int x = 23, int y = 32) //ИСПОЛЬЗОВАНИЕ НЕОБЯЗАТЕЛЬНЫХ ПАРАМЕТРОВ
|
||
|
{
|
||
|
WriteLine("x {0}, y {1}", x, y);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
static void Otstyp()
|
||
|
{
|
||
|
WriteLine("\n\n");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
namespace testfortest
|
||
|
{
|
||
|
public class fortest
|
||
|
{
|
||
|
string text;
|
||
|
int dlin;
|
||
|
public fortest(string str)
|
||
|
{
|
||
|
text = str;
|
||
|
dlin = str.Length;
|
||
|
}
|
||
|
|
||
|
public int dlina() => dlin;
|
||
|
}
|
||
|
}
|