Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


AddEx in C#

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Fri Nov 21, 2008 7:08 am    Post subject: AddEx in C# Reply with quote

Just made this to possibly make it easier to work with big numbers in C#. I've only made the Addition method, but i hope ill be able to complete the methods for subtraction, multiplication and divsion aswell.
Code:

public static string Add(string addend, string addend2)
        {
            string buffer = "", buffer2 = "", result = "", resultBuffer = "";
            int index = 1, mem = 0, mem2 = 0;

            if (addend.Length - addend2.Length >= 0)
            { buffer = addend2; buffer2 = addend; }
            else { buffer = addend; buffer2 = addend2; }
           
            for (int i = buffer.Length - 1; i > -1; i--)
            {
                mem = int.Parse(buffer[i].ToString()) + int.Parse(buffer2[buffer2.Length - index].ToString());
                mem += mem2; mem2 = 0;

                if (mem > 9) { mem2 = int.Parse(mem.ToString()[0].ToString()); resultBuffer +=mem.ToString()[1]; }
                else resultBuffer += mem.ToString(); index++;
            }
           
            for (int i = buffer2.Length - index; i > -1; i--)
            {
                if (mem2 != 0)
                {
                    string partOfBuffer = buffer2.Substring(buffer2.Length - index);
                    if (int.Parse(buffer2[buffer2.Length - index].ToString()) + mem2 > 9)
                    {
                        if (mem2.ToString().Length ==2)
                        partOfBuffer.Replace(buffer2[buffer2.Length - index].ToString(), mem2.ToString()[1].ToString());
                    if (mem2.ToString().Length == 1)// This might be incorrect, but 3 would be illogical
                        partOfBuffer.Replace(buffer2[buffer2.Length - index].ToString(), mem2.ToString()[0].ToString());
                        mem2 = int.Parse(mem2.ToString()[0].ToString());
                    }
                    else
                    {
                        partOfBuffer.Replace(buffer2[buffer2.Length - index].ToString(), mem2.ToString());
                        mem2 = 0;
                    }
                    buffer2 = buffer2.Substring(0, buffer2.Length - (buffer2.Length - index)) + partOfBuffer;
                }
                resultBuffer += buffer2[buffer2.Length - index]; index++;  }
         
            for (int i = resultBuffer.Length - 1; i > -1; i--)
            { result += resultBuffer[i]; }
            return result;
        }
For those who can't see what this does, it is supposed to make it easier to add numbers that are bigger than 36893488147419103232 . This is done by storing the numbers as strings, and calling my method.
I'm not sure if this works on all numbers, so please give feedback if you get wrong results.
Below I've attached a simple program which makes use of this method.



The Extension 'zip' was deactivated by an board admin, therefore this Attachment is not displayed.


_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Fri Nov 21, 2008 9:21 am    Post subject: Reply with quote

That's pretty long, too many variables and calls. (buffer, buffer2, result, resultBuffer, index, mem, mem2... ToString, Parse, Substring, Replace...)

just do long addition and add the numbers to a list of integers.

Edit: Here's my bad attempt of making a LargeInteger class... works perfectely fine with integers, strings is the problem.

By the way, every cell holds only 1 digit, if you modify the code a bit to hold up to 9 digits (999,999,999) it'll save a lot of space.

Also notice that the digits are in reversed order, this way I don't have to move every digit to the next cell before adding the next digit, I just add the digit at the end and print it from last cell to first.
Code:
using System;
using System.Collections.Generic;

namespace ConsoleForTests
{
    class LargeInteger
    {
        private List<int> Integers = new List<int>();
        public int Count { get { return Integers.Count; } }

        public LargeInteger()
        {
            this.Integers.Add(0);
        }

        public LargeInteger(LargeInteger copy) //Copy constructor
        {
            for (int i = 0; i < copy.Count; i++)
                this.Integers.Add(copy.GetDigit(i));
        }

        private void Add(int num, int StartingIndex)
        {
            if (num == 0)
                return;
            int n, i = StartingIndex;
            do
            {
                n = num % 10;
                if (n == 0)
                {
                    this.Integers.Add(0); //There's a problem here... it should only add a 0 if it's the last digit (like in 1930), won't work for numbers like 1035.
                    i++;
                }
                else
                    if (i == this.Integers.Count)
                    {
                        this.Integers.Add(n);
                        n = 0;
                        i++;
                    }
                    else for (int j = i; j < this.Integers.Count && n > 0; j++)
                        {
                            if (n + this.Integers[j] >= 10)
                            {
                                n += this.Integers[j];
                                this.Integers[j] = n % 10;
                                if (j + 1 == this.Integers.Count)
                                    this.Integers.Add(1);
                                else this.Integers[j + 1] += n / 10;
                                n = 0;
                            }
                            else
                            {
                                this.Integers[j] += n;
                                n = 0;
                                i++;
                            }
                        }
            } while (Convert.ToBoolean(num /= 10));

            while (this.Integers[this.Integers.Count - 1] == 0) this.Integers.RemoveAt(this.Integers.Count - 1); //I noticed there's an extra 0 at the end sometimes, I couldn't fix the problem so I just removed... work on it a little bit and it should work fine.
        }

        public void Add(int num)
        {
            this.Add(num, 0);
        }

        public void Add(LargeInteger num)
        {
            for (int i = 0; i < num.Count; i++)
                this.Add(num.GetDigit(i), i);
        }

        public void Add(string str)
        {
            int num, index = 0;
            while (str.Length >= 10) //Get up to 9 digits.
            {
                int i = 9;
                while (str[str.Length - i] == '0') i--;
                num = int.Parse(str.Substring(str.Length - i, i));
                str = str.Substring(0, str.Length - i);
                this.Add(num, index);
                index += i;
            }
            num = int.Parse(str);
            if (num > 0)
                this.Add(num, index);
        }

        public void Mul(int By)
        {
            LargeInteger x = new LargeInteger(this);
            for (int i = 1; i < By; i++)
                this.Add(x);
        }

        public int GetDigit(int Index)
        {
            //return this.Integers[this.Integers.Count - Index - 1]; //Index 0 = First digit (the digit 1 in 1337)
            return this.Integers[Index]; //Index 0 = Last digit (the digit 5 in 3895)
        }

        public void Print()
        {
            for (int i = Integers.Count - 1; i >= 0; i--) //Print in reversed order...
                Console.Write(Integers[i]);
            Console.WriteLine();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LargeInteger LI = new LargeInteger();
            Console.WriteLine("New large integer created!\nAdding 153...");
            LI.Add(153);
            LI.Print();
            Console.WriteLine("Done! adding 153 again, 9 times...");
            for (int i = 1; i < 10; i++)
            {
                LI.Add(153);
                LI.Print();
            }
            Console.WriteLine("Done! now multipying by 20...");
            LI.Mul(20);
            LI.Print();
            Console.WriteLine("Done!");
            LI.Add("37107287533902102798797998220837590246510135740250");
            LI.Print();
            //Problem with using '0' digit with strings... some strings that contains a 0 will return the correct result, however not all. (the first one works fine)
            LI.Add("750"); //46376937677490009712648124896970078050417018260538
            LI.Print();
        }
    }
}

That's not as good as I thought it would be...
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri Nov 21, 2008 3:35 pm    Post subject: Reply with quote

I think .NET 4.0 has support for this out of the box.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites