Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Problema con le strutture
Forum - C# / VB.NET - Problema con le strutture

Avatar
Matthew (Member)
Expert


Messaggi: 387
Iscritto: 29/01/2007

Segnala al moderatore
Postato alle 12:18
Mercoledì, 30/07/2008
Questo topic è stato chiuso dal moderatore

Ciao a tutti,
sto provando a scrivere un programma che possa gestire grandi numeri, ossia al di la della portata delle normali variabili numeriche. Per farlo volevo dividere il numero in un array di stringhe ognuna delle quali contiene 5 cifre. Finora sono riuscito a creare una struttura chiamata Gnum (Grande numero) che divide il numero in due array: uno per la parte intera e uno per i decimali.
Mi spiego meglio:
creata la struttura
Codice sorgente - presumibilmente C#

  1. public struct Gnum
  2.     {
  3.         private static int x, y, sd, resto;
  4.         private static string strnumD, smParz;
  5.         private static string[] stra, straD, str1;
  6.         private static Gnum somma;
  7.  
  8.         public Gnum(string strnum)
  9.         {
  10.             y = strnum.IndexOf(",");
  11.  
  12.             str1 = new string[2];
  13.             if (y != -1)
  14.             {
  15.                 str1 = strnum.Split(',');
  16.  
  17.                 strnum = str1[0];
  18.                 strnumD = str1[1];
  19.             }
  20.             else
  21.             {
  22.                 strnumD = "0";
  23.             }
  24.  
  25.             while (Math.IEEERemainder((double)strnum.Length, 5) != 0)
  26.             {
  27.                 strnum = strnum.Insert(0, "0");
  28.             }
  29.  
  30.             for (x = 5; strnum.Length > x; x += 6)
  31.             {
  32.                 strnum = strnum.Insert(strnum.Length - x, "#");
  33.             }
  34.  
  35.             while (Math.IEEERemainder((double)strnumD.Length, 5) != 0)
  36.             {
  37.                 strnumD = strnumD.Insert(strnumD.Length, "0");
  38.             }
  39.  
  40.             for (x = 5; strnumD.Length > x; x += 6)
  41.             {
  42.                 strnumD = strnumD.Insert(strnumD.Length - x, "#");
  43.             }
  44.  
  45.             stra = new string[50];
  46.             stra = strnum.Split('#');
  47.  
  48.             straD = new string[50];
  49.             straD = strnumD.Split('#');
  50.  
  51.             this.Intero = stra;
  52.             this.Decimali = straD;
  53.         }
  54.        
  55.         public string[] Intero
  56.         {
  57.             get { return stra; }
  58.             set { stra = value; }
  59.         }
  60.  
  61.         public string[] Decimali
  62.         {
  63.             get { return straD; }
  64.             set { straD = value; }
  65.         }
  66. }


posso facilmente creare un grande numero così:
Codice sorgente - presumibilmente C# / VB.NET

  1. Gnum n1 = new Gnum(textBox1.Text);


il problema però e che non riesco a creare due grandi numeri distinti, infatti il seguente codice :
Codice sorgente - presumibilmente C# / VB.NET

  1. Gnum n1 = new Gnum(textBox1.Text);
  2. Gnum n2 = new Gnum(textBox2.Text);


crea si due grandi numeri, ma quando viene creato il secondo il primo si modifica da solo e diventa uguale al secondo.
Come posso fare per risolvere questo problema?

Grazie per l'attenzione e grazie in anticipo per l'aiuto!
Buona giornata a tutti.


Matthew ha allegato un file: Gnum_01.zip (52633 bytes)
Clicca qui per scaricare il file

Ultima modifica effettuata da Matthew il 30/07/2008 alle 12:25
PM
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 10:56
Giovedì, 31/07/2008
Semplicemente perchè hai dichiarato le variabili static. Questo significa che per qualunque istanza della struttura Gnum, quelle variabili sono uguali: ossia è come se fossero messe "in comune" tra tutti gli oggetti di tipo Gnum. Per risolvere il problema, basta togliere la keyword static.

PM
Avatar
Matthew (Member)
Expert


Messaggi: 387
Iscritto: 29/01/2007

Segnala al moderatore
Postato alle 13:36
Giovedì, 31/07/2008
Grazie mille ho seguito il tuo suggerimento e ora funziona!

PM
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 10:26
Venerdì, 01/08/2008
Ok allora chiudo.

PM