מיני פרויקט חלונות תרגיל 1

benny#C, NET.Leave a Comment


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

namespace lub1
{
    enum Options
    {
        Guess, Range, Exit
    }
    class Program
    {
        static void Swap<T>(ref T lhs, ref T rhs)    
        {
            T temp;
            temp = lhs;
            lhs = rhs;
            rhs = temp;
        }


        static void Main(string[] args)  // main function
        {
            Console.WriteLine("welcome to nerds games \nfor array game nember press 0:\nfor magic squwre press 1:\nfor exit press 2");
            string choice = Console.ReadLine();  // choice input
            int x = Convert.ToInt32(choice);  //  convert choice to int for switch

            switch (x)
            {
                case 0:
                    guess_number();
                    break;
                case 1:
                    magicSquwre();
                    break;
                case 2:
                    return;
                
            }

            Console.ReadKey();
        }

        private static void magicSquwre()   // magic Squwre function
        {
            int[,] magicSquwre = new int[5, 5];
            Console.WriteLine("please full 5 rows, 5 numbers for each row");
            String input;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    input = Console.ReadLine();
                    magicSquwre[i, j] = Convert.ToInt32(input);  // initialize the 2d array
                }
            }


            bool chack = true;
            int sum = 0, sum2 = 0;

            for (int i = 0; i < 5; i++)
            {
                sum += magicSquwre[0, i];   // sum of the first row for compare
            }

            for (int i = 1; i < 5; i++)     // chack rows
            {
                for (int j = 0; j < 5; j++)
                {
                    sum2 += magicSquwre[i, j];
                }
                if (sum != sum2)        // compare to the first row
                {
                    chack = false;
                    break;
                }
                sum2 = 0;
            }
            if (chack == false)
            {
                goto lable;
            }
            for (int i = 0; i < 5; i++)     // chack coloms
            {
                for (int j = 0; j < 5; j++)
                {
                    sum2 += magicSquwre[j, i];
                }
                if (sum != sum2)  // compare to the first row
                {
                    chack = false;
                    break;
                }
                sum2 = 0;
            }

            if (chack == false)
            {
                goto lable;
            }

            for (int i = 0; i < 5; i++)     // Main diagonal
                sum2 += magicSquwre[i, i];

            if (sum != sum2)   // compare to the first row
            {
                chack = false;
                goto lable;
            }

            sum2 = 0;


            for (int i = 0; i < 5; i++)     // second diagonal
            {
                for (int j = 4; j >= 0; j--)
                {
                    sum2 += magicSquwre[i, j];
                }
                if (sum != sum2)
                {
                    chack = false;
                    break;
                }
                sum2 = 0;
            }

        lable: if (chack == true)
                Console.WriteLine("this is a magic Squwre");
            else
                Console.WriteLine("this is not magic Squwre");
        }

        private static void guess_number()    // array function
        {
            int[] intArray = new int[100];
            Random r = new Random();
            for (int i = 0; i < 100; i++)
            {
                intArray[i] = r.Next(0, 1000);  // initialize the array with random numbers

            }

            
            int x;
            Console.WriteLine("for guess please press 0:\nfor range please press 1\nfor exit please press  2");
            string choice = Console.ReadLine();


            bool b = Int32.TryParse(choice, out x);

            switch (x)
            {
                case 0:
                    {
                        Console.WriteLine("please enter your guess number: ");
                        int y;
                        bool flag = false;
                        choice = Console.ReadLine();
                        b = Int32.TryParse(choice, out y);

                        
                        flag = intArray.Contains(y);

                        if (!flag)
                            Console.WriteLine("your number not found");
                        else
                            Console.WriteLine("your number found");
                            
                        

                    }
                    break;

                case 1:

                    Console.WriteLine("please enter your range and guess");
                    int lowRange, highRange, guess;

                    choice = Console.ReadLine();
                    string[] str = choice.Split(',');
                    lowRange = Convert.ToInt32(str[0]);
                    highRange = Convert.ToInt32(str[1]);
                    guess = Convert.ToInt32(str[2]);

                    if (lowRange > highRange)
                    {
                        Swap<int>(ref lowRange, ref highRange);
                    }

                    int sum = 0;
                    foreach (var item in intArray)
                    {
                        if (item >= lowRange && item <= highRange)
                            sum++;
                    }

                    if (sum == guess)
                        Console.WriteLine("your guess is correct !!!");
                    else
                        Console.WriteLine("your guess is not correct !!!");
                    break;
                case 2:
                    break;
            }
        }
    }
    
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *