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

C#: Operators, Expressions and Statements

Ето ги и домашните от лекцията Operators, Expressions and Statements. Темата не е от най-лесните и в същото време е доста важна. На мен лично побитовеите операции ми отнеха доста време докато започна да се справям с тях. Видео и материали за лекцията може да се намерят тук.

1. Write an expression that checks if given integer is odd or even.
using System;

class OddOrEven
{
    static void Main()
    {
        Console.WriteLine("Enter one Number:");
        int number = int.Parse(Console.ReadLine());
        int reminder = number % 2;
        if (reminder == 0)
        {
            Console.WriteLine("The number is even");
        }
        else
        {
            Console.WriteLine("The number is odd");
        }
    }
}

2. Write a boolean expression that checks for given integer if it can be divided (without remainder) by 7 and 5 in the same time.
using System;

class DivideByFiveAndSeven
{
    static void Main()
    {
        Console.WriteLine("Enter one number:");
        int number = int.Parse(Console.ReadLine());
        int fiveReminder = number % 5;
        int sevenReminder = number % 7;
        if (fiveReminder == 0 && sevenReminder == 0)
        {
            Console.WriteLine("The number can be divided to 5 and to 7 at the same time");
        }
        else
        {
            Console.WriteLine("The number can't be divided to 5 and to 7 at the same time");
        }
    }
}

3. Write an expression that calculates rectangle’s area by given width and height.
using System;

class RectangleArea
{
    static void Main()
    {
        Console.WriteLine("Enter the width of the rectangle:");
        float width = float.Parse(Console.ReadLine());
        Console.WriteLine("Enter the height of the rectangle");
        float height = float.Parse(Console.ReadLine());
        float area = width * height;
        Console.WriteLine("Area = {0}", area);
    }
}

4. Write an expression that checks for given integer if its third digit (right-to-left) is 7. E. g. 1732 -> true.
using System;

class CheckThirdDigit
{
    static void Main()
    {
        Console.WriteLine("Enter a number to check (minimum 3 digits)");
        int number = int.Parse(Console.ReadLine());
        number /= 100;
        number %= 10;
        if (number == 7)
        {
            Console.WriteLine("The third digit from right to left is 7");
        }
        else
        {
            Console.WriteLine("The third digit from right to left is not 7");
        }
    }
}

5. Write a boolean expression for finding if the bit 3 (counting from 0) of a given integer is 1 or 0.
using System;

class FindingThirdBit
{
    static void Main()
    {
        Console.WriteLine("Enter one number: ");
        int number = int.Parse(Console.ReadLine());
        int bitMove = number >> 3;
        int reminder = bitMove % 2;
        if (reminder == 0)
        {
            Console.WriteLine("Third bit is 0");
        }
        else
        {
            Console.WriteLine("Third bit is 1");
        }
        //Second variant
        int mask = 1;
        mask <<= 3;
        if ((mask & number) > 0)
        {
            Console.WriteLine("Third bit is 1");
        }
        else
        {
            Console.WriteLine("Third bit is 0");
        }
    }
}

6. Write an expression that checks if given point (x,  y) is within a circle K(O, 5).
using System;

class PointInCircle
{
    static void Main()
    {
        double radius = 5;
        double area = Math.PI * Math.Sqrt(radius);
        Console.WriteLine("Enter a value for 'x': ");
        double x = double.Parse(Console.ReadLine());
        Console.WriteLine("Enter a value for 'y': ");
        double y = double.Parse(Console.ReadLine());
        if ((x * x + y * y) <= radius * radius)
        {
            Console.WriteLine("The point is inside the Circle");
        }
        else
        {
            Console.WriteLine("The point is outside the Circle");
        }
    }
}

7. Write an expression that checks if given positive integer number n (n <= 100) is prime. E.g. 37 is prime.
using System;

class PrimeNumbers
{
    static void Main()
    {
        Console.WriteLine("Enter a number between 1 and 100: ");
        byte number = byte.Parse(Console.ReadLine());
        if (number == 2 | number == 3 | number == 5 | number == 7 | number == 11 | number == 13 | number == 17 | number == 19 | number == 23 | number == 29 | number == 31 | number == 37 | number == 41 | number == 43 | number == 47 | number == 53 | number == 59 | number == 61 | number == 67 | number == 71 | number == 73 | number == 79 | number == 83 | number == 89 | number == 97)
        {
            Console.WriteLine("The number is prime");
        }
        else
        {
            Console.WriteLine("The number is not prime");
        }
    }
}

8. Write an expression that calculates trapezoid's area by given sides a and b and height h.
using System;

class TrapezoidsArea
{
    static void Main()
    {
        Console.WriteLine("Enter a number for side 'a': ");
        float a = float.Parse(Console.ReadLine());
        Console.WriteLine("Enter a number for side 'b'");
        float b = float.Parse(Console.ReadLine());
        Console.WriteLine("Enter a number for the height 'h'");
        float h = float.Parse(Console.ReadLine());
        float area = ((a + b) * h) / 2;
        Console.WriteLine("The area is equal to: {0}", area);
    }
}

9. Write an expression that checks for given point (x, y) if it is within the circle K( (1,1), 3) and out of the rectangle R(top=1, left=-1, width=6, height=2).
using System;

class PointInCircleAndOutRectangle
{
    static void Main()
    {
        double r = 3;
        Console.WriteLine("Enter a value for 'x': ");
        double x = double.Parse(Console.ReadLine());
        Console.WriteLine("Enter a value for 'y': ");
        double y = double.Parse(Console.ReadLine());
        if ((((x - 1) * (x - 1) + (y - 1) * (y - 1)) <= r * r) && !((x >= -1 && x <= 4) && (y <= 1 && y >= -1)))
        {
            Console.WriteLine("The point is inside the Circle and Outside Rectangle");
        }
        else
        {
            Console.WriteLine("The point is outside the Circle and Outside the Ractangle");
        }
    }
}

10. Write a boolean expression that returns if the bit at position p (counting from 0) in a given integer number v has value of 1. Example: v=5; p=1 -> false.
using System;

class CheckABitAtPosition
{
    static void Main()
    {
        Console.WriteLine("Enter a numner to check:");
        int number = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter a bit position to check if it is 1:");
        int position = int.Parse(Console.ReadLine());
        int check = 1;
        int mask = check << position;
        Console.WriteLine((mask & number) == 0 ? false : true);
    }
}

11. Write an expression that extracts from a given integer i the value of a given bit number b. Example: i=5; b=2 -> value=1.
using System;

class ValueOfBit
{
    static void Main()
    {
        Console.WriteLine("Enter a numner to check:");
        int number = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter a bit position to checked");
        int position = int.Parse(Console.ReadLine());
        int check = 1;
        int mask = check << position;
        Console.WriteLine((mask & number) != 0 ? 1 : 0);
    }
}

12. We are given integer number n, value v (v=0 or 1) and a position p. Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n.
Example: n = 5 (00000101), p=3, v=1 -> 13 (00001101)
n = 5 (00000101), p=2, v=0 -> 1 (00000001)
using System;

class ModifyBit
{
    static void Main()
    {
        Console.WriteLine("Enter one number number: ");
        int number = int.Parse(Console.ReadLine());
        string binRepresentation = Convert.ToString(number, 2).PadLeft(8, '0');
        Console.WriteLine(binRepresentation);
        Console.WriteLine("Enter a position of the bit to be modified: ");
        int position = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter a binary number (0 or 1): ");
        int bit = int.Parse(Console.ReadLine());
        int mask = 1 << position;
        int convertedNumber = 0;
        if (bit == 1)
        {
            convertedNumber = number | mask;
        }
        else
        {
            convertedNumber = number ^ mask;
        }
        Console.WriteLine("New number: {0}" + convertedNumber);
    }
}

13. Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.
using System;

