C# – Operators And Expressions

Условия и решения на задачите от лекция „Operators and expressions“

Задача 1. Odd Or Even Check

Условие: Write an expression that checks if given integer is odd or even.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.OddOrEvenCheck
{
    class Program
    {
        static void Main()
        {
                int number;
                Console.Write("Enter the number:");
                bool isInt = int.TryParse(Console.ReadLine(),out number);
                if (isInt)
                {
                    if (number % 2 == 0)
                    {
                        Console.WriteLine("The number is even");
                    }
                    else
                    {
                        Console.WriteLine("The number is odd");
                    }
                }
                else
                {
                    Console.WriteLine("Not a valid entry!");
                }
        }
    }
}

Задача 2. Check For Devision Without Reminder

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2.CheckForDevisionWithoutReminder
{
    class Program
    {
        static void Main()
        {
            bool check=false;
            int number;
            Console.Write("Enter the number:");
            bool isInt = int.TryParse(Console.ReadLine(),out number);

            if (isInt)
            {

                if (number % 7 == 0 && number % 5 == 0)
                {
                    check = true;
                }
                Console.WriteLine("The result of the check is:{0}", check);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 3. Calculation Of Rectange’s Area

Условие: Write an expression that calculates rectangle’s area by given width and height.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.CalculationOfRectange_sArea
{
    class Program
    {
        static void Main()
        {
            double height, wight;
            Console.Write("Enter the height of the rectange:");
            bool isValidHeight = double.TryParse(Console.ReadLine(),out height);
            Console.Write("Enter the wight of the rectange:");
            bool isValidWight = double.TryParse(Console.ReadLine(),out wight);
            if (isValidHeight && isValidHeight)
            {
                double area = height * wight;
                Console.WriteLine("The area of the rectange is:{0}", area);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 4. Check If Third Digit Is 3

Условие: Write an expression that checks for given integer if its third digit (right-to-left) is 7. E. g. 1732 => true.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4.CheckIfThirdDigitIs3
{
    class Program
    {
        static void Main()
        {
            bool check = false;
            Console.Write("Enter the number:");
            int number;
            bool isInt = int.TryParse(Console.ReadLine(), out number);
            if (isInt)
            {
                if (number % 1000 > 699 && number % 1000 < 800)
                {
                    check = true;
                }
                Console.WriteLine("Is the third digit equal to 7?:{0}", check);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 5. Check If Bit 3 Is 1 Or 0

Условие: Write a boolean expression for finding if the bit 3 (counting from 0) of a given integer is 1 or 0.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _5.CheckIfBit3Is1Or0
{
    class Program
    {
        static void Main()
        {
            bool is3Digit1 = false;
            Console.Write("Enter the number:");
            int number;
            bool isNumber = int.TryParse(Console.ReadLine(),out number);
            if (isNumber)
            {
                if ((number & 8) == 8)
                {
                    is3Digit1 = true;
                }
                Console.WriteLine("Is the third digit equal to 1?:{0}", is3Digit1);
            }
            else 
            {
                Console.WriteLine("Not a valid entry!");           
            }
        }
    }
}

Задача 6. Is The Point Within The Circle

Условие: Write an expression that checks if given point (x, y) is within a circle K(O, 5).

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _6.IsThePointWithinTheCircle
{
    class Program
    {
        static void Main()
        {
            bool check = false;
            double X, Y;
            Console.Write("Enter X:");
            bool Xcheck = double.TryParse(Console.ReadLine(),out X);
            Console.Write("Enter Y:");
            bool Ycheck = double.TryParse(Console.ReadLine(),out Y);
            if (Xcheck && Ycheck)
            {
                if ((X * X + Y * Y) <= 25)
                {
                    check = true;
                }
                Console.WriteLine("Is the point ({0},{1}) within the circle?:{2}", X, Y, check);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 7. Check If A Number Is Prime

Условие: Write an expression that checks if given positive integer number n (n ≤ 100) is prime. E.g. 37 is prime.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _7.CheckIfANumberIsPrime
{
    class Program
    {
        static void Main()
        {
            bool isPrime =true;
            int number;
            Console.Write("Enter the number:");
            bool isInt = int.TryParse(Console.ReadLine(), out number);

            if (isInt)
            {
                for (int i = 2; i <= Math.Sqrt(number); i++)
                {
                    if (number % i == 0)
                    {
                        isPrime = false;
                    }
                }
                Console.WriteLine("Is this a prime number?:{0}", isPrime);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 8. Trapezoid Area Calculation

Условие: Write an expression that calculates trapezoid’s area by given sides a and b and height h.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _8.TrapezoidAreaCalculation
{
    class Program
    {
        static void Main()
        {
            double a, b,h;
            Console.Write("Enter the first side of the trapezoid:");
            bool isValid_a = double.TryParse(Console.ReadLine(),out a);
            Console.Write("Enter the second side of the trapezoid:");
            bool isValid_b = double.TryParse(Console.ReadLine(),out b);
            Console.Write("Enter the height of the trapezoid:");
            bool isValid_h = double.TryParse(Console.ReadLine(),out h);

            if (isValid_a && isValid_b && isValid_h)
            {
                double area = (a + b)*0.5*h;
                Console.WriteLine("The area of the rectange is:{0}", area);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 9. Is The Point In The Circle – Out Of Rectange

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _9.IsThePointInTheCircle_OutOfRectange
{
    class Program
    {
        static void Main()
        {
            bool check = false;
            double X, Y;
            Console.Write("Enter X:");
            bool Xcheck = double.TryParse(Console.ReadLine(), out X);
            Console.Write("Enter Y:");
            bool Ycheck = double.TryParse(Console.ReadLine(), out Y);
            if (Xcheck && Ycheck)
            {
                if ((X >= -1 && Y >= -1) && (X <= 5 && Y <= 1))
                {
                 //Do nothing   
                }
                else
                {
                    double displacedX = X - 1;
                    double displacedY = Y - 1;
                    if ((displacedX * displacedX + displacedY * displacedY) <= 9)
                    {
                        check = true;
                    }
                }
                Console.WriteLine("Is the point ({0},{1}) within the circle and out of the rectangle?:{2}", X, Y, check);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
            
        }
    }
}

Задача 10. Check If a Bit In Int Is 0 or 1

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _10.CheckIfaBitInIntIs0or1
{
    class Program
    {
        static void Main()
        {
            bool isDigit1 = false;
            byte p;
            int v;
            Console.Write("Enter the position of the bit p:");
            bool ispByte = byte.TryParse(Console.ReadLine(), out p);
            Console.Write("Enter the integer number v:");
            bool isvInt = int.TryParse(Console.ReadLine(), out v);

            if (ispByte&&isvInt&&p<32)
            {
                int mask = 1 << p;
                if ((v & mask) == mask)
                {
                    isDigit1 = true;
                }
                Console.WriteLine("Is bit{0} of intiger {1} equal to 1?:{2}",p,v,isDigit1);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 11. Find In A Given Value Given Bit Number

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _11.FindInAGivenValueGivenBitNumber
{
    class Program
    {
        static void Main()
        {
            byte value = 0;
            int i;
            byte b;
            Console.Write("Enter the integer number i:");
            bool isiInt = int.TryParse(Console.ReadLine(), out i);
            Console.Write("Enter the position of the bit b:");
            bool isbByte = byte.TryParse(Console.ReadLine(), out b);

            if (isiInt && isbByte&&b<32)
            {
                int mask = 1 << b;
                if ((i & mask) == mask)
                {
                    value = 1;
                }
                Console.WriteLine("value={0}",value);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 12. Change Bit On Given Position

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _12.ChangeBitOnGivenPossition
{
    class Program
    {
        static void Main()
        {
            byte value,p;
            int n;
            Console.Write("Enter the integer number n:");
            bool isnInt = int.TryParse(Console.ReadLine(), out n);
            Console.Write("Enter the value of the bit:");
            bool isValueByte = byte.TryParse(Console.ReadLine(), out value);
            Console.Write("Enter the position of the bit p:");
            bool ispByte = byte.TryParse(Console.ReadLine(), out p);

            if (isnInt && isValueByte&&ispByte&&value<2&&p<32)
            {
                
                int mask = 1 << p;
                if (value == 1)
                {
                    n = n | mask;
                }
                else
                {
                    if ((n & mask) == mask)
                    {
                        n=n ^ mask;
                    }
                }
                Console.WriteLine("n={0}", n);
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 13. 3 Bits Exchange

Условие: Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.

Решение:

using System;

namespace _13._3BitsExchange
{
    class Program
    {
        static void Main()
        {
            uint n;
            Console.Write("Enter the unsigned integer number n:");
            bool isnInt = uint.TryParse(Console.ReadLine(), out n);
            if (isnInt)
            {
                Console.WriteLine("binary initial n:");
                Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));

                n = ((~(7u << 24 | 7u << 3)) & n) | (((n & (7u << 3)) << 21) | ((n & (7u << 24)) >> 21));//Swap bits 3,4,5 with 24,26,26

                Console.WriteLine("binary new n:");
                Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Задача 14. Bits Exchange

Условие: 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;

namespace _14.BitsExchange
{
    class Program
    {
        static void Main()
        {
            uint n;
            byte p, q, k;
            Console.Write("Enter the unsigned integer number n:");
            bool isnInt = uint.TryParse(Console.ReadLine(), out n);
            Console.Write("Enter the start position of the first bit sequence p:");
            bool ispByte = byte.TryParse(Console.ReadLine(), out p);
            Console.Write("Enter the start position of the second bit sequence q:");
            bool isqByte = byte.TryParse(Console.ReadLine(), out q);
            Console.Write("Enter the lenght of the sequence k:");
            bool iskByte = byte.TryParse(Console.ReadLine(), out k);

            if (isnInt&ispByte&isqByte&iskByte)
            {
                if ((p + k) < 31 && (q + k) < 31 && (Math.Abs(p - q) >= k))
                {
                    if (p > q)
                    {
                        byte temp = q;
                        q = p;
                        p = temp;
                    }
                    Console.WriteLine("binary initial n:");
                    Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));

                    n = ((~(((uint)Math.Pow(2, k) - 1) << q | ((uint)Math.Pow(2, k) - 1) << p)) & n) | (((n & (((uint)Math.Pow(2, k) - 1) << p)) << (Math.Abs(p-q))) | ((n & (((uint)Math.Pow(2, k) - 1) << q)) >> (Math.Abs(p-q))));//Swap bits p with bits q
                    Console.WriteLine("binary new n:");
                    Console.WriteLine(Convert.ToString(n, 2).PadLeft(32, '0'));
                }
                else
                {
                    Console.WriteLine("Not a valid entry!");
                }
            }
            else
            {
                Console.WriteLine("Not a valid entry!");
            }
        }
    }
}

Оставете коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *