using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MyAppSTD2018
{
class Program
{
static void Main(string[] args)
{
// Shortcuts Ctrl+. | Ctrl+r,r | cw \t\t | Ctrl+k,d | Ctrl+k,c | Ctrl+k,u
// I. Types have: name (e.g. int), size, default value
// II. Variables have: name (A-Za-z_0-9 all utf-8, can't start with 0-9, can't be a keyword), type, value
// built-in types:
//1. integer (default = 0)
byte myByte = 23; // unsigned 8-bis (0 to 255) default = 0
sbyte mySByte = -128; // signed 8-bit (-128 to 127) default = 0
short myShort = -1000; // signed 16-bit (-32,768 to 32,767)
ushort myUshort = 2000; // unsigned 16-bit (0 to 65,535)
int myVar = 4; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
int myVar2 = 5; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
int sum = myVar + myVar2; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
uint myUint = 12000U; // unsigned 32-bit (0 to 4, 294, 967, 295)
sum = 0xA8F1; // hexadecimal literal
sum = 0XA8F1; // hexadecimal literal
long myLong = 42432L;// signed 64-bit (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
ulong myUlong = 23423423U; // unsigned 64-bit (0 to 18,446,744,073,709,551,615)
ulong maxIntValue = UInt64.MaxValue;
Console.WriteLine(maxIntValue); // 18446744073709551615
// Check if there is overflow and throw an exception
//checked
//{
// int maxInt = int.MaxValue;
// maxInt = maxInt + 1;
// Console.WriteLine(maxInt);
//}
// if there is no check we will get wrong result -2147483648
// BigInteger myBigInt = 3213213; // No limits
// 2. Real (default 0.0 for F/f D/d M/m)
float myFloat = 4.566F; // signed 32-bit (±1.5e-45 to ±3.4e38) up to 7 symbols/digits
myFloat = 67.8E35f; // litteral with "E/e"
double myDouble = 34.56d; // signed 64-bit (±5.0e-324 to ±1.7e+308) up to 15-16 symbols/digits
myDouble = -3.4e-10d;
decimal myDecimal = 23.45M; // signed 128-bit (±1.0e-28 to ±7.9e28) precission 28-29 symbols/digits, closest to 0 ±1.0e-28, decimal floating-point arithmetic
// Declare some variables
float floatPI = 3.141592653589793238f;
double doublePI = 3.141592653589793238;
// Print the results on the console
Console.WriteLine("Float PI is: " + floatPI); // Float PI is: 3.141593 only 7 digits
Console.WriteLine("Double PI is: " + doublePI); // Double PI is: 3.14159265358979 16 digits
decimal decimalPI = 3.14159265358979323846m;
Console.WriteLine(decimalPI); // 3.14159265358979323846
// 3. Char
char myFirstLetter = 'S';
Console.WriteLine((int)myFirstLetter);
char symbol = (char)5;
char myChar = '\u0065';
Console.WriteLine(myChar);
myChar = '\uffff';
Console.WriteLine(myChar);
// escaping
char myEscape = '\n'; // \n \t \r \' \\ \" \uXXXX
// 4. String (default null) Reference, value in the heap
string myName = "Stoyan Cheresharov";
string path = @"D:\CamtasiaVideos"; // instead of D:\\CamtasiaVideos
Console.WriteLine($"The Path is {path}");
//StringBuilder text = new StringBuilder();
//foreach (char symbol in list)
// {
// text.Append(symbol);
//}
// 5. Bool (default false)
bool myBool = true; // false | true
// 6. Object (default null) Reference, value in the heap
Object myObject = 3;
Nullable<int> i1 = null;
int? i2 = i1;
// typeof()
// Console.WriteLine(typeof(i1)); // error
// Used to obtain the System.Type object for a type. A typeof expression takes the following form:
System.Type type = typeof(int);
int i = 0;
type = i.GetType();
Console.WriteLine(type);
// sizeof - Used to obtain the size in bytes for an unmanaged type.
Console.WriteLine(sizeof(sbyte)); // 1
// Unmanaged types include:
// The simple types that are listed in the following table:
// Expression Constant value
//sizeof(sbyte) 1
//sizeof(byte) 1
//sizeof(short) 2
//sizeof(ushort) 2
//sizeof(int) 4
//sizeof(uint) 4
//sizeof(long) 8
//sizeof(ulong) 8
//sizeof(char) 2(Unicode)
//sizeof(float) 4
//sizeof(double) 8
//sizeof(decimal) 16
//sizeof(bool) 1
Console.WriteLine("Hello World! The sum is " + sum);
Console.WriteLine("-----------------------");
Console.WriteLine((char)65);
Console.WriteLine((int)'D');
Console.WriteLine('\u0065');
int? myVanya = null;
Console.WriteLine("Vanya is " + myVanya.ToString() + " years old!");
Console.WriteLine(sizeof(long));
int myDesiInt = 10;
// double myDesiDouble = 1.2;
int myDesiDouble = 3;
Console.WriteLine("Desi " + ((double)myDesiInt / myDesiDouble));
// Console.WriteLine("Desi " + ((myDesiInt / myDesiDouble));
// Ctrl+r,r - refactoring/replace a name/string
// Not possible
//int? implicitConvertInt = 0;
//if (implicitConvertInt) { // Can not implicitly convert type int to bool
//}
//Object implicitConverToObj = null;
//if (implicitConverToObj) { // Can not implicitly convert type Obj to bool
//}
myDesiInt++;
Console.WriteLine(myDesiInt);
Console.WriteLine((myDesiInt > 18) ? "You are an adult" : "You are young");
byte myTest = 1;
Console.WriteLine(myTest);
Console.WriteLine(myTest << 1);
Console.WriteLine(myTest << 2);
myTest <<= 2;
Console.WriteLine(myTest);
byte myGeorgi = 8;
Console.WriteLine(myTest | myGeorgi);
int squarePerimeter = 17;
double squareSide = squarePerimeter / 4.0;
double squareArea = squareSide * squareSide;
Console.WriteLine(squareSide); // 4.25
Console.WriteLine(squareArea); // 18.0625
int a = 5;
int b = 4;
Console.WriteLine(a + b); // 9
Console.WriteLine(a + b++); // 9
Console.WriteLine(a + b); // 10
Console.WriteLine(a + (++b)); // 11
Console.WriteLine(a + b); // 11
Console.WriteLine(14 / a); // 2
Console.WriteLine(14 % a); // 4
a = 11;
b = 2;
// Arithmetic operators
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a / b);
Console.WriteLine(a * b);
Console.WriteLine(a++);
Console.WriteLine(a);
Console.WriteLine(++a);
Console.WriteLine(a % b);
Console.WriteLine(--a);
Console.WriteLine(a--);
// For comparison
Console.WriteLine(a > b);
Console.WriteLine(a < b);
Console.WriteLine(a >= b);
Console.WriteLine(a <= b);
Console.WriteLine(a == b);
Console.WriteLine(a != b);
// Logical
bool x = true;
bool y = false;
Console.WriteLine(x && y);
Console.WriteLine(x || y);
Console.WriteLine(x ^ y);
Console.WriteLine(!x);
// for asignments
a += 3; // a = a + 3;
// Bitwise operators
Console.WriteLine(1 << 1);
Console.WriteLine(2 >> 1);
Console.WriteLine(1 | 2);
Console.WriteLine(1 & 2);
Console.WriteLine(1 ^ 3);// 2
// int? a = 5;
// int? b = 6
// a = null;
// int? z = a ?? b;
// long d = 3214321342134231443;
// int i = checked((int)d); // System.OverflowException
int one = 1;
int zero = 0;
// Console.WriteLine(one / zero); // DivideByZeroException
double dMinusOne = -1.0;
double dZero = 0.0;
Console.WriteLine(dMinusOne / zero); // -Infinity
Console.WriteLine(one / dZero); // Infinity
Console.Clear();
Console.Write("Stoyan ");
Console.Write("Cheresharov \n\r");
Console.WriteLine("My name is Desi. I am {0} years old. my favorite number is {1}", myDesiInt, 3);
Console.WriteLine("| {0,10:######} |", 12);
// Formatting Numbers
// Standard Formatting Symbols C, D, E, F, N, P, X
Console.WriteLine("{0:C2}", 123.456); //Output: 123,46 лв.
Console.WriteLine("{0:D6}", -1234); //Output: -001234
Console.WriteLine("{0:E2}", 123); //Output: 1,23Е+002
Console.WriteLine("{0:F2}", -123.456); //Output: -123,46
Console.WriteLine("{0:N2}", 1234567.8); //Output: 1 234 567,80
Console.WriteLine("{0:P}", 0.456); //Output: 45,60 %
Console.WriteLine("{0:X}", 254); //Output: FE
// Custom Formatting Symbols 0000, ###, %, E0 Е+0 Е-0
Console.WriteLine("{0:0.00}", 1); //Output: 1,00
Console.WriteLine("{0:#.##}", 0.234); //Output: ,23
Console.WriteLine("{0:#####}", 12345.67); //Output: 12346
Console.WriteLine("{0:(0#) ### ## ##}", 29342525); //Output: (02) 934 25 25
Console.WriteLine("{0:%##}", 0.234);//Output: %23
// Standard Formatting Symbols for DateTime
DateTime d = new DateTime(2018, 9, 14, 15, 30, 22);
Console.WriteLine("{0:D}", d); // d D
Console.WriteLine("{0:t}", d); // t T
Console.WriteLine("{0:Y}", d); // y Y
// Custom Formatting Symbols for DateTime d, dd, M, MM, yy, yyyy, hh, HH, m, mm, s, ss
Console.WriteLine("{0:dd/MM/yyyy HH:mm:ss}", d);
Console.WriteLine("{0:d.MM.yy г.}", d);
// return;
Console.Clear();
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("bg-Bg");
Console.WriteLine("| {0,10:C} |", 120000.4532);
Console.Clear();
// Reading from the console and parsing
Console.Write("Please enter your age: ");
string inputVariable = Console.ReadLine();
// Parsing strings to numbers
int age = int.Parse(inputVariable);
double ageInDouble;
double.TryParse(inputVariable, out ageInDouble);
Console.WriteLine(Convert.ToInt16(inputVariable));
Console.WriteLine(age + 10);
// Read a character from the input with Read
//Console.Write("Please enter a character: ");
//int nextChar = Console.Read();
//Console.WriteLine(nextChar);
//Console.WriteLine((char)nextChar);
// return;
// ReadKey
//Console.WriteLine("Please press ESC key to exit this endless loop");
//ConsoleKeyInfo cki;
//do {
// if (!Console.KeyAvailable)
// {
// Console.Clear();
// Console.WriteLine("Please hit Esc");
// }
// cki = Console.ReadKey(true);
// System.Threading.Thread.Sleep(50);
//} while(cki.Key != ConsoleKey.Escape);
Console.OutputEncoding = Encoding.UTF8;
Console.ForegroundColor = ConsoleColor.Green;
// Console.BackgroundColor = ConsoleColor.Blue;
// To change the background color of the > console window as a whole, set the BackgroundColor property and call the Clear method.
// Console.BackgroundColor = ConsoleColor.Blue;
// Console.Clear();
Console.SetCursorPosition(5, 10); // left top
Console.WriteLine("Това е кирилица: ☺");
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("en-GB"); // UK doesn't work
// Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine("Your balance is {0, 30:C2}", 1234567.8904534);
//for (int charCode = 0; charCode < 1000; i++)
//{
// Console.WriteLine((char)i);
//}
int secondNumber = 3;
int firstNumber = 2;
if (secondNumber > firstNumber)
{
// int biggerNumber = secondNumber;
Console.WriteLine(secondNumber);
}
char ch = 'X';
if (ch == 'A' || ch == 'a')
{
Console.WriteLine("Vowel [ei]");
}
else if (ch == 'E' || ch == 'e')
{
Console.WriteLine("Vowel [i:]");
}
else if (ch == 'I' || ch == 'i')
{
Console.WriteLine("Vowel [ai]");
}
else if (ch == 'O' || ch == 'o')
{
Console.WriteLine("Vowel [ou]");
}
else if (ch == 'U' || ch == 'u')
{
Console.WriteLine("Vowel [ju:]");
}
else
{
Console.WriteLine("Consonant");
}
//if (булев израз)
//{
// тяло на условната конструкция;
//}
//else
//{
// тяло на else-конструкция;
//}
//switch (селектор)
//{
// case целочислена - стойност - 1: конструкция; break;
// case целочислена - стойност - 2: конструкция; break;
// case целочислена - стойност - 3: конструкция; break;
// case целочислена - стойност - 4: конструкция; break;
// // …
// default: конструкция; break;
//}
//while (условие)
//{
// тяло на цикъла;
//}
// Initialize the counter
int counter = 0;
// Execute the loop body while the loop condition holds
while (counter <= 9)
{
// Print the counter value
Console.WriteLine("Number : " + counter);
// Increment the counter
counter++;
}
// n! = (n - 1)!*n
Console.Write("Please, enter a number: ");
int n = int.Parse(Console.ReadLine());
// "decimal" is the biggest type that can hold integer values
decimal factorial = 1;
// Perform an "infinite loop"
while (true)
{
if (n <= 1)
{
break;
}
factorial *= n;
n--;
}
Console.WriteLine("n! = " + factorial);
int myDoTest = 0;
do
{
Console.Write("Please enter a number betwen 0 and 5: ");
myDoTest = int.Parse(Console.ReadLine());
} while (myDoTest > 5 || myDoTest < 0);
//for (инициализация; условие; обновяване)
//{
// тяло на цикъла;
//}
for (int j = 0; j <= 10; j++)
{
Console.Write(j + " ");
}
int t = 0;
while (t < 10)
{
// i++;
// break;
if (t % 2 == 0)
{
t++;
continue;
}
Console.WriteLine("t = " + t);
t++;
}
int[] numbers = { 2, 3, 5, 7, 11, 13, 17, 19 };
foreach (int k in numbers)
{
Console.Write(" " + k);
}
Console.WriteLine();
String[] towns = { "Sofia", "Plovdiv", "Varna", "Bourgas" };
foreach (String town in towns)
{
Console.Write(" " + town);
}
int special = 34;
Console.WriteLine($"This is my variable {special}");
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Yellow;
string[] students = new string[100];
students[0] = "Konstantin";
students[1] = "Angel";
Console.WriteLine(students[0]);
int[] myArray = { 1, 2, 3, 4, 5, 6 };
int[] myArray2 = new int[3] { 23, 45, 67 };
string[] daysOfWeek =
{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
int[] array = new int[] { 1, 2, 3, 4, 5 };
Console.Write("Output: ");
for (int index = 0; index < array.Length; index++)
{
// Doubling the number
array[index] = 2 * array[index];
// Print the number
Console.Write(array[index] + " ");
}
// Output: 2 4 6 8 10
//foreach (var item in collection)
//{
// // Process the value here
//}
int[,] matrix =
{
{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8}, // row 1 values
};
// The matrix size is 2 x 4 (2 rows, 4 cols)
Console.WriteLine();
Console.WriteLine(matrix[0,3]); // 4
Console.WriteLine(matrix.GetLength(0));
Console.WriteLine(matrix.GetLength(1));
int[][] myJaggedArray = {
new int[] {5, 7, 2},
new int[] {10, 20, 40},
new int[] {3, 25},
new int[] { 1,2,3,4,5,6,78,9}
};
int[][,] jaggedOfMulti = new int[2][,];
// another way to declare jagged array
int[][] jaggedArray;
jaggedArray = new int[2][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[3];
// String number = "100";
// int fromBase = 16;
// int toBase = 10;
// String result1 = Convert.ToString(Convert.ToInt32(number, fromBase), toBase);
// result == "256"
// Console.WriteLine(result1);
/*
// Convert to binary system
Console.Write("Please, enter a number to convert to binary: ");
// with 340282366920938000000000000000000000000
// System.OverflowException: Value was either too large or too small for an Int32.
int number2Convert = int.Parse(Console.ReadLine());
int[] binaryNumber = new int[128]; // the default value is 0
int digitCounter = 127;
while (number2Convert > 0 && digitCounter > 0)
{
binaryNumber[digitCounter] = number2Convert % 2;
digitCounter--;
number2Convert /= 2;
Console.WriteLine(number2Convert);
}
foreach (var digit in binaryNumber)
{
Console.Write(digit);
}
Console.WriteLine();
Console.WriteLine(Math.Pow(2, 128)); // 3.40282366920938E+38
// Console.WriteLine("{0,10:N}", Math.Pow(2, 128)); // 340,282,366,920,938,000,000,000,000,000,000,000,000.00
// Console.WriteLine("{0,10:D}", Math.Pow(2, 128)); // Exception
Console.WriteLine("{0,10:F0}", Math.Pow(2, 128)); // 340282366920938000000000000000000000000
// https://stackoverflow.com/questions/2954962/convert-integer-to-binary-in-c-sharp
int value = 8;
string binary = Convert.ToString(value, 2);
// Convert from any classic base to any base in C#
String number = "100";
int fromBase = 16;
int toBase = 10;
String result = Convert.ToString(Convert.ToInt32(number, fromBase), toBase);
// result == "256"
// Supported bases are 2, 8, 10 and 16
*/
// Convert from decimal to binary system
//34 - > 100010 = 1 * 2 ^ 5 + 0 + 0 + 0 + 1 * 2 ^ 1 + 0 = 32 + 2 = 34
//34 : 2 = 17 0
//17 : 2 = 8 1
//8 : 2 = 4 0
//4 : 2 = 2 0
//2 : 2 = 1 0
// 1
//100010
//100010
// 8| 4 | 2 | 1
// e.g. 12 is 8 + 4 then we put 1 in positions of 8 and 4 -> 1100
// Convert form base 16 to base 2
//0010 | 1010 | 1010 | 1010 | 1111 | 1110 | 0001 | 1111 | 1111
//2AAAFD1FF
// Method calls
string[] studentsTemp = new string[2];
InputNames(studentsTemp);
InputNames(studentsTemp, 5);
InputNames(studentsTemp, 3, 2, 3, 4, 5, 6, 2, 3, 6, 6);
InputNames(n: 6, names: studentsTemp);
// Recursion
int factorial2 = 5;
Console.WriteLine("!{0} = {1}", factorial2, Factorial(factorial2));
// Standard Math functions
Console.WriteLine(Math.Cos(23));
// every string can be used as array
string stoyanName = "Stoyan";
Console.WriteLine(stoyanName[0]); // S
// string functions - tyme the name of the variable and .
Console.WriteLine(stoyanName.IndexOf('t'));
// etc.
}
// Method declaration and definition, default parameters, variable number of parameters
static void InputNames(string[] names, int n = 4, params int[] myExtraParams)
{
for (var i = 0; i < names.Length; i++)
{
Console.Write("Please enter element number {0} :", i);
names[i] = Console.ReadLine() + " Default Mark = " + n + " Final Mark" + ((myExtraParams.Length > 0) ? myExtraParams[i].ToString() : "0"); //
Console.WriteLine(names[i]);
}
}
// Recursion
// !n = !(n-1) * n
static int Factorial(int n)
{
if (n == 0) return 1;
return Factorial(n - 1) * n;
}
}
}
Comments