class BitsExchange
{
    static void Main()
    {
        Console.WriteLine("Enter a number to be modified:");
        uint number = uint.Parse(Console.ReadLine());
        uint mask3 = (number & 1 << 3);
        uint bit3 = mask3 >> 3;
        uint mask4 = (number & 1 << 4);
        uint bit4 = mask4 >> 4;
        uint mask5 = (number & 1 << 5);
        uint bit5 = mask5 >> 5;
        uint mask24 = (number & 1 << 24);
        uint bit24 = mask24 >> 24;
        uint mask25 = (number & 1 << 25);
        uint bit25 = mask25 >> 25;
        uint mask26 = (number & 1 << 26);
        uint bit26 = mask26 >> 26;
        uint result;
        uint tempResult;
        if (bit3 == 0)
        {
            tempResult = number & ((uint)(1 << 24));
        }
        else
        {
            tempResult = number | 1 << 24;
        }
        result = tempResult;
        if (bit4 == 0)
        {
            tempResult = result & ~((uint)(1 << 25));
        }
        else
        {
            tempResult = result | 1 << 25;
        }
        result = tempResult;
        if (bit5 == 0)
        {
            tempResult = result & ~((uint)(1 << 26));
        }
        else
        {
            tempResult = result | 1 << 26;
        }
        result = tempResult;
        if (bit24 == 0)
        {
            tempResult = result & ~((uint)(1 << 3));
        }
        else
        {
            tempResult = result | 1 << 3;
        }
        result = tempResult;
        if (bit25 == 0)
        {
            tempResult = result & ~((uint)(1 << 4));
        }
        else
        {
            tempResult = result | 1 << 4;
        }
        result = tempResult;
        if (bit26 == 0)
        {
            tempResult = result & ~((uint)(1 << 5));
        }
        else
        {
            tempResult = result | 1 << 5;
        }
        result = tempResult;
        Console.WriteLine("Number before {0} , binary before {1}", number, Convert.ToString(number, 2).PadLeft(32, '0'));
        Console.WriteLine("Number after {0} , binary after {1}", result, Convert.ToString(result, 2).PadLeft(32, '0'));
    }
}

14. * Write a program that exchanges bits {p, p+1, …, p+k-1) with bits {q, q+1, …, q+k-1} of given 32-bit unsigned integer.
using System;

class BitsExchange2
{
    static void Main()
    {
        Console.WriteLine("Enter a number to be modified:");
        int number = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the first bit position of next three to be modified:");
        int firstPosition = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the first bit position of the next three to be switched:");
        int lastPosition = int.Parse(Console.ReadLine());
        int maskP = (number & 1 << firstPosition);
        int bitP = maskP >> firstPosition;
        int maskPPlusOne = (number & 1 << (firstPosition + 1));
        int bitPPlusOne = maskPPlusOne >> (firstPosition + 1);
        int maskPPlusTwo = (number & 1 << (firstPosition + 2));
        int bitPPlusTwo = maskPPlusTwo >> (firstPosition + 2);
        int maskPLast = (number & 1 << lastPosition);
        int bitPLast = maskPLast >> lastPosition;
        int maskPLastPlusOne = (number & 1 << (lastPosition + 1));
        int bitPLastPlusOne = maskPLastPlusOne >> (lastPosition + 1);
        int maskPLastPlusTwo = (number & 1 << (lastPosition + 2));
        int bitPLastPlusTwo = maskPLastPlusTwo >> (lastPosition + 2);
        int result;
        int tempResult;
        if (bitP == 0)
        {
            tempResult = number & ((int)(1 << lastPosition));
        }
        else
        {
            tempResult = number | 1 << lastPosition;
        }
        result = tempResult;
        if (bitPPlusOne == 0)
        {
            tempResult = result & ~((int)(1 << (lastPosition + 1)));
        }
        else
        {
            tempResult = result | 1 << (lastPosition + 1);
        }
        result = tempResult;
        if (bitPPlusTwo == 0)
        {
            tempResult = result & ~((int)(1 << (lastPosition + 2)));
        }
        else
        {
            tempResult = result | 1 << (lastPosition + 2);
        }
        result = tempResult;
        if (bitPLast == 0)
        {
            tempResult = result & ~((int)(1 << firstPosition));
        }
        else
        {
            tempResult = result | 1 << firstPosition;
        }
        result = tempResult;
        if (bitPLastPlusOne == 0)
        {
            tempResult = result & ~((int)(1 << (firstPosition + 1)));
        }
        else
        {
            tempResult = result | 1 << (firstPosition + 1);
        }
        result = tempResult;
        if (bitPLastPlusTwo == 0)
        {
            tempResult = result & ~((int)(1 << (firstPosition + 2)));
        }
        else
        {
            tempResult = result | 1 << (firstPosition + 2);
        }
        result = tempResult;
        Console.WriteLine("Number before {0} , binary before {1}", number, Convert.ToString(number, 2).PadLeft(32, '0'));
        Console.WriteLine("Number after {0} , binary after {1}", result, Convert.ToString(result, 2).PadLeft(32, '0'));
    }
}

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

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