Forum guidelines
AuthorQuestion
c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012
hi
i have a project and i cant do it..
my project is "string four process"
example:
string number1=12345678978945612378945612378945612378945664563214722155278954;
string number2=7894561233214569877896541233214569874563258966558632889625
 how can i find numer1+number2?
please help....
thanks


AuthorReply
Re: c# HELP!!!
Posted By Vulpes on 21 Nov 2012  
If you're using .NET 4.0 (VS 2010) or later, then you can use the BigInteger structure.

To run the following program you need to add a reference to System.Numerics.dll to your project:

using System;
using System.Numerics;

class Program
{
   static void Main()
   {
      string number1 = "12345678978945612378945612378945612378945664563214722155278954";
      string number2 = "7894561233214569877896541233214569874563258966558632889625";
      BigInteger big1 = BigInteger.Parse(number1);
      BigInteger big2 = BigInteger.Parse(number2);
      BigInteger big3 = big1 + big2;
      Console.WriteLine(big3);
      Console.ReadKey();
   }
}

The output should be:

12353573540178826948823508920178826948820227822181280788168579



Re: c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012  
error!!! Error 1 The type or namespace name 'BigInteger' could not be found (are you missing a using directive or an assembly reference?) C:\Users\mypc\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 14 7 ConsoleApplication1 Error 2 The name 'BigInteger' does not exist in the current context C:\Users\mypc\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 14 25 ConsoleApplication1 Error 3 The type or namespace name 'BigInteger' could not be found (are you missing a using directive or an assembly reference?) C:\Users\mypc\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 15 7 ConsoleApplication1 Error 4 The name 'BigInteger' does not exist in the current context C:\Users\mypc\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 15 25 ConsoleApplication1 Error 5 The type or namespace name 'BigInteger' could not be found (are you missing a using directive or an assembly reference?) C:\Users\mypc\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 16 7 ConsoleApplication1
Re: c# HELP!!!
Posted By Vulpes on 21 Nov 2012  
As I said in my previous post, you have to add a reference to System.Numerics.dll (which contains the BigInteger struct) for this to work and you need to be using VS 2010 (or 2012) for this dll to be present on your system.

The following using directive also needs to be added at the top of your source code file:

using System.Numerics;

To add a reference right click on your project's name in Solution Explorer and choose Add reference from the drop down. Then navigate to System.Numerics.dll under the .NET tab and press OK.



Re: c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012  
im using c# visual studio expess 2010
Re: c# HELP!!!
Posted By Vulpes on 21 Nov 2012  
It should still work.

Just follow the instructions above to add the reference to the dll you need.

Re: c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012  
only an error Error 1 The type or namespace name 'Numerics' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\mypc\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 5 14 ConsoleApplication1
Re: c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012  
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string number1 = "12345678978945612378945612378945612378945664563214722155278954"; string number2 = "7894561233214569877896541233214569874563258966558632889625"; BigInteger big1 = BigInteger.Parse(number1); BigInteger big2 = BigInteger.Parse(number2); BigInteger big3 = big1 + big2; Console.WriteLine(big3); Console.ReadKey(); } } }
Re: c# HELP!!!
Posted By Vulpes on 21 Nov 2012  
If you've added the reference correctly, then you should see System.Numerics listed under 'References' in Solution Explorer.
Re: c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012  
firstly, thanks for your helping... can you write code corretly then post me please this project very importent for me if i cant, i will fail
Re: c# HELP!!!
Posted By Vulpes on 21 Nov 2012  
The code is correct.

All you need to do is add the reference to System.Numerics.dll and it will compile and run fine.

Re: c# HELP!!!
Posted By Orhan SALUR on 21 Nov 2012  
its oke i do it thanks you very mach
Re: c# HELP!!!
Posted By Maksim on 21 Nov 2012  
Right click in Solution Explorer on References folder and select Add Reference. Then select Assemblies, Framework. There you should find System.Numerics, so Import it and use code provided above.

Re: c# HELP!!!
Posted By Orhan SALUR on 23 Nov 2012  
hi... how can i write BigInteger class i need to BigInteger class mein code thanks...
Re: c# HELP!!!
Posted By Vulpes on 23 Nov 2012  
BigInteger classes are hard to write.

It look even Microsoft's programmers several years to obtain acceptable performance from the current .NET implementation which is why it didn't appear until .NET 4.0.

If you're thinking of writing your own,. I'd check out this old article:

http://www.codeproject.com/Articles/2728/C-BigInteger-Class

Re: c# HELP!!!
Posted By Orhan SALUR on 23 Nov 2012  
hi... i have to write this code our teacher said that wrote with matrix but i cant do it i have no good english so i cant search foreign website if you can help me, will be happy thanks...
Vulpes
  • 1
  • 0
accepted
Re: c# HELP!!!
Posted By Vulpes on 24 Nov 2012   Accepted Answer
Well, writing your own BigInteger class is out of the question as I said earlier.

Also I don't know what your teacher means about using a matrix and there's no Matrix class in the .NET framework in any case.

However, I'll show you a relatively easy way of adding two large numbers which doesn't use BigInteger but mimics how you'd add such numbers on a piece of paper using the basic rules of arithmetic. To do this we need to set up some arrays:

using System;

class Program
{
   static void Main()
   {
      string number1 = "12345678978945612378945612378945612378945664563214722155278954";
      string number2 = "7894561233214569877896541233214569874563258966558632889625";
      string number3 = Add(number1, number2);
      Console.WriteLine(number3);
      Console.ReadKey();
   }

   static string Add(string n1, string n2)
   {
      int len1 = n1.Length;
      int len2 = n2.Length; 
      int max = Math.Max(len1, len2);
      int[] ia1 = new int[max];
      int[] ia2 = new int[max];
      int[] ia3 = new int[max + 1]; // allow for carry forward
      for(int i = max - len1; i < max; i++) ia1[i] = n1[i + len1 - max] - 48;
      for(int i = max - len2; i < max; i++) ia2[i] = n2[i + len2 - max] - 48;
      int sum = 0;
      int carry = 0;

      for(int i = max - 1; i >= 0; i--)
      {
         sum = ia1[i] + ia2[i] + carry;
         ia3[i + 1] = sum % 10;
         carry = sum / 10;
      }
 
      if (carry == 1) ia3[0] = 1;
      char[] c3 = new char[max + 1];
      for(int i = 0; i < max + 1; i++) c3[i] = (char)(ia3[i] + 48);
      return new string(c3).TrimStart('0');       
   }


Again the output is:

12353573540178826948823508920178826948820227822181280788168579



SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.