Advertisement
wingman007

IntroC#_loops_arrays_decimal_methods_recursion_2a

Sep 17th, 2014
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HelloWorld2a
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // !!! Use Ctrl + F5 to rub the program in VS
  14.             // F10 - trace the program. Execute step by step without entering the methods
  15.             // F9 - set a break point. F5 reach the breakpoint and use F10 or F11 to execute step by step
  16.             // F11 - trace with entering the methods.
  17.             // F10 ("step over") does not descend any further into the call stack. It moves to the next line of the current function.
  18.             // F11 ("step into") drills down into the function being called.
  19.             // http://visualstudioshortcuts.com/2015/
  20.  
  21.             // 1. Variables, types, literals
  22.             int age = -128;
  23.             double money = 2.54D;
  24.             char firstLetter = 'S'; // '\u000f';
  25.             bool isMale = true;
  26.            // int 2f1dhgf = 12; ERROR can not start with number
  27.             string name = "Stoyan";
  28.             object myObject = 5;
  29.             // All types:
  30.             // byte (8 bits), sbyte, short, ushort, int (32 bits), uint, long, ulong (64 bits)
  31.             // float (32 bits, 7 digits after the point), double (64 bits, 15-16 digits after), decimal (128 bits)
  32.             // char
  33.             // string
  34.             // bool
  35.             // object
  36.             // literals: uint 0u, ulong 0u, long 0L, float 0.0f, double 0.0d, decimal 0.0m , char '\u0000,
  37.             // 0xaa (base 16), 3.14e+1 = 31.4, can have letter at the end as well 3.14e+1f for float ()
  38.             // Escape: \uXXXX, \', \", \\, \n, \t
  39.  
  40.             // paradox
  41.             double sum = 0.1 + 0.2;
  42.             Console.WriteLine(sum == 0.3); // False
  43.  
  44.  
  45.             Nullable<int> myNUllable = null;
  46.             int? anotherNullable = myNUllable;
  47.            
  48.             int first = 13;
  49.             int second = 2;
  50.             double result;
  51.             result = first / second; // !!! result is 6. If you want to get the right 6.5 convert the types        
  52.             result = (double)first / second; // !!! the right 6.5
  53.            
  54.             Console.WriteLine(13 / 2); // 6 integer divide by integer gives integer
  55.             Console.WriteLine(13 / 2.0 ); // 6.5 integer divided by double gives double
  56.  
  57.  
  58.             DateTime timeNow = DateTime.Now; // // 15.9.2016 г. 13:24:17 ч.
  59.             timeNow = timeNow.AddYears(2);
  60.             Console.WriteLine(timeNow); // 15.9.2018 г. 13:24:17 ч.
  61.  
  62.             DateTime myDateTime = new DateTime();
  63.             Console.WriteLine(myDateTime); // 1.1.0001 г. 00:00:00 ч.
  64.  
  65.             // 2. Operators
  66.  
  67. /*
  68.             byte a = 13;
  69.             byte b = 5;
  70.             // 1.
  71.             Console.WriteLine("{0} + {1} = {2}",a, b, a + b);
  72.             Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
  73.             Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
  74.             Console.WriteLine("{0} / {1} = {2}", a, b, a / b);
  75.             Console.WriteLine("{0} % {1} = {2}", a, b, a % b);
  76.             Console.WriteLine("++{0}  = {1}", a, ++a);
  77.  
  78.             // 2. comparison
  79.             Console.WriteLine("{0} < {1} = {2}", a, b, a < b);
  80.             Console.WriteLine("{0} > {1} = {2}", a, b, a > b);
  81.             Console.WriteLine("{0} <= {1} = {2}", a, b, a <= b);
  82.             Console.WriteLine("{0} >= {1} = {2}", a, b, a >= b);
  83.             Console.WriteLine("{0} == {1} = {2}", a, b, a == b);
  84.             Console.WriteLine("{0} == {1} = {2}", a, b, a != b);
  85.            
  86.             // 3. Boolean
  87.             bool c = true;
  88.             bool d = false;
  89.  
  90.             Console.WriteLine("{0} && {1} = {2}", c, d, c && d);
  91.             Console.WriteLine("{0} || {1} = {2}", c, d, c || d);
  92.             Console.WriteLine("{0} ^ {1} = {2}", c, d, c ^ d);
  93.             Console.WriteLine("!{0} = {1}", c, !c);
  94.  
  95.             // kdjsfhsdkfjh askjfsdk fdkjh fksjh fkjsd
  96.             // a = 5; // a = a + 5;
  97.  
  98.             Console.WriteLine("{0} << 1 {1}", 1, 1 << 1);
  99.             Console.WriteLine("{0} >> 1 {1}", 1, 1 >> 1);
  100.             Console.WriteLine("{0} | {1} = {2}", a, b, a | b);
  101.             Console.WriteLine("{0} & {1} = {2}", a, b, a & b);
  102.             Console.WriteLine("{0} ^ {1} = {2}", a, b, a ^ b);
  103.             Console.WriteLine("~{0} = {1}", a, ~a);
  104.  
  105. */
  106.  
  107.  
  108.             // De Morgan's laws
  109.             // !(a && b) == (!a || !b)
  110.             // !(a || b) == (!a && !b)
  111.  
  112.             byte a = 3; // 0000 0011 = 3
  113.             byte b = 5; // 0000 0101 = 5
  114.             Console.WriteLine(a | b); // 0000 0111 = 7
  115.             Console.WriteLine(a & b); // 0000 0001 = 1
  116.             Console.WriteLine(a ^ b); // 0000 0110 = 6
  117.             Console.WriteLine(~a & b); // 0000 0100 = 4
  118.             Console.WriteLine(a << 1); // 0000 0110 = 6
  119.             Console.WriteLine(a << 2); // 0000 1100 = 12
  120.             Console.WriteLine(a >> 1); // 0000 0001 = 1
  121.             // Set the bit in position p to 0
  122.             // n = n & (~(1 << p));
  123.             // set the bit in position p to 1
  124.             // n = n | (1 << p);
  125.  
  126.             byte result = 23;
  127.             Console.WriteLine(Convert.ToString(result, 2).PadLeft(16, '0'));
  128.             // 0000000000110011
  129.  
  130.             // Concatenation
  131.             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!
  132.  
  133.             bool pr = (1 + 2) * 8 % 4 > (23 + 45) % 34;
  134.  
  135.             // 3. Console
  136.             Console.WriteLine("Please enetr your age?");
  137.             string input = Console.ReadLine();
  138.             int parsed = int.Parse(input);
  139.             Console.WriteLine("You have entered: {1} {0}", ++parsed, age);
  140.  
  141. /* Or use try throw catch finally
  142.             try
  143.             {
  144.                 age = int.Parse(input);
  145.             }
  146.             catch (System.FormatException e)
  147.             {
  148.                 Console.WriteLine("Incorect data!!! {0}", e.Message);
  149.                 // throw new System.
  150.             }
  151.             finally
  152.             {
  153.                 Console.WriteLine("I am always executed!");
  154.             }
  155. */
  156.  
  157.             Console.WriteLine("Hello World! And I am " + age + " years old!");
  158.             Console.WriteLine("I have in my pocket : " + money);
  159.             Console.WriteLine("The first letter of my name is: " + firstLetter);
  160.             Console.WriteLine("Am I a man? : " + isMale);
  161.             Console.WriteLine("My Name is " + name);
  162.             Console.WriteLine("Result: " + pr);
  163.  
  164.             // 4. Control statements
  165.             if (parsed < 20)
  166.             {
  167.                 Console.WriteLine("You are an teenager!");
  168.             }
  169.             else if (parsed >= 20 && parsed <= 30)
  170.             {
  171.                 Console.WriteLine("You are in a brilliant stage of your life the! It is time for fun");
  172.             }
  173.             else if (parsed > 30 && parsed <= 40)
  174.             {
  175.                 Console.WriteLine("Its time for a family life");
  176.             }
  177.             else {
  178.                 Console.WriteLine("You are an old man");
  179.             }
  180.  
  181.             switch (parsed) { // integar, string enum
  182.                 case 21 :
  183.                 case 20 :
  184.                     Console.WriteLine("switch You are in perfect age!");
  185.                     break;
  186.                 case 50:
  187.                     Console.WriteLine("switch You are in the middle of your life");
  188.                     break;
  189.                 default :
  190.                     Console.WriteLine("switch I don't know what to say");
  191.                     break;
  192.             }
  193.  
  194.             Console.WriteLine(1);
  195.             Console.WriteLine(2);
  196.             Console.WriteLine(3);
  197.             // ...
  198.             Console.WriteLine("1\n2\n3\n");
  199.  
  200.             int i = 1;
  201.             while (i <= 10) {
  202.                 if (i == 5) break;
  203.                 Console.WriteLine(i);
  204.                 i++;
  205.             }
  206.  
  207.  
  208.             Console.WriteLine("Please choose:");
  209.             Console.WriteLine("1. List the partitions");
  210.             Console.WriteLine("2. Create a partition");
  211.             Console.WriteLine("3. Delete partiotion");
  212.             Console.WriteLine("4. Exit");
  213.             string choice = "";
  214.             while (true) {
  215.                 //... do something smart
  216.                 choice = Console.ReadLine();
  217.                 if (choice == "4")
  218.                 {
  219.                     break;
  220.                 }
  221.             }
  222.  
  223.  
  224.             i = 11;
  225.             do
  226.             {
  227.                 Console.WriteLine("do-while: - {0}", i);
  228.                 i++;
  229.             } while (i <= 10);
  230.  
  231.  
  232.  
  233.             /* Menu
  234.             string choice = "";
  235.             i = 1;
  236.             do
  237.             {
  238.                 Console.Clear();
  239.                 Console.WriteLine("Моля изберете:");
  240.                 Console.WriteLine("1. Пържени яйца");
  241.                 Console.WriteLine("2. Фасул");
  242.                 Console.WriteLine("3. Пиле с ориз");
  243.                 Console.WriteLine("4. Изход");
  244.                 // Console.WriteLine("do-while: {0}!", i);
  245.                 choice = Console.ReadLine();
  246.                 if (choice == "4")
  247.                 {
  248.                     break;
  249.                 }
  250.                
  251.                 if (choice == "1") {
  252.                     Console.Clear();
  253.                     Console.WriteLine("Натиснете който и да е клавиш, за да се върнете към главното меню!");
  254.                     Console.WriteLine("За да си опържиш яийца ...");
  255.                     choice = Console.ReadLine();
  256.                 }
  257.              
  258.                 //if (choice == "1")
  259.                 //{
  260.                 //    do
  261.                 //    {
  262.                 //        Console.Clear();
  263.                 //        Console.WriteLine("Натиснете 4 за да се върнете към главното меню!");
  264.                 //        Console.WriteLine("За да си опържиш яийца ...");
  265.                 //        choice = Console.ReadLine();
  266.                 //        if (choice == "4") break;
  267.                 //    } while (true);
  268.  
  269.                 //}
  270.            
  271.             }while(true);
  272.             */
  273.  
  274.  
  275.  
  276.             int k;
  277.  
  278.             for (i = 0; i < 10; i++) {
  279.                 if (i % 2 != 1) continue;
  280.                 Console.WriteLine("for - {0}", i);
  281.             }
  282.  
  283.             string[] group2a = { "Milena", "Geri", "Valq", "Maria", "Milena", "Sofia", "Musti", "Virginia", "Slavcho", "Borislav", "Venci", "Rado"};
  284.  
  285.             Console.WriteLine("The list of group 2a:");
  286.             foreach (string namestr in group2a) {
  287.                 Console.WriteLine(namestr);
  288.             }
  289.  
  290.             // block scoping
  291.             // string namestr; // This is not correct
  292.  
  293.             // child block scope
  294.             {
  295.                 string namestr; // it is OK
  296.             }
  297.  
  298.             for (i = 0; i < 10; i++) {
  299.                 for (int j = 0; j < i; j++) {
  300.                     Console.Write("*");
  301.                 }
  302.                 Console.WriteLine();
  303.             }
  304.  
  305.             /* 1.
  306.             int[] myArray;
  307.             myArray = new int[5];
  308.             myArray[4] = 45;
  309.             */
  310.  
  311.             // 2.
  312.             // int[] myArray = { 23, 45, 67, 89 };
  313.  
  314.             // 3.
  315.             int[] myArray = new int[5] { 23, 45, 67, 89, 98 };
  316.  
  317.             for (i = 0; i < myArray.Length; i++) {
  318.                 Console.WriteLine("myArray[{0}] = {1}", i, myArray[i]);
  319.             }
  320.  
  321.             string[,] positionsOfTheStudents = {
  322.                    {"Milena", "Geri","","","","Valq", "","Maria"},
  323.                    {"Milena", "Sofia","","Musti","Virginia","", "","Slavcho"},
  324.                    {"Borislav", "Venci","","Rado","","", "",""}
  325.             };
  326.  
  327.             for (i = 0; i < positionsOfTheStudents.GetLength(0); i++)
  328.             {
  329.                 Console.Write("Red {0}: ", i);
  330.                 for (int j = 0; j < positionsOfTheStudents.GetLength(1); j++)
  331.                 {
  332.                     Console.Write(" {0}", positionsOfTheStudents[i, j]);
  333.                 }
  334.                 Console.WriteLine();
  335.             }
  336.  
  337.             int[][] jaggedArray;
  338.             jaggedArray = new int[2][];
  339.             jaggedArray[0] = new int[8];
  340.             jaggedArray[1] = new int[5];
  341.  
  342.  
  343.             byte resultTest = 148;
  344.  
  345.             // bynary
  346.             Console.WriteLine(Convert.ToString(resultTest, 2).PadLeft(16, '0'));
  347.             Console.WriteLine("{0,10:X}", 148);
  348.             // Console.WriteLine("{0,10:C4}", 148);
  349.  
  350.             /*
  351.             0000000010010100
  352.  
  353.             1*2^7 + 1*2^4 + 1* 2^2 = 128 + 16 + 4 = 148
  354.  
  355.             148 = 1*10^2 + 4*10^1 + 8*10^0 = 100 + 40 +8 = 148
  356.  
  357.             148 % 2 = 0
  358.             74 % 2 = 0
  359.             37 % 2 = 1
  360.             18 % 2 = 0
  361.             9 % 2 = 1
  362.             4 % 2 = 0
  363.             2 % 2 = 0
  364.                     1
  365.  
  366.             10010100
  367.             10010100
  368.             */
  369.  
  370.             /*
  371.             string temp = "";
  372.  
  373.             for (i = 0; i < 10; i++) {
  374.                 temp += "*";
  375.             }
  376.  
  377.             Console.WriteLine(temp);
  378.  
  379.             temp = "";
  380.             for (i = 0; i < 5; i++)
  381.             {
  382.                 temp += "@";
  383.             }
  384.             Console.WriteLine(temp);
  385.  
  386.             */
  387.  
  388.             Console.WriteLine(createString(10));
  389.             Console.WriteLine(createString(5));
  390.             Console.WriteLine(createString(5, "@"));
  391.             Console.WriteLine(createString(12, "^", "Milena", "Geri"));
  392.             Console.WriteLine(createString());
  393.  
  394.             Console.WriteLine("Factorial from {1} = {0} ", factorial(6), 6);
  395.             Console.WriteLine("Factorial from {1} = {0} ", factorialIteration(6), 6);
  396.  
  397.             Console.WriteLine("Fibonacci number {1} = {0} ", fibonacci(7), 7);
  398.  
  399.  
  400.             Console.WriteLine("Prime numbers:");
  401.             bool isPrime = true;
  402.             for (i = 2; i < 100; i++)
  403.             {
  404.                 isPrime = true;
  405.                 for (int j = 2; j < i; j++)
  406.                 {
  407.                     // if (i % j == 0)
  408.                     if (i % Math.Sqrt(j) == 0) // better
  409.                     {
  410.                         isPrime = false;
  411.                         break;
  412.                     }  
  413.                 }
  414.                 if (isPrime) {
  415.                     Console.WriteLine(i);
  416.                 }
  417.             }
  418.  
  419.             // Pine Tree
  420.             Console.WriteLine("Pine Tree");
  421.             int size = 80;
  422.             for (i = 0; i < size; i++)
  423.             {
  424.                 if (i % 2 == 0) continue;
  425.                 for (int k = (size - i) / 2; k > 0; k--)
  426.                 {
  427.                     Console.Write(" ");
  428.                 }
  429.                 for (int j = 0; j < i; j++)
  430.                 {
  431.                     Console.Write("*");
  432.                 }
  433.                 Console.WriteLine();
  434.             }
  435.  
  436.             Console.WriteLine("The number 0xfabc = {0,10:d} ", 0xfabc);
  437.             Console.WriteLine("The number 64188 = {0,10:x}", 64188);
  438.  
  439.         }
  440.  
  441.         static string createString(int strLength, string str = "*", params string[] par) {
  442.             string temp = "";
  443.             for (int i = 0; i < strLength; i++)
  444.             {
  445.                 temp += str;
  446.             }
  447.  
  448.             foreach (string item in par)
  449.             {
  450.                 Console.WriteLine(item);
  451.             }
  452.  
  453.             return temp;
  454.         }
  455.  
  456.         /*
  457.         static string createString(int sadh, string erwrew = "*", params string[] par)
  458.         {
  459.             return "Error!!!";
  460.         }
  461.         */
  462.  
  463.         static string createString()
  464.         {
  465.             return "Hahahah";
  466.         }
  467.  
  468.         // (n-1)! * n
  469.         static int factorial(int n)
  470.         {
  471.             if (n == 0) return 1;
  472.             return factorial(n - 1) * n;
  473.         }
  474.  
  475.         static int factorialIteration(int n)
  476.         {
  477.             var factorial = 1;
  478.             for (int i = 1; i <= n; i++)
  479.             {
  480.                 factorial *= i;
  481.             }
  482.  
  483.             return factorial;
  484.         }
  485.  
  486.         // Fibonacci Fn = (Fn-2) + (Fn-1)
  487.         // e.g. 1,1,2,3,5,8
  488.         static int fibonacci(int n) {
  489.             if (n <= 2) return 1;
  490.             return  fibonacci(n-2) + fibonacci(n-1);
  491.         }
  492.     }
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement