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/C++ - Problemi di aritmetica dei puntatori
Forum - C/C++ - Problemi di aritmetica dei puntatori

Avatar
spode (Normal User)
Pro


Messaggi: 151
Iscritto: 03/09/2010

Segnala al moderatore
Postato alle 17:02
Lunedì, 04/08/2014
Ciao!
Potreste aiutarmi con questo codice:
Codice sorgente - presumibilmente C++

  1. char* strBetweenPointersReverse(const char* yourstring, const char *pos1, const char *pos2, char whereToPut[]) {
  2.         #if DEBUG
  3.         puts("\nstringBetweenReverse()");
  4.         printf("\nyourstring %s", yourstring);
  5.         #endif
  6.         strcpy(whereToPut, yourstring);
  7.         strdelete(whereToPut, whereToPut, pos1); //non mi dà problemi
  8.         strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut)); //qui ho problemi
  9.         return whereToPut;
  10. }



dove yourstring = ""-13+2*41\0", pos1 = "-13+2*41" (posizione 0 di yourstring) e pos2="2*41" (posizione 6 di yourstring).

Codice sorgente - presumibilmente C#

  1. char* strdelete(char *where, char *from_inc, char* to_inc) {
  2.         if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
  3.         char temp[strlen(where)-(to_inc-from_inc)+1]; //in particolare noto che la differenza tra i puntatori non dà il risultato atteso =(
  4.         int pos=0;
  5.         for(;(from_inc-(where+pos)) != 0; pos++)
  6.                 *(temp+pos) = *(where+pos);
  7.         for(int posTo_inc=1; *(to_inc+posTo_inc) != '\0';posTo_inc++,pos++)
  8.                 *(temp+pos) = *(to_inc+posTo_inc);
  9.         *(temp+pos)='\0';
  10.         strcpy(where,temp);
  11.         return where;
  12. }



Ho rilevato che il risultato non è quello atteso per via di to_inc-from_inc: eppure puntano entrambi a youstring... ci sono casi in cui lavora bene e altri in cui non va, tipo quello menzionato sopra.

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 3:36
Mercoledì, 06/08/2014
Che compilatore stai usando?


Il mio blog: https://piero.dev
PM Quote
Avatar
spode (Normal User)
Pro


Messaggi: 151
Iscritto: 03/09/2010

Segnala al moderatore
Postato alle 9:38
Mercoledì, 06/08/2014
Ciao! GCC con Eclipse Luna per il debug, sotto OpenSuse 13.1 e hardware...beh il processore è un vecchio Intel Dual core.

Ultima modifica effettuata da spode il 06/08/2014 alle 13:42
PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 17:12
Mercoledì, 06/08/2014
Dovresti postarci il codice completo... in particolare come viene chiamata strBetweenPointersReverse.


Il mio blog: https://piero.dev
PM Quote
Avatar
spode (Normal User)
Pro


Messaggi: 151
Iscritto: 03/09/2010

Segnala al moderatore
Postato alle 20:14
Mercoledì, 06/08/2014
Codice sorgente - presumibilmente C++

  1. /*
  2. Ottiene "(132-2*51)"
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. #define DEBUG 0
  10. #define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
  11.  
  12. long int powi(int num, int exp) {//10^0
  13.         long int ris=1;
  14.         while(exp--)
  15.                 ris=ris*num;
  16.         return ris;
  17. }
  18.  
  19. int intlen(int num) {
  20.         int i=1;
  21.         while(num/powi(10,i) != 0)
  22.                 i++;
  23.         return i;
  24. }
  25.  
  26. /*
  27.  * Delete characters from where starting from from_included position to to_included position.
  28.  * Positions included in the process of deleting.
  29.  *
  30.  * Example:
  31.  * where = +1*51-1*132+1*51\0
  32.  * from_included = 0, to_included=4
  33.  * OutPut: where = -1*132+1*51\0
  34.  * */
  35. void strdeleteAt(char where[], const int from_included, const int to_included) {
  36.         char temp[strlen(where)-(to_included-from_included)+1];
  37.         int i;
  38.         for(i=0; i<from_included;i++)
  39.                 temp[i] = where[i];
  40.         strcpy(temp+i, where+to_included+1);
  41.         strcpy(where, temp);
  42. }
  43.  
  44. /*
  45.  * Deletes characters in where starting from from_inc position to to_inc position.
  46.  * Positions included in the process of deleting.
  47.  *
  48.  * Warning: passing a NULL pointer causes where to be returned.
  49.  *
  50.  * Example:
  51.  * where = +1*51-1*132+1*51\0
  52.  * from_inc = strstr(where, "+");
  53.  * to_inc=strstr(where, "*");
  54.  * OutPut: where = 51-1*132+1*51\0
  55.  */
  56. char* strdelete(char where[], char *from_inc, char* to_inc) {
  57.         if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
  58.         printf("\nwhere: %s %d\nto_inc: %s %d\nfrom_inc: %s %d", where, strlen(where), to_inc, strlen(to_inc), from_inc, strlen(from_inc));
  59.         short len = strlen(where)-(to_inc-from_inc)+1;//from inc è pos 1 => errore
  60.         int pos=0;
  61.         if(len <= 0) return where;
  62.         char temp[len];
  63.         for(;from_inc-(where+pos) != 0; pos++)
  64.                 *(temp+pos) = *(where+pos);
  65.         for(int posTo_inc=1; *(to_inc+posTo_inc) != '\0';posTo_inc++,pos++)
  66.                 *(temp+pos) = *(to_inc+posTo_inc);
  67.         *(temp+pos)='\0';
  68.         strcpy(where,temp);
  69.         return where;
  70. }
  71.  
  72. char* int2string(const int toBeParsed, char* whereToPut) {//-1
  73.         char digits[] = "0123456789";
  74.         int pos = 0, len=intlen(toBeParsed);
  75.         if(toBeParsed<=0) {
  76.                 *whereToPut = '-';
  77.                 pos++;
  78.         }
  79.  
  80.         /* Determine the integer and putting it into whereToPut.
  81.          * for 38:
  82.          *    whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
  83.          *    whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
  84.          *    whereToPut => "38\0"
  85.         */
  86.         while(len-- > 0)
  87.                 *(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
  88.         *(whereToPut+pos)='\0';
  89.         #if DEBUG
  90.         printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
  91.         #endif
  92.         return whereToPut;
  93. }
  94.  
  95. char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
  96.         int pos = 0;
  97.         for(; to_included >= from_included; from_included++, pos++)
  98.                 *(whereToPut+pos) = *(where+from_included);
  99.         *(whereToPut+pos) = '\0';
  100.         return whereToPut;
  101. }
  102.  
  103. char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
  104.         if(to_included == NULL)
  105.                 return strcpy(whereToPut, from_included);
  106.         int pos = 0;
  107.         for(; to_included >= from_included; from_included++, pos++)
  108.                 *(whereToPut+pos) = *(from_included);
  109.         *(whereToPut+pos) = '\0';
  110.         return whereToPut;
  111. }
  112.  
  113. int countChars(const char where[], const char character) {
  114.         int cont = 0;
  115.         for(; *where;where++)
  116.                 if(*where == character) cont++;
  117.         return cont;
  118. }
  119.  
  120. _Bool strcontain(const char where[], const char character) {
  121.         return countChars(where, character);
  122. }
  123.  
  124. _Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
  125. {
  126.         _Bool r=1;
  127.         for(int i=0;i<charactersLen && r; i++)
  128.                 r = r && countChars(where, characters[i]);
  129.         return r;
  130. }
  131.  
  132. char* strstrArray(const char where[], const char chars[], const short charsLeng) {
  133.         //restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
  134.         char* occ = NULL;
  135.         for(int i=0; i < charsLeng; i++)
  136.         for(int pos = 0; *(where+pos); pos++)
  137.                 if(*(where+pos) == chars[i])
  138.                         if(occ == NULL)
  139.                         {
  140.                                 occ = where+pos;
  141.                                 pos = strlen(where)-1;
  142.                         }
  143.                         else
  144.                                 if(occ != NULL && where+pos < occ)
  145.                                 {
  146.                                         occ = where+pos;
  147.                                         pos = strlen(where)-1;
  148.                                 }
  149.         return occ;
  150. }
  151.  
  152. void emptystring(char string[]) {
  153.         while(*string++ = '\0');
  154. }
  155.  
  156. char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
  157.         #if DEBUG
  158.         puts("\nstringBetweenReverse()");
  159.         printf("\nyourstring %s", yourstring);
  160.         #endif
  161.         strcpy(whereToPut, yourstring);
  162.         strdelete(whereToPut, whereToPut, pos1);
  163.         strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut));
  164.         return whereToPut;
  165.         #if DEBUG
  166.         printf("\nwhereToPut = %s", whereToPut);
  167.         #endif
  168. }
  169.  
  170. /*
  171.  * This function find the first addend's components of an expression.
  172.  *
  173.  * @Params:
  174.  * exp is the expression where you want to find the first addend;
  175.  * mul2 is the memory location where the multiplier will be put. Must be "" when passed;
  176.  * num is the memory location where the number will be put. Must be "" when passed.
  177.  *
  178.  * @returns:
  179.  * true if the first addend was found. false otherwise.
  180.  *
  181.  * Example:
  182.  * exp = "è?(["; mul2 = ""; num = ""
  183.  * Output: mul2 = ""; ncum = ""
  184.  *
  185.  * Example :
  186.  * exp = "2*132-51"; mul2 = ""; num = ""
  187.  * Output: mul2 = "2", num = "132
  188.  * */
  189. _Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
  190.         char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
  191.         if(endnum != NULL)
  192.                 endnum--;
  193.         if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
  194.                 if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strstr(exp, "*"))
  195.                 { //-132-2*51; -2*132-2*51; -2*132
  196.                         char r[9+1];
  197.                         strcpy(r,strBetweenPointersReverse(exp, strstr(exp, "-"), strstr(exp, "*")-1, mul2));
  198.                         if(strcontainArray(r, signs, 2)) //is there, next to + or -, a number?
  199.                         { //-132-2*51
  200.                                 strcpy(mul2, "-1");//sure that exp contains *
  201.                                 substringAtstrstr(exp, exp+1, endnum, num);
  202.                         }
  203.                         else
  204.                         { //-2*132-2*51; -2*132
  205.                                 substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
  206.                         }
  207.                 }
  208.                 else //-132
  209.                 {
  210.                         strcpy(mul2, "-1");
  211.                         substringAtstrstr(exp, exp+1, endnum, num);
  212.                 }
  213.         else //132-2*51; 2*132-2*51; 132; 2*132
  214.                 if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
  215.                 { //132-2*51; 2*132-2*51
  216.                         if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
  217.                         {
  218.                                 substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
  219.                                 substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
  220.                         }
  221.                         else //132-2*51
  222.                         {
  223.                                 strcpy(mul2, "1");
  224.                                 substringAtstrstr(exp, exp, endnum, num);
  225.                         }
  226.                 }
  227.                 else
  228.                         if(strcontain(exp, '*'))
  229.                         { //2*132
  230.                                 substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
  231.                                 substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
  232.                         }
  233.                         else
  234.                         {//132 that must be multiplied per n
  235.                                 strcpy(mul2, "1");
  236.                                 if(*exp=='+')
  237.                                         exp++;
  238.                                 strcpy(num, exp);
  239.                         }
  240.  
  241.         if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
  242.                 return 0;
  243.         else
  244.                 return 1;
  245. }
  246.  
  247. char *distribute(const char sign, char mul1[], char exp[], char result[]){
  248.         char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
  249.         short lung = 0, pos = 0;
  250.         while(findAddendCmpts(exp+pos, mul2, num))
  251.         {
  252.                 if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
  253.                         pos+=occ -(exp+pos);
  254.                 else pos+=strlen(exp+pos);
  255.                 int mulDone = atoi(mul2) * atoi(mul1);
  256.                 if(mulDone > 0) {
  257.                         strcat(result, "+");
  258.                         lung++;
  259.                 }
  260.                 char wtp[intlen(mulDone)+1];
  261.                 strcat(result, int2string(mulDone, wtp));
  262.                 lung+=intlen(mulDone);
  263.                 strcat(result, "*");
  264.                 lung++;
  265.                 strcat(result, num);
  266.                 lung+=strlen(num);
  267.                 *(result+lung+2) = '\0';
  268.                 *num = '\0';
  269.                 *mul2 = '\0';
  270.         }
  271.         return result;
  272. }
  273.  
  274. int main(void) {
  275.         char wtp[10] = "";
  276.         char exp[] ="-13+2*41";
  277.         printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
  278. //dovrei ottenere "-2*13+4*41"
  279.         return 0;
  280. }


Ultima modifica effettuata da spode il 06/08/2014 alle 20:16
PM Quote
Avatar
spode (Normal User)
Pro


Messaggi: 151
Iscritto: 03/09/2010

Segnala al moderatore
Postato alle 13:38
Giovedì, 07/08/2014
Codice sorgente - presumibilmente C++

  1. /*
  2. Ottiene "(132-2*51)"
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. #define DEBUG 0
  10. #define NUMERO 19 //numero max caratteri per a,b e k in k=ax+by e MCD(a,b)
  11.  
  12. long int powi(int num, int exp) {//10^0
  13.         long int ris=1;
  14.         while(exp--)
  15.                 ris=ris*num;
  16.         return ris;
  17. }
  18.  
  19. int intlen(int num) {
  20.         int i=1;
  21.         while(num/powi(10,i) != 0)
  22.                 i++;
  23.         return i;
  24. }
  25.  
  26. /*
  27.  * Delete characters from where starting from from_included position to to_included position.
  28.  * Positions included in the process of deleting.
  29.  *
  30.  * Example:
  31.  * where = +1*51-1*132+1*51\0
  32.  * from_included = 0, to_included=4
  33.  * OutPut: where = -1*132+1*51\0
  34.  * */
  35. void strdeleteAt(char where[], const int from_included, const int to_included) {
  36.         char temp[strlen(where)-(to_included-from_included)+1];
  37.         int i;
  38.         for(i=0; i<from_included;i++)
  39.                 temp[i] = where[i];
  40.         strcpy(temp+i, where+to_included+1);
  41.         strcpy(where, temp);
  42. }
  43.  
  44. /*
  45.  * Deletes characters in where starting from from_inc position to to_inc position.
  46.  * Positions included in the process of deleting.
  47.  *
  48.  * Warning: passing a NULL pointer causes where to be returned.
  49.  *
  50.  * Example:
  51.  * where = +1*51-1*132+1*51\0
  52.  * from_inc = strstr(where, "+");
  53.  * to_inc=strstr(where, "*");
  54.  * OutPut: where = 51-1*132+1*51\0
  55.  */
  56. char* strdelete(char where[], char *from_inc, char* to_inc) {
  57.         if(to_inc == NULL || from_inc == NULL || where == NULL) return where;
  58.         short len = strlen(where)-(strlen(from_inc)-strlen(to_inc))+1;//from inc è pos 1 => errore
  59.         if(len <= 0) return where;
  60.         register int pos=0;
  61.         char temp[len];
  62.         for(;strlen(where+pos)-strlen(from_inc)+1 != 0; pos++)
  63.                 *(temp+pos) = *(where+pos);
  64.         for(register int posTo_inc=1; *(to_inc+posTo_inc) != '\0'; posTo_inc++,pos++)
  65.                 *(temp+pos) = *(to_inc+posTo_inc);
  66.         *(temp+pos)='\0';
  67.         strcpy(where,temp);
  68.         return where;
  69. }
  70.  
  71. char* int2string(const int toBeParsed, char* whereToPut) {//-1
  72.         char digits[] = "0123456789";
  73.         int pos = 0, len=intlen(toBeParsed);
  74.         if(toBeParsed<=0) {
  75.                 *whereToPut = '-';
  76.                 pos++;
  77.         }
  78.  
  79.         /* Determine the integer and putting it into whereToPut.
  80.          * for 38:
  81.          *    whereToPut[0] = digits[38/10 -10*(38/100)] = '3'
  82.          *    whereToPut[1] = digits[38/1 - 10*(38/10)] = '8'
  83.          *    whereToPut => "38\0"
  84.         */
  85.         while(len-- > 0)
  86.                 *(whereToPut + pos++) = digits[abs(toBeParsed) / powi(10,len) - 10*(abs(toBeParsed) / powi(10,len+1))];
  87.         *(whereToPut+pos)='\0';
  88.         #if DEBUG
  89.         printf("\n\tint2string. toBeParsed=%d; whereToPut=%s\n",toBeParsed, whereToPut);
  90.         #endif
  91.         return whereToPut;
  92. }
  93.  
  94. char* substringAt(char where[], int from_included, int to_included, char whereToPut[]) {
  95.         int pos = 0;
  96.         for(; to_included >= from_included; from_included++, pos++)
  97.                 *(whereToPut+pos) = *(where+from_included);
  98.         *(whereToPut+pos) = '\0';
  99.         return whereToPut;
  100. }
  101.  
  102. char* substringAtstrstr(const char where[], char* from_included, const char* to_included, char whereToPut[]) {
  103.         if(to_included == NULL)
  104.                 return strcpy(whereToPut, from_included);
  105.         int pos = 0;
  106.         for(; to_included >= from_included; from_included++, pos++)
  107.                 *(whereToPut+pos) = *(from_included);
  108.         *(whereToPut+pos) = '\0';
  109.         return whereToPut;
  110. }
  111.  
  112. int countChars(const char where[], const char character) {
  113.         int cont = 0;
  114.         for(; *where;where++)
  115.                 if(*where == character) cont++;
  116.         return cont;
  117. }
  118.  
  119. _Bool strcontain(const char where[], const char character) {
  120.         return countChars(where, character);
  121. }
  122.  
  123. _Bool strcontainArray(const char where[], const char characters[], const short charactersLen)
  124. {
  125.         _Bool r=1;
  126.         for(int i=0;i<charactersLen && r; i++)
  127.                 r = r && countChars(where, characters[i]);
  128.         return r;
  129. }
  130.  
  131. char* strstrArray(const char where[], const char chars[], const short charsLeng) {
  132.         //restituire l'occorrenza più vicina a where di una delle stringhe in chars. Per esempio per 132-2*51 e 132+2*51
  133.         char* occ = NULL;
  134.         for(int i=0; i < charsLeng; i++)
  135.         for(int pos = 0; *(where+pos); pos++)
  136.                 if(*(where+pos) == chars[i])
  137.                         if(occ == NULL)
  138.                         {
  139.                                 occ = where+pos;
  140.                                 pos = strlen(where)-1;
  141.                         }
  142.                         else
  143.                                 if(occ != NULL && where+pos < occ)
  144.                                 {
  145.                                         occ = where+pos;
  146.                                         pos = strlen(where)-1;
  147.                                 }
  148.         return occ;
  149. }
  150.  
  151. void emptystring(char string[]) {
  152.         while(*string++ = '\0');
  153. }
  154.  
  155. char* strBetweenPointersReverse(const char* yourstring, char *pos1, char *pos2, char whereToPut[]) {
  156.         #if DEBUG
  157.         puts("\nstringBetweenReverse()");
  158.         printf("\nyourstring %s", yourstring);
  159.         #endif
  160.         strcpy(whereToPut, yourstring);
  161.         strdelete(whereToPut, whereToPut, pos1);
  162.         strdelete(whereToPut, pos2, whereToPut+strlen(whereToPut)-1);
  163.         return whereToPut;
  164.         #if DEBUG
  165.         printf("\nwhereToPut = %s", whereToPut);
  166.         #endif
  167. }
  168.  
  169. /*
  170.  * This function find the first addend's components of an expression.
  171.  *
  172.  * @Params:
  173.  * exp is the expression where you want to find the first addend;
  174.  * mul2 is the memory location where the multiplier will be put. Must be "" when passed;
  175.  * num is the memory location where the number will be put. Must be "" when passed.
  176.  *
  177.  * @returns:
  178.  * true if the first addend was found. false otherwise.
  179.  *
  180.  * Example:
  181.  * exp = "è?(["; mul2 = ""; num = ""
  182.  * Output: mul2 = ""; ncum = ""
  183.  *
  184.  * Example :
  185.  * exp = "2*132-51"; mul2 = ""; num = ""
  186.  * Output: mul2 = "2", num = "132
  187.  * */
  188. _Bool findAddendCmpts(const register char exp[], char mul2[], char num[]) {
  189.         char signs[] = {'+','-'}, *endnum = strstrArray(exp+1, signs, 2);
  190.         if(endnum != NULL)
  191.                 endnum--;
  192.         if(*(exp) == '-') //132-2*51; -132-2*51; 2*132-2*51; -2*132-2*51; 132; -132; -2*132; 2*132
  193.                 if(strcontain(exp, '*') && strstrArray(exp, signs,2) < strchr(exp, '*'))
  194.                 { //-132-2*51; -2*132-2*51; -2*132
  195.                         if(strcontainArray(strBetweenPointersReverse(exp, strchr(exp, '-'), strchr(exp, '*')-1, mul2), signs, 2)) //is there, next to + or -, a number?
  196.                         { //-132-2*51
  197.                                 strcpy(mul2, "-1");//sure that exp contains *
  198.                                 substringAtstrstr(exp, exp+1, endnum, num);
  199.                         }
  200.                         else
  201.                         { //-2*132-2*51; -2*132
  202.                                 substringAtstrstr(exp, mul2+2, strstrArray(mul2+2, signs, 2), num);
  203.                         }
  204.                 }
  205.                 else //-132
  206.                 {
  207.                         strcpy(mul2, "-1");
  208.                         substringAtstrstr(exp, exp+1, endnum, num);
  209.                 }
  210.         else //132-2*51; 2*132-2*51; 132; 2*132
  211.                 if(strcontain(exp+1, '-') || strcontain(exp+1, '+'))
  212.                 { //132-2*51; 2*132-2*51
  213.                         if(strcontain(exp, '*') && strstr(exp, "*") < strstrArray(exp, signs, 2)) //2*132-2*51
  214.                         {
  215.                                 substringAtstrstr(exp,exp, strstr(exp, "*")-1, mul2);
  216.                                 substringAtstrstr(exp, strstr(exp,"*")+1, endnum, num);
  217.                         }
  218.                         else //132-2*51
  219.                         {
  220.                                 strcpy(mul2, "1");
  221.                                 substringAtstrstr(exp, exp, endnum, num);
  222.                         }
  223.                 }
  224.                 else
  225.                         if(strcontain(exp, '*'))
  226.                         { //2*132
  227.                                 substringAtstrstr(exp, exp, strstr(exp, "*")-1, mul2);
  228.                                 substringAtstrstr(exp, strstr(exp, "*")+1, endnum, num); //all right with NULL ;) (see substringAtstrstr
  229.                         }
  230.                         else
  231.                         {//132 that must be multiplied per n
  232.                                 strcpy(mul2, "1");
  233.                                 if(*exp=='+')
  234.                                         exp++;
  235.                                 strcpy(num, exp);
  236.                         }
  237.  
  238.         if(strcmp(num, "") == 0 || strcmp(mul2, "") == 0)
  239.                 return 0;
  240.         else
  241.                 return 1;
  242. }
  243.  
  244. char *distribute(const char sign, char mul1[], char exp[], char result[]){
  245.         char mul2[5+1], num[5+1], signs[] = {'+', '-'}, *occ=NULL;;
  246.         short lung = 0, pos = 0;
  247.         while(findAddendCmpts(exp+pos, mul2, num))
  248.         {
  249.                 if((occ=strstrArray(exp+pos+1, signs, 2))!=NULL)
  250.                         pos+=occ -(exp+pos);
  251.                 else pos+=strlen(exp+pos);
  252.                 int mulDone = atoi(mul2) * atoi(mul1);
  253.                 if(mulDone > 0) {
  254.                         strcat(result, "+");
  255.                         lung++;
  256.                 }
  257.                 char wtp[intlen(mulDone)+1];
  258.                 strcat(result, int2string(mulDone, wtp));
  259.                 lung+=intlen(mulDone);
  260.                 strcat(result, "*");
  261.                 lung++;
  262.                 strcat(result, num);
  263.                 lung+=strlen(num);
  264.                 *(result+lung+2) = '\0';
  265.                 *num = '\0';
  266.                 *mul2 = '\0';
  267.         }
  268.         return result;
  269. }
  270.  
  271. int main(void) {
  272.         char wtp[10] = "";
  273.         char exp[] = "-13+2*41";
  274.         printf("\ndistribuzione: 2* (%s) =\n%s",exp,distribute('+', "2", exp, wtp));
  275.         return 0;
  276. }



Ora va bene. Ho praticamente usato strchr anzichè strstr e poi ho usato strlen su ciò che viene restituito da strchr nella funzione strdelete. Ma perchè ora va bene e prima no?
P.S.: il doppio post era d'obbligo secondo me. Inoltre ho un problema per l'amministratore: nella versione per cellulare (ottima ;) ), in WP 8.0 Lumia 520, ho rilevato che ogni post viene sdoppiato.

Ultima modifica effettuata da spode il 07/08/2014 alle 13:40
PM Quote