wingman007 icon

2018_IntroC#PartTimeOperatorsConsoleIfFor

wingman007 | PRO | 09/15/18 06:29:09 AM UTC | 0 ⭐ | 1303 👁️ | Never ⏰ | []
C# |

13.31 KB

|

None

|

0 👍

/

0 👎

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)
        {
            // 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
 
            // 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}");
 
            // 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
            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);
            Console.Clear();
            Thread.CurrentThread.CurrentCulture =
            CultureInfo.GetCultureInfo("bg-Bg");
 
 
            Console.WriteLine("| {0,10:C} |", 120000.4532);
            Console.Clear();
            Console.Write("Please eneter your age: ");
            string inputVariable = Console.ReadLine();
            int age = int.Parse(inputVariable);
            // double.TryParse(inputVariable,out );
            // Convert.ToInt16();
            Console.WriteLine(age + 10);
 
            Console.OutputEncoding = Encoding.UTF8;
 
            Console.ForegroundColor = ConsoleColor.Green;
            // Console.BackgroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(5, 10);
            Console.WriteLine("Това е кирилица: ☺");
 
            Thread.CurrentThread.CurrentCulture =
            CultureInfo.GetCultureInfo("en-GB");
 
            Console.WriteLine("Your balance is {0, 10:C2}", 1234567.8904534);
 
            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");
            }
 
 
            //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
 
            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 enetr 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}");
            
            /*
            // Convert to binary system
            Console.Write("Please enter a number to convert: ");
            // 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
            */
        }
    }
}

Comments