/*

 * Created by SharpDevelop.

 * User: 3th

 * Date: 7/25/2002

 * Time: 6:50 PM

 *

 * To change this template use Tools | Options | Coding | Edit Standard Headers.

 */

namespace System.Collections

{

      public interface IComparable

      {

            int CompareTo(object o);

      }

}

namespace System.Collections

{

 using System;

 public class longint:IComparable

 {

     

      public void div(string s,out byte B,out string str)  //output B,arr1

      {

            int y;

            int x=(int)s[0]-'0';

            int i=1;

            int m=10;

            str="";

            while((i<3)&&(i<s.Length))

            {

                  x=x*m+((int)s[i++]-'0');         //x=s[0]s[1]s[2]

            }

            if(i==s.Length)

            {

                  y=x/Base;

             str=str+y.ToString();

             x=x-(y*Base);

            }

                 

            while(i<s.Length)

            {

            if((x<Base)&&(i<s.Length))

            {

                  x=x*m+((int)s[i++]-'0');

            }

             y=x/Base;

             str=str+y.ToString();

             x=x-(y*Base);

            }

            B=(byte)x;//Console.WriteLine("{0}   {1}",str,B);

                 

      }

      //*********************************************************

      //this function to add two string and return string

     

      public string Add( ref string str1,string str2,ref int c)     //c input when privause string have carry

      {

            int temp;//int l;

            string str="";

            if(str1.Length<str2.Length)                     //two string must have same length

            {

                 

            while(str1.Length!=str2.Length)

                  {

                        str1='0'+str1;//Console.WriteLine("{0}       {1}",str1.Length,str2.Length);

                  }

            }

            if(str1.Length>str2.Length)

            {

                  while(str1.Length!=str2.Length)

                  {

                        str2='0'+str2;//Console.WriteLine(str1);

                  }

            }

         

            //***************

          /*if(str2.Length==1)

            {

            if(str1.Length<str2.Length)                     //two string must have same length

            {

                  for(int i=0;i<(str2.Length-str1.Length);i++)

                  {

                        str1='0'+str1;

                  }

            }

            if(str2.Length!=str1.Length)

            {

                  for(int i=0;i<(str1.Length-str2.Length);i++)

                  {

                        str2='0'+str2;

                  }

            }

            }*/

            //*******************

            //Console.WriteLine("{0}          {1}",str1,str2);

            for(int i=str2.Length-1;i>=0;i--)              //add char by char for str1and str2

            {

                  temp=str1[i]-'0'+str2[i]-'0'+c;            //c is carry

                  c=0;

                  if(temp>=10)

                  {

                        c=1;

                        temp=temp-10;

                  }

                  str=temp.ToString()+str;

                  //Console.WriteLine("{0}        {1}           {2}        {3}",str1[i],str2[i],temp,c);

            }

            //Console.WriteLine(str);

            return str;

                 

           

      }

      //*********************************************************

      public byte[] mul(byte[] arr1,byte x,int offset)               //this function to mul one of byte in bases inarray of byte ,offset number of zero

      {                                                             //ex: B3  B2  B1   B0

       int c=0;int i; byte[] newarr=new byte[60];                         //                 B0

       for(i= 0;i<arr1.Length;i++)                               //   ________________

       {                                                            //.....B0B2 B1B0 B0B0

         int y=(x*arr1[i])+c;                                        //ex: B3  B2  B1   B0

         c=0;                                                       //            B1    

         if(y>Base)                                                 //   ________________

         {                                                          //..B1B2 B1B1 B0B1 0    and so on

             c=y/Base;

             y=y%Base;

         }

         newarr[i+offset]=(byte)y;

       }

        newarr[i+offset]=(byte)c;

           

       return (newarr);

      }

       //**********************************************************

      const int Base=256;

      public byte[] arr;

      public string str;

      //***********************************************************

      public longint(string s)

      {

       byte b;

       int i=0;

     string str1="";

       this.str=s;

       arr=new byte[30];        //this for only string length  155 char

       div(s,out b,out str1);

       arr[i++]=b;

       while((str1!="0")&&(i<30))

       {

        div( str1 ,out b,out str1);

        arr[i++]=b;

       }

      }

      //**********************************************************

      public longint(byte[] arr1)

      {

            this.arr=new byte[30];

            for(int i=0;i<arr.Length;i++)

            {

                this.arr[i]=arr1[i];

            }

      }

      //**********************************************************

      public override string ToString()

      {

         int i=29;string str="";

         while(arr[i--]==0)  

         {}

         i=i+1;

         while(i>=0)

            str=str+"   "+arr[i--].ToString();

            return str;

      }

      public longint Append(int digit)

      {

            string s="";

            s=str+digit.ToString();

            return new longint(s);

      }

      public longint AppendAlongint(longint L)

      {

            string s="";

            s=this.str.ToString()+L.str.ToString();

            return new longint(s);

      }

      public longint Prepend(int digit)

      {

            string s="";

            s=digit.ToString()+str;

            return new longint(s);

      }

      public longint PrependAlongint(longint L)

      {

            string s="";

            s=L.str.ToString()+this.str.ToString();

            return new longint(s);

      }

     

//*********************************************************************

public static longint operator+(longint lhs,longint rhs)

{

      int c=0;int x;

      byte[] newarr=new byte[30];

      for(int i=0;i<lhs.arr.Length;i++)

      {

            x=lhs.arr[i]+rhs.arr[i]+c;

            c=0;

            if(x>=Base)

            {

                  x=x-Base;

                  c=1;

            }

            newarr[i]=(byte)x;

      }

      return new longint(newarr);

}

//**********************************************************************

public static longint operator-(longint lhs,longint rhs)

{

      if(lhs>rhs)

      {

      int b=0;int r=0;

      byte[] newarr=new byte[30];

      for(int i=0;i<lhs.arr.Length;i++)

      {

            b=0;int z=r;

            if(lhs.arr[i]-r<rhs.arr[i])

            b=Base;

            newarr[i]=(byte)(lhs.arr[i]+b-r-rhs.arr[i]);

            r=0;

            if(lhs.arr[i]-z<rhs.arr[i])

            r=1;

      }

      return new longint(newarr);

   }

   if(lhs==rhs)

      return new longint("0");

      else  

        throw new ArgumentException("cannot lhs must greather than rhs");

}

//********************************************************************

public static longint operator*(longint lhs,longint rhs)

{

      byte [] newarr=new byte[30];

      longint result=new longint(newarr);

      for (int i=0;i<lhs.arr.Length;i++)

      {

            result=result+new longint(lhs.mul(lhs.arr,((byte)(rhs.arr[i])),i) );

      }

      return  result;

}

//*********************************************************************

public static longint operator/(longint lhs,longint rhs)

{

   if (lhs>rhs)

   {

      longint result=new longint("0");longint res=new longint((string)lhs);longint one =new longint("1");

      while(res>rhs)

      {

            res=res-rhs;

            result=result+one;

      }

      return result;

   }

   throw new ArgumentException("cannot lhs must greather than rhs");

}

//*********************************************************************

public static longint operator%(longint lhs,longint rhs)

{

   if (lhs>rhs)

   {Console.WriteLine("::");

      longint res=new longint((string)lhs);//Console.WriteLine((string)res);

      while(res>rhs)

      {

      res=res-rhs;

      }

      return res;

   }

   throw new ArgumentException("cannot lhs must greather than rhs");

}

//****************************************************************************

public  int CompareTo(object comp)

{

      if(comp is longint)

      {

        for(int i=29;i>=0;i--)

        {

            if(arr[i]!=((longint)comp).arr[i])

            {

                  if(arr[i]>((longint)comp).arr[i])

                        return 1;

                  return -1;

            }

        }

        return 0;

      }

      throw new ArgumentException("cannot compare ,you must compare longint type with longint type");

}

//***********************************************************************

public static bool operator>(longint lhs,longint rhs)

{

      if((lhs.CompareTo(rhs))==1)

          return true;

      return false;

}

public static bool operator<(longint lhs,longint rhs)

{

      if((lhs.CompareTo(rhs))==-1)

          return true;

      return false;

}

public static bool operator>=(longint lhs,longint rhs)

{

      if(((lhs.CompareTo(rhs))==1)||((lhs.CompareTo(rhs))==0))

          return true;

      return false;

}

public static bool operator<=(longint lhs,longint rhs)

{

      if(((lhs.CompareTo(rhs))==-1)||((lhs.CompareTo(rhs))==0))

          return true;

      return false;

}

//****************************************************************

public static bool operator==(longint lhs,longint rhs)

{

      if((lhs.CompareTo(rhs))==0)

          return true;

      return false;

}

public static bool operator!=(longint lhs,longint rhs)

    {

      return !(lhs == rhs);

    }

public override bool Equals(object o)

    {

      if (o is longint)

      {

            return false;

      }

         return this==(longint) o;

    }

public override int GetHashCode()

      {

            return 0;

      }

//*********************************************************************************

public static implicit operator longint(int theInt)

{

      int x=theInt;int i=0;

      byte [] newarr=new byte[30];

      while (x/Base!=0)

      {

            newarr[i++]=((byte)(x%Base));

            x=x/Base;

      }

      newarr[i]=((byte)x);

      return new longint(newarr);

}

//****************************************************************

public static implicit operator string(longint L)

{

      ulong m=1;

            int c=0;

            int r=-1;

            string str="";

            string []arr1=new string[30];

            for(int i=0;i<L.arr.Length;i++)          //full new Array with (string*Base^x)

            {

                  string s=(L.arr[i]*m).ToString();

                  if (s!="0")                           //so no zero in arr1

                  {arr1[i]=s;}//Console.WriteLine(arr1[i]);}                           //arr1 is array of string ,so after that we will add it's element by use Add(string s,string ss)

                  m=m*Base;//Console.WriteLine(s);

            }

            while (arr1[++r]!=null)

            {

                  str=L.Add(ref str,arr1[r],ref c);

            }

            return c.ToString()+str;

           

 }

     

//**************************************************************

public static longint copy(longint L)

{

    return new longint(L.arr);     

}

//***************************************************************

public int Length

{

      get

      {

            int i=29;

            while(arr[i--]==0)     

          {}

          return (i+2);

      }

}

}

//****************************************************************

public class tester

{

public static int Main()

{

      //longint L1=new longint("321");

      longint L2=new longint("1234");

      longint L5=new longint("123456");

      //Console.WriteLine(L1.ToString());

      //Console.WriteLine(L5.ToString());

      //string k=(string) L2;

      //longint L3=L2/L5;

      longint L4=L2*L5;

     //longint L6=L2+L5;

      //longint L7=L2-L5;

      //longint L9=L2%L5;

      //if (L1<L2)

      Console.WriteLine((string)L4);

      // L1.Prepend(123);

     

      return 0;

}

}

}

 

Hosted by www.Geocities.ws

1