Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [C] Trasformare stringa

    ciau

    se ho una stringa contenente "prog=0.64155436" come faccio a trasformarla in una con dentro 64,15% ??


  2. #2

    Re: [C] Trasformare stringa

    Originariamente inviato da TestaDiMucca
    ciau ;)

    se ho una stringa contenente "prog=0.64155436" come faccio a trasformarla in una con dentro 64,15% ??

    :D
    codice:
    /* proviamo.c - by michele ;) */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    char *f (char *);
    
    int main (void) {
            char str[] = "prog=0.64155436";
            printf ("%s \n", f (str));
            return (0);
    }
    
    char *f (char *s) {
            float f;
            char *p = (char *) malloc (sizeof (s));
            if (strstr (s, "prog="))
                            s = s + 5;
            f = atof (s);
            f = f * 100;
            sprintf (p, "%2.2f%%", f);
            return (p);
            
    }
    Dovrebbe andare :)

  3. #3
    ma com'è che se implemento il tuo codice nel mio prog, non funzia ?

    codice:
    #include <stdio.h>
    #include <stdio.h>
    #include <string.h>
    
    char *f (char *);
    
    int main(void) {
      char done[100];  
      char *stato;
      FILE *file;  
    
      file = fopen("state.sah", "r"); 
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Seti@Home ha finito di elaborare la WorkUnit.\n");
        return 1;
      }
      else {
            
        while(fgets(done, 100, file)!=NULL) { 
        
            if(strstr(done,"prog=0.")) { printf("%s",done);
            printf ("%s \n", f (done)); }
            
           }
    
        fclose(file);
        system ("PAUSE");
        return 0;
      }
    }
    
    char *f (char *s) {
            float f;
            char *p = (char *) malloc (sizeof (s));
            if (strstr (s, "prog="))
                            s = s + 5;
            f = atof (s);
            f = f * 100;
            sprintf (p, "%2.2f%%", f);
            return (p);  
            
    }
    come output mi da :

    prog=0.64155436
    229339200.00%


  4. #4
    up

  5. #5


    Hai messo due volte stdio, sostituisci con stdlib.

  6. #6
    porca zozza hai ragionissima

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.