Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HelloWorld2a
- {
- class Program
- {
- static void Main(string[] args)
- {
- // !!! Use Ctrl + F5 to rub the program in VS
- // F10 - trace the program. Execute step by step without entering the methods
- // F9 - set a break point. F5 reach the breakpoint and use F10 or F11 to execute step by step
- // F11 - trace with entering the methods.
- // F10 ("step over") does not descend any further into the call stack. It moves to the next line of the current function.
- // F11 ("step into") drills down into the function being called.
- // http://visualstudioshortcuts.com/2015/
- // 1. Variables, types, literals
- int age = -128;
- double money = 2.54D;
- char firstLetter = 'S'; // '\u000f';
- bool isMale = true;
- // int 2f1dhgf = 12; ERROR can not start with number
- string name = "Stoyan";
- object myObject = 5;
- // All types:
- // byte (8 bits), sbyte, short, ushort, int (32 bits), uint, long, ulong (64 bits)
- // float (32 bits, 7 digits after the point), double (64 bits, 15-16 digits after), decimal (128 bits)
- // char
- // string
- // bool
- // object
- // literals: uint 0u, ulong 0u, long 0L, float 0.0f, double 0.0d, decimal 0.0m , char '\u0000,
- // 0xaa (base 16), 3.14e+1 = 31.4, can have letter at the end as well 3.14e+1f for float ()
- // Escape: \uXXXX, \', \", \\, \n, \t
- // paradox
- double sum = 0.1 + 0.2;
- Console.WriteLine(sum == 0.3); // False
- Nullable<int> myNUllable = null;
- int? anotherNullable = myNUllable;
- int first = 13;
- int second = 2;
- double result;
- result = first / second; // !!! result is 6. If you want to get the right 6.5 convert the types
- result = (double)first / second; // !!! the right 6.5
- Console.WriteLine(13 / 2); // 6 integer divide by integer gives integer
- Console.WriteLine(13 / 2.0 ); // 6.5 integer divided by double gives double
- DateTime timeNow = DateTime.Now; // // 15.9.2016 г. 13:24:17 ч.
- timeNow = timeNow.AddYears(2);
- Console.WriteLine(timeNow); // 15.9.2018 г. 13:24:17 ч.
- DateTime myDateTime = new DateTime();
- Console.WriteLine(myDateTime); // 1.1.0001 г. 00:00:00 ч.
- // 2. Operators
- /*
- byte a = 13;
- byte b = 5;
- // 1.
- Console.WriteLine("{0} + {1} = {2}",a, b, a + b);
- Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
- Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
- Console.WriteLine("{0} / {1} = {2}", a, b, a / b);
- Console.WriteLine("{0} % {1} = {2}", a, b, a % b);
- Console.WriteLine("++{0} = {1}", a, ++a);
- // 2. comparison
- Console.WriteLine("{0} < {1} = {2}", a, b, a < b);
- Console.WriteLine("{0} > {1} = {2}", a, b, a > b);
- Console.WriteLine("{0} <= {1} = {2}", a, b, a <= b);
- Console.WriteLine("{0} >= {1} = {2}", a, b, a >= b);
- Console.WriteLine("{0} == {1} = {2}", a, b, a == b);
- Console.WriteLine("{0} == {1} = {2}", a, b, a != b);
- // 3. Boolean
- bool c = true;
- bool d = false;
- Console.WriteLine("{0} && {1} = {2}", c, d, c && d);
- Console.WriteLine("{0} || {1} = {2}", c, d, c || d);
- Console.WriteLine("{0} ^ {1} = {2}", c, d, c ^ d);
- Console.WriteLine("!{0} = {1}", c, !c);
- // kdjsfhsdkfjh askjfsdk fdkjh fksjh fkjsd
- // a = 5; // a = a + 5;
- Console.WriteLine("{0} << 1 {1}", 1, 1 << 1);
- Console.WriteLine("{0} >> 1 {1}", 1, 1 >> 1);
- Console.WriteLine("{0} | {1} = {2}", a, b, a | b);
- Console.WriteLine("{0} & {1} = {2}", a, b, a & b);
- Console.WriteLine("{0} ^ {1} = {2}", a, b, a ^ b);
- Console.WriteLine("~{0} = {1}", a, ~a);
- */
- // De Morgan's laws
- // !(a && b) == (!a || !b)
- // !(a || b) == (!a && !b)
- byte a = 3; // 0000 0011 = 3
- byte b = 5; // 0000 0101 = 5
- Console.WriteLine(a | b); // 0000 0111 = 7
- Console.WriteLine(a & b); // 0000 0001 = 1
- Console.WriteLine(a ^ b); // 0000 0110 = 6
- Console.WriteLine(~a & b); // 0000 0100 = 4
- Console.WriteLine(a << 1); // 0000 0110 = 6
- Console.WriteLine(a << 2); // 0000 1100 = 12
- Console.WriteLine(a >> 1); // 0000 0001 = 1
- // Set the bit in position p to 0
- // n = n & (~(1 << p));
- // set the bit in position p to 1
- // n = n | (1 << p);
- byte result = 23;
- Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
- // 0000000000110011
- // Concatenation
- Console.WriteLine(23 + 45 + 45 + "23 this will be concatinated instead of added there is a string operad!"); // 11323 this will be concatinated instead of added there is a string operad!
- bool pr = (1 + 2) * 8 % 4 > (23 + 45) % 34;
- // 3. Console
- Console.WriteLine("Please enetr your age?");
- string input = Console.ReadLine();
- int parsed = int.Parse(input);
- Console.WriteLine("You have entered: {1} {0}", ++parsed, age);
- /* Or use try throw catch finally
- try
- {
- age = int.Parse(input);
- }
- catch (System.FormatException e)
- {
- Console.WriteLine("Incorect data!!! {0}", e.Message);
- // throw new System.
- }
- finally
- {
- Console.WriteLine("I am always executed!");
- }
- */
- Console.WriteLine("Hello World! And I am " + age + " years old!");
- Console.WriteLine("I have in my pocket : " + money);
- Console.WriteLine("The first letter of my name is: " + firstLetter);
- Console.WriteLine("Am I a man? : " + isMale);
- Console.WriteLine("My Name is " + name);
- Console.WriteLine("Result: " + pr);
- // 4. Control statements
- if (parsed < 20)
- {
- Console.WriteLine("You are an teenager!");
- }
- else if (parsed >= 20 && parsed <= 30)
- {
- Console.WriteLine("You are in a brilliant stage of your life the! It is time for fun");
- }
- else if (parsed > 30 && parsed <= 40)
- {
- Console.WriteLine("Its time for a family life");
- }
- else {
- Console.WriteLine("You are an old man");
- }
- switch (parsed) { // integar, string enum
- case 21 :
- case 20 :
- Console.WriteLine("switch You are in perfect age!");
- break;
- case 50:
- Console.WriteLine("switch You are in the middle of your life");
- break;
- default :
- Console.WriteLine("switch I don't know what to say");
- break;
- }
- Console.WriteLine(1);
- Console.WriteLine(2);
- Console.WriteLine(3);
- // ...
- Console.WriteLine("1\n2\n3\n");
- int i = 1;
- while (i <= 10) {
- if (i == 5) break;
- Console.WriteLine(i);
- i++;
- }
- Console.WriteLine("Please choose:");
- Console.WriteLine("1. List the partitions");
- Console.WriteLine("2. Create a partition");
- Console.WriteLine("3. Delete partiotion");
- Console.WriteLine("4. Exit");
- string choice = "";
- while (true) {
- //... do something smart
- choice = Console.ReadLine();
- if (choice == "4")
- {
- break;
- }
- }
- i = 11;
- do
- {
- Console.WriteLine("do-while: - {0}", i);
- i++;
- } while (i <= 10);
- /* Menu
- string choice = "";
- i = 1;
- do
- {
- Console.Clear();
- Console.WriteLine("Моля изберете:");
- Console.WriteLine("1. Пържени яйца");
- Console.WriteLine("2. Фасул");
- Console.WriteLine("3. Пиле с ориз");
- Console.WriteLine("4. Изход");
- // Console.WriteLine("do-while: {0}!", i);
- choice = Console.ReadLine();
- if (choice == "4")
- {
- break;
- }
- if (choice == "1") {
- Console.Clear();
- Console.WriteLine("Натиснете който и да е клавиш, за да се върнете към главното меню!");
- Console.WriteLine("За да си опържиш яийца ...");
- choice = Console.ReadLine();
- }
- //if (choice == "1")
- //{
- // do
- // {
- // Console.Clear();
- // Console.WriteLine("Натиснете 4 за да се върнете към главното меню!");
- // Console.WriteLine("За да си опържиш яийца ...");
- // choice = Console.ReadLine();
- // if (choice == "4") break;
- // } while (true);
- //}
- }while(true);
- */
- int k;
- for (i = 0; i < 10; i++) {
- if (i % 2 != 1) continue;
- Console.WriteLine("for - {0}", i);
- }
- string[] group2a = { "Milena", "Geri", "Valq", "Maria", "Milena", "Sofia", "Musti", "Virginia", "Slavcho", "Borislav", "Venci", "Rado"};
- Console.WriteLine("The list of group 2a:");
- foreach (string namestr in group2a) {
- Console.WriteLine(namestr);
- }
- // block scoping
- // string namestr; // This is not correct
- // child block scope
- {
- string namestr; // it is OK
- }
- for (i = 0; i < 10; i++) {
- for (int j = 0; j < i; j++) {
- Console.Write("*");
- }
- Console.WriteLine();
- }
- /* 1.
- int[] myArray;
- myArray = new int[5];
- myArray[4] = 45;
- */
- // 2.
- // int[] myArray = { 23, 45, 67, 89 };
- // 3.
- int[] myArray = new int[5] { 23, 45, 67, 89, 98 };
- for (i = 0; i < myArray.Length; i++) {
- Console.WriteLine("myArray[{0}] = {1}", i, myArray[i]);
- }
- string[,] positionsOfTheStudents = {
- {"Milena", "Geri","","","","Valq", "","Maria"},
- {"Milena", "Sofia","","Musti","Virginia","", "","Slavcho"},
- {"Borislav", "Venci","","Rado","","", "",""}
- };
- for (i = 0; i < positionsOfTheStudents.GetLength(0); i++)
- {
- Console.Write("Red {0}: ", i);
- for (int j = 0; j < positionsOfTheStudents.GetLength(1); j++)
- {
- Console.Write(" {0}", positionsOfTheStudents[i, j]);
- }
- Console.WriteLine();
- }
- int[][] jaggedArray;
- jaggedArray = new int[2][];
- jaggedArray[0] = new int[8];
- jaggedArray[1] = new int[5];
- byte resultTest = 148;
- // bynary
- Console.WriteLine(Convert.ToString(resultTest, 2).PadLeft(16, '0'));
- Console.WriteLine("{0,10:X}", 148);
- // Console.WriteLine("{0,10:C4}", 148);
- /*
- 0000000010010100
- 1*2^7 + 1*2^4 + 1* 2^2 = 128 + 16 + 4 = 148
- 148 = 1*10^2 + 4*10^1 + 8*10^0 = 100 + 40 +8 = 148
- 148 % 2 = 0
- 74 % 2 = 0
- 37 % 2 = 1
- 18 % 2 = 0
- 9 % 2 = 1
- 4 % 2 = 0
- 2 % 2 = 0
- 1
- 10010100
- 10010100
- */
- /*
- string temp = "";
- for (i = 0; i < 10; i++) {
- temp += "*";
- }
- Console.WriteLine(temp);
- temp = "";
- for (i = 0; i < 5; i++)
- {
- temp += "@";
- }
- Console.WriteLine(temp);
- */
- Console.WriteLine(createString(10));
- Console.WriteLine(createString(5));
- Console.WriteLine(createString(5, "@"));
- Console.WriteLine(createString(12, "^", "Milena", "Geri"));
- Console.WriteLine(createString());
- Console.WriteLine("Factorial from {1} = {0} ", factorial(6), 6);
- Console.WriteLine("Factorial from {1} = {0} ", factorialIteration(6), 6);
- Console.WriteLine("Fibonacci number {1} = {0} ", fibonacci(7), 7);
- Console.WriteLine("Prime numbers:");
- bool isPrime = true;
- for (i = 2; i < 100; i++)
- {
- isPrime = true;
- for (int j = 2; j < i; j++)
- {
- // if (i % j == 0)
- if (i % Math.Sqrt(j) == 0) // better
- {
- isPrime = false;
- break;
- }
- }
- if (isPrime) {
- Console.WriteLine(i);
- }
- }
- // Pine Tree
- Console.WriteLine("Pine Tree");
- int size = 80;
- for (i = 0; i < size; i++)
- {
- if (i % 2 == 0) continue;
- for (int k = (size - i) / 2; k > 0; k--)
- {
- Console.Write(" ");
- }
- for (int j = 0; j < i; j++)
- {
- Console.Write("*");
- }
- Console.WriteLine();
- }
- Console.WriteLine("The number 0xfabc = {0,10:d} ", 0xfabc);
- Console.WriteLine("The number 64188 = {0,10:x}", 64188);
- }
- static string createString(int strLength, string str = "*", params string[] par) {
- string temp = "";
- for (int i = 0; i < strLength; i++)
- {
- temp += str;
- }
- foreach (string item in par)
- {
- Console.WriteLine(item);
- }
- return temp;
- }
- /*
- static string createString(int sadh, string erwrew = "*", params string[] par)
- {
- return "Error!!!";
- }
- */
- static string createString()
- {
- return "Hahahah";
- }
- // (n-1)! * n
- static int factorial(int n)
- {
- if (n == 0) return 1;
- return factorial(n - 1) * n;
- }
- static int factorialIteration(int n)
- {
- var factorial = 1;
- for (int i = 1; i <= n; i++)
- {
- factorial *= i;
- }
- return factorial;
- }
- // Fibonacci Fn = (Fn-2) + (Fn-1)
- // e.g. 1,1,2,3,5,8
- static int fibonacci(int n) {
- if (n <= 2) return 1;
- return fibonacci(n-2) + fibonacci(n-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement