مهلت ارسال پاسخ به چالش تمام شد و این هم کد من برای جواب چالش ترک عملگر ها

 

 

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

namespace multiplication
{
    //multiplication whithout using any operator (boolean operators (&& || !) are permited).
    //solved by Behnam Simjoo on 2019/05/10
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Clear();
                Console.Write("a=");
                int a = int.Parse(Console.ReadLine());
                Console.Write("b=");
                int b = int.Parse(Console.ReadLine());
                int res = f(a, b);
                Console.WriteLine($"---------------------\nResult: {res}");
                Console.ReadKey();
            }
        }
        static int f(int a,int b)
        {
            if (a < b) { int t = a;  a = b;  b = t; }  //for more speed
            int temp = a;
            for(int i = 1; i < b; i=Sum(i,1))  //using Sum function in place of ++ operation
                a = Sum(a, temp);
            return a;
        }
        static int Sum (int a,int b)
        {
            bool[] ab = Convert.ToString(a, 2).PadLeft(32, '0').Select(s=>s=='1').ToArray();
            bool[] bb = Convert.ToString(b, 2).PadLeft(32, '0').Select(s=>s=='1').ToArray();
            //converting int to bin string and converting 0 to false and 1 to true
            int carry = 0,result=0;
            do {
                carry = 0;
                result = 0;
                bool[] r = new bool[32];
                List<bool> c = new List<bool>(32);
                r=ab.Select((s, i) => s && bb[i] ? false : (s || bb[i])).ToArray();     //Boolean sum without carry
                c.AddRange(ab.Select((s, i) =>s && bb[i]).ToArray());                   //Boolean sum calcuting carry

                // a | b || result of a+b | carry
                //---|---||---------------|------
                // 0 | 0 ||       0       |   0
                // 1 | 0 ||       1       |   0
                // 0 | 1 ||       1       |   0
                // 1 | 1 ||       0       |   1

                //     a=19=00010011|
                //      +            }=> r=00001010 , c=00010001
                //     b=25=00011001|
                //       -----------
                //result=44=00101100
                //shifting c to left
                c.Add(false);
                c.RemoveAt(0);
                // c=00010001 => shift => c=00100010
                ab = c.ToArray();
                bb = r;
                //seting ab = carry and bb = r to continue sum process until carry became zero
                c.Reverse();
                r = r.Reverse().ToArray();
                //reversing boolarrays for BitArray
                Array A = new int[2];
                BitArray res = new BitArray(r);
                res.CopyTo(A, 0);
                BitArray cry = new BitArray(c.ToArray());
                cry.CopyTo(A, 1);
                carry = (int)A.GetValue(1);
                result = (int)A.GetValue(0);
                //converting binary to single integer value
                //then cuntinue process until carry != 0
                //1st next step: r=00001010, c=00100010 => r+c => r=00101000, c=00000100
                //2nd next step: r=00101000, c=00000100 => r+c => r=00101100, c=00000000 => result = 44
            } while (carry != 0);
            return result;
        }
    }
}

 


مشخصات

تبلیغات

محل تبلیغات شما

آخرین مطالب این وبلاگ

محل تبلیغات شما محل تبلیغات شما

آخرین وبلاگ ها

برترین جستجو ها

آخرین جستجو ها

پخش عمده لباس زیر سنگ کاری نمای ساختمان.۹۶۲۱۹۸۲_۰۹۱۹ مجله اينترنتي طراحي سايت شیما علی زاده سکوت بی پایان آس دل کارشناسی ارشد و دکتری زمین شناسی شبنم خیال