вторник, 1 януари 2013 г.

C#: Primitive Data Types and Variables

Това са решенията на задачите от домашното по C# на тема Primitive Data Types and Variables. Видео и материали от лекцията може да се намерят тук.

1. Declare five variables choosing for each of them the most appropriate of the types byte, sbyte, short, ushort, int, uint, long, ulong to represent the following values: 52130, -115, 4825932, 97, -10000.
using System;

class DeclareFiveVariables
{
    static void Main()
    {
        ushort firstValue = 52130;
        sbyte secondValue = -115;
        int thirdValue = 4825932;
        byte fourthValue = 97;
        short fifthValue = -10000;
        Console.WriteLine("{0} {1} {2} {3} {4}", firstValue, secondValue, thirdValue, fourthValue, fifthValue);
    }
}

2. Which of the following values can be assigned to a variable of type float and which to a variable of type double: 34.567839023, 12.345, 8923.1234857, 3456.091?
using System;

class FloatOrDouble
{
    static void Main()
    {
        double firstValue = 34.567839023;
        float secondValue = 12.345f;
        double thirdValue = 8923.1234857;
        float fourthValue = 3456.091f;
        Console.WriteLine("float: {0} {1} " , secondValue , fourthValue);
        Console.WriteLine("double: {0} {1}" , firstValue , thirdValue);
    }
}

3. Write a program that safely compares floating-point numbers with precision of 0.000001. Examples: (5.3 ; 6.01) -> false;  (5.00000001 ; 5.00000003) -> true
using System;

class CompareFloatingPointNumbers
{
    static void Main()
    {
        float a = 6.34f;
        float b = 7.21f;
        float c = 6.123456789f;
        float d = 6.12345678901f;
        bool compareFirst = (a == b);
        Console.WriteLine(compareFirst);
        bool compareSecond = (c == d);
        Console.WriteLine(compareSecond);
    }
}

4. Declare an integer variable and assign it with the value 254 in hexadecimal format. Use Windows Calculator to find its hexadecimal representation.
using System;

class HexadecimalFormat
{
    static void Main()
    {
        int firstValue = 0xFE;
        Console.WriteLine("{0:X}", firstValue);
    }
}

5. Declare a character variable and assign it with the symbol that has Unicode code 72. Hint: first use the Windows Calculator to find the hexadecimal representation of 72.
using System;

class DeclareUnicodeCode
{
    static void Main()
    {
        int decValue = 0x48;
        char symbol = (char)decValue;
        Console.WriteLine(symbol);
    }
}

6. Declare a boolean variable called isFemale and assign an appropriate value corresponding to your gender.
using System;

class IsFemale
{
    static void Main()
    {
        int male = 1;
        int female = 2;
        bool isFemale = (male < female);
        Console.WriteLine("My gender is male: {0}", isFemale);
    }
}

7. Declare two string variables and assign them with “Hello” and “World”. Declare an object variable and assign it with the concatenation of the first two variables (mind adding an interval). Declare a third string variable and initialize it with the value of the object variable (you should perform type casting).
using System;

class ObjectToString
{
    static void Main()
    {
        string firstWord = "Hello";
        string secondWord = "World";
        object combination = firstWord + " " + secondWord;
        string a = (string)combination;
        Console.WriteLine(a);
    }
}

8. Declare two string variables and assign them with following value:
The "use" of quotations causes difficulties.
Do the above in two different ways: with and without using quoted strings.
using System;

class Quotations
{
    static void Main()
    {
        string first = @"The ""use"" of quotations causes difficulties";
        string second = "The \"use\" of quotations causes difficulties";
        Console.WriteLine(first);
        Console.WriteLine(second);
    }
}

9. Write a program that prints an isosceles triangle of 9 copyright symbols ©. Use Windows Character Map to find the Unicode code of the © symbol. Note: the © symbol may be displayed incorrectly.
using System;

class CopyrightSymbol
{
    static void Main()
    {
        int copyrightSymbol = 0x00a9;
        char symbol = (char)copyrightSymbol;
        Console.WriteLine("  {0}", symbol);
        Console.WriteLine(" {0}{0}{0}", symbol);
        Console.WriteLine("{0}{0}{0}{0}{0}", symbol);
    }
}

10. A marketing firm wants to keep record of its employees. Each record would have the following characteristics – first name, family name, age, gender (m or f), ID number, unique employee number (27560000 to 27569999). Declare the variables needed to keep the information for a single employee using appropriate data types and descriptive names.
using System;

class Employees
{
    static void Main()
    {
        Console.WriteLine("Employee first name");
        string firstName = Console.ReadLine();
        Console.WriteLine("Employee family name");
        string familyName = Console.ReadLine();
        Console.WriteLine("Employee age:");
        byte age = byte.Parse(Console.ReadLine());
        Console.WriteLine("Employee gendder(f or m)");
        string gender = Console.ReadLine();
        Console.WriteLine("Employee ID Number");
        long idNumber = long.Parse(Console.ReadLine());
        Console.WriteLine("Unique employee number(27560000 to 27569999)");
        int employeeUniqueNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Employee full details:");
        Console.WriteLine("First Name: {0}", firstName);
        Console.WriteLine("Family Name: {0}", familyName);
        Console.WriteLine("Age: {0}", age);
        if (gender == "m")
        {
            Console.WriteLine("Gender: Male");
        }
        else if (gender == "f")
        {
            Console.WriteLine("Gender: Female");
        }
        else
        {
            Console.WriteLine("Wrong symbol");
        }
        Console.WriteLine("ID Nymber: {0}", idNumber);
        if (employeeUniqueNumber < 27560000)
        {
            Console.WriteLine("Wrong Number");
        }
        else if (employeeUniqueNumber > 27569999)
        {
            Console.WriteLine("Wrong Number");
        }
        else
        {
            Console.WriteLine("Employee Unique Number: {0}", employeeUniqueNumber);
        }
    }
}

11. Declare  two integer variables and assign them with 5 and 10 and after that exchange their values.
using System;

class TwoVariablesExchage
{
    static void Main()
    {
        Console.WriteLine("a=5");
        Console.WriteLine("b=10");
        int a = 5;
        int b = 10;
        int temp = a;
        a = b;
        b = temp;
        Console.WriteLine("a= {0}", a);
        Console.WriteLine("b= {0}", b);
    }
}

12. Find online more information about ASCII (American Standard Code for Information Interchange) and write a program that prints the entire ASCII table of characters on the console.
using System;

class AsciiCharacters
{
    static void Main()
    {
        int characters = 126;
        for (int i = 0; i <= characters; i++)
        {

            Console.Write((char)i);
        }
        Console.WriteLine();
    }
}

13. Create a program that assigns null values to an integer and to double variables. Try to print them on the console, try to add some values or the null literal to them and see the result.
using System;

class NullValues
{
    static void Main()
    {
        int? someInteger = null;
        double? someDouble = null;
        Console.WriteLine(someInteger);
        Console.WriteLine(someDouble);
    }
}

14. A bank account has a holder name (first name, middle name and last name), available amount of money (balance), bank name, IBAN, BIC code and 3 credit card numbers associated with the account. Declare the variables needed to keep the information for a single bank account using the appropriate data types and descriptive names.
using System;

class BankAccount
{
    static void Main()
    {
        Console.WriteLine("Enter your first name");
        string firstName = Console.ReadLine();
        Console.WriteLine("Enter your midle name");
        string midleName = Console.ReadLine();
        Console.WriteLine("Ënter your last name");
        string lastName = Console.ReadLine();
        Console.WriteLine("Enter your amount of money");
        decimal amount = decimal.Parse(Console.ReadLine());
        Console.WriteLine("Enter your bank name");
        string bankName = Console.ReadLine();
        Console.WriteLine("Enter your IBAN");
        string iban = Console.ReadLine();
        Console.WriteLine("Enter your BIC code");
        string bic = Console.ReadLine();
        Console.WriteLine("Enter your first credit card number");
        long firstCardNumber = long.Parse(Console.ReadLine());
        Console.WriteLine("Enter your second credit card number");
        long secondCardNumber = long.Parse(Console.ReadLine());
        Console.WriteLine("Enter your third credit card number");
        long thirdCardNumber = long.Parse(Console.ReadLine());
        Console.WriteLine("Your Bank Account information:");
        Console.WriteLine("First Name: {0}", firstName);
        Console.WriteLine("Middle Name: {0}", midleName);
        Console.WriteLine("Last Name: {0}", lastName);
        Console.WriteLine("Amount of money: {0}", amount);
        Console.WriteLine("Bank Name: {0}", bankName);
        Console.WriteLine("IBAN: {0}", iban);
        Console.WriteLine("BIC: {0}", bic);
        Console.WriteLine("First credit card number: {0}", firstCardNumber);
        Console.WriteLine("Second credit card number: {0}", secondCardNumber);
        Console.WriteLine("Third credit card number: {0}", thirdCardNumber);
    }
}

Няма коментари:

Публикуване на коментар