Discussione sull’articolo "Le basi sulla programmazione in MQL5: Stringhe" - pagina 2

 
denkir:

...Personalmente, ero interessato alla domanda sui limiti di lunghezza delle linee....

Ho sperimentato ora, 32 mb ci stanno, ci starebbero anche di più, ma ci vorrebbe molto tempo.
 

According to the description of these functions in MQL5 Reference, they allow us to add up strings in a more space efficient (in terms of the occupied working memory) and faster way.

In effetti ci sono incoerenze nella documentazione. Non so se l'utilizzo della memoria sia più veloce o meno, StringConcatenate() è molto più lento di StringAdd e +.

2013.04.11 19:09:48    teststring (EURUSD,M1)    time for 'StringConcatenate(c,a,b)' = 1170 milliseconds, i = 1000000
2013.04.11 19:09:47    teststring (EURUSD,M1)    time for 'StringAdd(a,b)' = 94 milliseconds, i = 1000000
2013.04.11 19:09:47    teststring (EURUSD,M1)    time for 'c = a + b' = 265 milliseconds, i = 1000000


Ottimo articolo, un must per tutti i principianti.

[Eliminato]  
Grazie.
 
Ottimo articolo, grazie.
 

è bello stancarsi di voi per renderci meno ignoranti. grazie per il vostro lavoro

 

Aggiunge un separatore di gruppi di cifre alla stringa:

//+------------------------------------------------------------------+
//|DelimitatoreGruppiDigitali.mq5 |
//| Copyright 2018, MetaQuotes Software Corp. | |
//| https://www.mql5.com
//+------------------------------------------------------------------+
#property script_show_inputs
//--- Parametri esterni
input double Value     =1000000;
input string Delimiter =" "; // Delimitatore, per impostazione predefinita: " ", esempio: 1 000 000.0
//+------------------------------------------------------------------+
//| Funzione di avvio del programma di script|
//+------------------------------------------------------------------+
void OnStart(void)
  {
   ::Print(DelimiterGroupsDigits(string(Value),Delimiter));
  }
//+------------------------------------------------------------------+
//| Separatore di gruppi di cifre|
//+------------------------------------------------------------------+
string DelimiterGroupsDigits(const string value,const string delimiter=" ")
  {
   string res=value;
   string elements[];
//--- Rimuovere i caratteri extra dai bordi.
   ::StringTrimLeft(res);
   ::StringTrimRight(res);
//--- Verificare la presenza di un numero reale
   ushort sep   =::StringGetCharacter(".",0);
   int    total =::StringSplit(res,sep,elements);
//--- Ottenuta la stringa
   if(total>0)
     {
      string str[];
      int length=::StringLen(elements[0]);
      for(int k=0,i=length-1; i>=0; i--)
        {
         int size=::ArraySize(str);
         ::ArrayResize(str,size+1);
         str[size]=::StringSubstr(elements[0],i,1);
         k++;
         //--- Aggiungere un separatore
         if(k>=3 && i>0)
           {
            int array_size=::ArraySize(str);
            ::ArrayResize(str,array_size+1);
            str[array_size]=delimiter;
            k=0;
           }
        }
      //--- Raccogliere la stringa
      res="";
      int elements_total=::ArraySize(str);
      for(int i=elements_total-1; i>=0; i--)
         ::StringAdd(res,str[i]);
      //--- Se numero reale
      if(total>1)
         ::StringAdd(res,"."+elements[1]);
     }
//--- Restituire il risultato
   return(res);
  }
//+------------------------------------------------------------------+

//---

Risultato:

1 000 000.0
10,000,000.0
100'000'000.0
10 000.545
 
Anatoli Kazharski:

Aggiunge un separatore di gruppi di cifre a una stringa:

In qualche modo complicato.
 
fxsaber:
È piuttosto complicato.
Cercherete di ottimizzarlo?
 
Anatoli Kazharski:
Cercherete di ottimizzarlo?
string ModToString( ulong &Num, const int Mod = 1000, const int Len = 3 )
{
  const string Res = ((bool)(Num / Mod) ? IntegerToString(Num % Mod, Len, '0') : (string)(Num % Mod));
  
  Num /= Mod;
  
  return(Res);
}

string NumToString( ulong Num, const string Delimeter = " " )
{
  string Res = ModToString(Num);

  while (Num)
    Res = ModToString(Num) + Delimeter + Res;

  return(Res);
}

string NumToString( double Num, const int digits = 8, const string Delimeter = NULL )
{
  const string PostFix = (Num < 0) ? "-" : NULL;
  
  Num = MathAbs(Num);
    
  return(PostFix + NumToString((ulong)Num, Delimeter) + StringSubstr(DoubleToString(Num - (long)Num, digits), 1));
}

void OnStart()
{
  Print(NumToString(1234567.89, 2, " "));
}
 
fxsaber:

E' fantastico! E quattro volte più veloce:

void OnStart(void)
  {
   long count=10000000;
   uint start=GetTickCount();
   for(int i=0; i<count && !IsStopped(); i++)
      DelimiterGroupsDigits(string(Value),Delimiter);

   uint end=GetTickCount()-start;
   Print("01 > ms: ",end,"; res: ",DelimiterGroupsDigits(string(Value),Delimiter));
//---
   start=GetTickCount();
   for(int i=0; i<count && !IsStopped(); i++)
      NumToString(Value,2,Delimiter);

   end=GetTickCount()-start;
   Print("02 > ms: ",end,"; res: ",NumToString(Value,2,Delimiter));
  }

//---

2018.01.30 21:02:15.996 01 > ms: 19047; res: 1 000 000.0
2018.01.30 21:02:20.683 02 > ms: 4688; res: 1 000 000.00