Repository 32bit  Forum
Repository 64bit  Wiki

Problema tema d'esame [RISOLTO]

Forum dedicato alla programmazione.

Moderatore: Staff

Regole del forum
1) Citare sempre la versione di Slackware usata e la versione del Kernel. Questi dati aiutano le persone che possono rispondere.
2) Specificare sempre il tipo di shell (bash, sh, csh, etc...)
3) Leggere attentamente le risposte ricevute
4) Scrivere i messaggi con il colore di default, evitare altri colori.
5) Scrivere in Italiano o in Inglese, se possibile grammaticalmente corretto, evitate stili di scrittura poco chiari, quindi nessuna abbreviazione tipo telegramma o scrittura stile SMS o CHAT.
6) Appena registrati è consigliato presentarsi nel forum dedicato.

La non osservanza delle regole porta a provvedimenti di vari tipo da parte dello staff, in particolare la non osservanza dell'ultima regola porta alla cancellazione del post e alla segnalazione dell'utente. In caso di recidività l'utente rischia il ban temporaneo.

Problema tema d'esame [RISOLTO]

Messaggioda Blallo » ven set 04, 2009 20:02

Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLN 10
#define MAXWORD 30

typedef struct adv_l
{
   char email[MAXWORD];
   struct adv_l *next;
}adv_l;

void open_file(FILE *fp, char fileIN[MAXLN]);
void open_w_file(FILE *fp, char fileIn[MAXLN]);
void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD]);
void correct(char tmp[MAXWORD]);
void verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD]);
adv_l *put(adv_l *head, char tmp[MAXWORD]);
void destroy_list(adv_l *head);
void print_file(FILE *fp, adv_l *head);

int main()
{
   FILE *fp, *fpTMP, *fpOUT;
   adv_l *head=NULL;
   char fIN[MAXLN], fOUT[MAXLN], fBIS[MAXLN], tmp[MAXWORD];
   int found=0, em_1=0, em_2=0;

   printf("Inserire nome file input:\n");
   scanf("%s", fIN);
   open_file(fp, fIN);
   printf("Inserire nome file output:\n");
   scanf("%s", fOUT);
   open_w_file(fpOUT, fOUT);
   while(!feof(fp))
   {
      fscanf(fp, "%s", fBIS);
      open_file(fpTMP, fBIS);
      while(!feof(fpTMP))
      {
         search(fpTMP, &found, &em_1, tmp);
         if(found==1)
         {
            correct(tmp);
            verify_and_put(head, &em_2, tmp);
         }
      }
      fclose(fpTMP);
   }
   printf_file(fpOUT, head);
   fclose(fp);
   fclose(fpOUT);
   printf("Indirizzi e-mail nel file input: %d\n", em_1);
   printf("Indirizzi e-mail nel file output: %d\n", em_2);
   return EXIT_SUCCESS;
}

void open_file(FILE *fp, char fileIN[MAXLN])
{
   if((fp=fopen(fileIN,"r"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void open_w_file(FILE *fp, char fileIN[MAXLN])
{
   if((fp=fopen(fileIN,"w"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD])
{
   char *s;
   
   fscanf(fp, "%s", tmp);
   s=tmp;

   while(s!=NULL)
   {
      if(strcmp(s,"@")==0)
      {
         *(found)=1;
         *(em_1)++;
         break;
      }
      s++;
   }
}

void correct(char tmp[MAXWORD])
{
   int cont=0;
   char *s, *t;

   s=tmp;

   while(s!=NULL)
      if(isalpha(*s))
         cont++;
   t=(char *)malloc(sizeof(char)*cont + 1);
   s=tmp;

   while(s!=NULL)
   {
      if(isalpha(*s))
      {
         if(isupper(*s))
            tolower(*s);
         *t=*s;
         t++;
      }
      s++;
   }
   strcpy(tmp,t);
   free(t);
}

void verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD])
{
   int ver;
   adv_l *s;

   s=head;

   if(s==NULL)
   {
      head=put(head,tmp);
      *(em_2)++;
   }
   else
   {
      while(s!=NULL)
      {
         if(strcmp(s->email,tmp)==0)
         {
            ver=0;
            break;
         }
         else
            ver=1;
         s=s->next;
      }
   }

   if(ver==1)
   {
      head=put(head,tmp);
      *(em_2)++;
   }
}

adv_l *put(avd_l *head, char tmp[MAXWORD])
{
   adv_l *t;

   t=(adv_l *)malloc(sizeof(adv_l));
   strcpy(t->email,tmp);
   t->next=head;
   head=t;

   return head;
}

void destroy_list(adv_l *head)
{
   adv_l *t;

   while(head!=NULL)
   {
      t=head;
      head=head->next;
      free(t);
   }

   head=NULL;
}

void print_file(FILE *fp, adv_l *head)
{
   adv_l *s;

   while(s!=NULL)
   {
      fprintf(fp, "%s", s->email);
      s=s->next;
   }
}


questo programma legge il file files.txt
Codice: Seleziona tutto
nome1.txt
nome2.txt


apre i 2 file elencati dentro
nome1
Codice: Seleziona tutto
I docenti del corso di Tecniche e Linguaggi di Programmazione sono:
I Corso: prof. Elio Piccolo, tel. 7002, email: Elio Piccolo <elio.piccolo@polito.it>
II Corso: prof. Pietro Laface, tel. 7004, email: Pietro Laface <pietro.laface@polito.it>
III Corso: prof. Stefano Quer, tel. 7006, email: Stefano Quer <stefano.quer@polito.it>

nome2
Codice: Seleziona tutto
Write to goofy@disney.com to get help on this assignment:
Me@myhome.it is my address, while OGRE@darkforest.net is
not mine, but Sherk's, so you shouldn't write to
ogre@darkforest.net
Both <liza@polito.it> and "pamela@polito.it" are fancy
Addresses (and you can write them as Liza@Polito.IT
or 'PaMeLa@pOlItO.IT'

e deve darmi un file di output con tutti gli indirizzi email, senza duplicati, apici, caratteri "< > " e tutti in minuscolo.
Mi da errore alla funzione puts....qualcuno ha un'idea?
Ultima modifica di Blallo il dom set 06, 2009 1:20, modificato 1 volta in totale.
Io sono il detective Arsenio Magret, e porto sempre la camicia TATUATA!
Avatar utente
Blallo
Packager
Packager
 
Messaggi: 3054
Iscritto il: ven ott 12, 2007 11:37
Località: Torino / Torremaggiore (FG)
Nome Cognome: Savino Liguori
Slackware: 14 x64 / 12.2
Kernel: 3.2.x
Desktop: Xfce

Re: Problema tema d'esame [URGENTISSIMO!]

Messaggioda Blallo » ven set 04, 2009 22:28

Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLN 10
#define MAXWORD 30

typedef struct adv_l
{
   char email[MAXWORD];
   struct adv_l *next;
}adv_l;

void open_file(FILE *fp, char fileIN[MAXLN]);
void open_w_file(FILE *fp, char fileIn[MAXLN]);
void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD]);
void correct(char tmp[MAXWORD]);
void verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD]);
adv_l *put(adv_l *head, char tmp[MAXWORD]);
void destroy_list(adv_l *head);
void print_file(FILE *fp, adv_l *head);

int main()
{
   FILE *fp, *fpTMP, *fpOUT;
   adv_l *head=NULL;
   char fIN[MAXLN], fOUT[MAXLN], fBIS[MAXLN], tmp[MAXWORD];
   int found=0, em_1=0, em_2=0;

   printf("Inserire nome file input:\n");
   scanf("%s", fIN);
   open_file(fp, fIN);
   printf("Inserire nome file output:\n");
   scanf("%s", fOUT);
   open_w_file(fpOUT, fOUT);
   while(!feof(fp))
   {
      fscanf(fp, "%s", fBIS);
      open_file(fpTMP, fBIS);
      while(!feof(fpTMP))
      {
         search(fpTMP, &found, &em_1, tmp);
         if(found==1)
         {
            correct(tmp);
            verify_and_put(head, &em_2, tmp);
         }
      }
      fclose(fpTMP);
   }
   print_file(fpOUT, head);
   fclose(fp);
   fclose(fpOUT);
   printf("Indirizzi e-mail nel file input: %d\n", em_1);
   printf("Indirizzi e-mail nel file output: %d\n", em_2);
   return EXIT_SUCCESS;
}

void open_file(FILE *fp, char fileIN[MAXLN])
{
   if((fp=fopen(fileIN,"r"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void open_w_file(FILE *fp, char fileIN[MAXLN])
{
   if((fp=fopen(fileIN,"w"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD])
{
   char *s;

   fscanf(fp, "%s", tmp);
   s=tmp;

   while(*s!='\0')
   {
      if(strcmp(s,"@")==0)
      {
         *(found)=1;
         *(em_1)++;
         break;
      }
      s++;
   }
}

void correct(char tmp[MAXWORD])
{
   int cont=0;
   char *s, *t;

   s=tmp;

   while(s!=NULL)
      if(isalpha(*s))
         cont++;
   t=(char *)malloc(sizeof(char)*cont + 1);
   s=tmp;

   while(*s!='\0')
   {
      if(isalpha(*s))
      {
         if(isupper(*s))
            *s=tolower(*s);
         *t=*s;
         t++;
      }
      s++;
   }
   strcpy(tmp,t);
   free(t);
}

void verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD])
{
   int ver;
   adv_l *s;

   s=head;

   if(s==NULL)
   {
      head=put(head,tmp);
      *(em_2)++;
   }
   else
   {
      while(s!=NULL)
      {
         if(strcmp(s->email,tmp)==0)
         {
            ver=0;
            break;
         }
         else
            ver=1;
         s=s->next;
      }
   }

   if(ver==1)
   {
      head=put(head,tmp);
      *(em_2)++;
   }
}

adv_l *put(adv_l *head, char tmp[MAXWORD])
{
   adv_l *t;

   t=(adv_l *)malloc(sizeof(adv_l));
   strcpy(t->email,tmp);
   t->next=head;
   head=t;

   return head;
}

void destroy_list(adv_l *head)
{
   adv_l *t;

   while(head!=NULL)
   {
      t=head;
      head=head->next;
      free(t);
   }

   head=NULL;
}

void print_file(FILE *fp, adv_l *head)
{
   adv_l *s;

   while(s!=NULL)
   {
      fprintf(fp, "%s", s->email);
      s=s->next;
   }
}

ho corretto un paio di roba...ora il problema è che nel main quando da fp mi deve leggere una stringa per metterla in fBIS non legge nulla!
Io sono il detective Arsenio Magret, e porto sempre la camicia TATUATA!
Avatar utente
Blallo
Packager
Packager
 
Messaggi: 3054
Iscritto il: ven ott 12, 2007 11:37
Località: Torino / Torremaggiore (FG)
Nome Cognome: Savino Liguori
Slackware: 14 x64 / 12.2
Kernel: 3.2.x
Desktop: Xfce

Re: Problema tema d'esame [URGENTISSIMO!]

Messaggioda Blallo » sab set 05, 2009 3:24

Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLN 10
#define MAXWORD 30

typedef struct adv_l
{
   char email[MAXWORD];
   struct adv_l *next;
}adv_l;

void open_file(FILE **fp, char fileIN[MAXLN]);
void open_w_file(FILE **fp, char fileIn[MAXLN]);
void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD]);
void correct(char tmp[MAXWORD]);
adv_l *verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD]);
adv_l *put(adv_l *head, char tmp[MAXWORD]);
void destroy_list(adv_l *head);
void print_file(FILE *fp, adv_l *head);

int main()
{
   FILE *fp=NULL, *fpTMP, *fpOUT;
   adv_l *head=NULL;
   char fIN[MAXLN], fOUT[MAXLN], fBIS[MAXLN], tmp[MAXWORD];
   int found=0, em_1=0, em_2=0;

   printf("Inserire nome file input:\n");
   scanf("%s", fIN);
   open_file(&fp, fIN);
   printf("Inserire nome file output:\n");
   scanf("%s", fOUT);
   open_w_file(&fpOUT, fOUT);
   while(!feof(fp))
   {
      fscanf(fp, "%s", fBIS);
      open_file(&fpTMP, fBIS);
      while(!feof(fpTMP))
      {
         search(fpTMP, &found, &em_1, tmp);
         if(found==1)
         {
            correct(tmp);
            head=verify_and_put(head, &em_2, tmp);
         }
      }
      fclose(fpTMP);
   }
   print_file(fpOUT, head);
   fclose(fp);
   fclose(fpOUT);
   printf("Indirizzi e-mail nel file input: %d\n", em_1);
   printf("Indirizzi e-mail nel file output: %d\n", em_2);
   return EXIT_SUCCESS;
}

void open_file(FILE **fp, char fileIN[MAXLN])
{
   if(((*fp)=fopen(fileIN,"r"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void open_w_file(FILE **fp, char fileIN[MAXLN])
{
   if(((*fp)=fopen(fileIN,"w"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD])
{
   int i=0;

    (*found)=0;
   fscanf(fp, "%s", tmp);

   while(tmp[i]!='\0')
   {
      if(tmp[i] == '@')
      {
         (*found)=1;
         (*em_1)++;
         printf("* %s\n", tmp);
         break;
      }
      i++;
   }
}

void correct(char tmp[MAXWORD])
{
   int cont=0, i=0, j=0;
   char *t;


   while(tmp[i]!='\0')
    {
        if(isalpha(tmp[i]) || tmp[i] == '@' || tmp[i]=='.')
         cont++;
        i++;
    }
   t=(char *)malloc(sizeof(char)*(cont + 1));
    i=0;
   while(tmp[i]!='\0')
   {
      if(isalpha(tmp[i]))
      {
         if(isupper(tmp[i]))
            tmp[i]=tolower(tmp[i]);
         t[j]=tmp[i];
         j++;
      }
      else if(tmp[i]=='@' || tmp[i]=='.')
      {
          t[j]=tmp[i];
         j++;
      }
      i++;
   }
   tmp[cont+1]='\0';
   strcpy(tmp,t);
    free(t);
   printf("%s %s\n", tmp, t);
}

adv_l *verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD])
{
   int ver=0;
   adv_l *s;

   s=head;

   if(s == NULL)
   {
      head=put(head,tmp);
      (*em_2)++;
   }
   else
   {
      while(s!=NULL)
      {
         if(strcmp(s->email,tmp)==0)
         {
            ver=0;
            break;
         }
         else
            ver=1;
         s=s->next;
      }
   }

   if(ver==1)
   {
      head=put(head,tmp);
      (*em_2)++;
   }

   return head;
}

adv_l *put(adv_l *head, char tmp[MAXWORD])
{
   adv_l *t=NULL;

   t=(adv_l *)malloc(sizeof(adv_l));
   strcpy(t->email,tmp);
   t->next=head;
   head=t;

   return head;
}

void destroy_list(adv_l *head)
{
   adv_l *t;

   while(head!=NULL)
   {
      t=head;
      head=head->next;
      free(t);
   }

   head=NULL;
}

void print_file(FILE *fp, adv_l *head)
{
   adv_l *s;

    s=head;
   while(s!=NULL)
   {
      fprintf(fp, "%s\n", s->email);
      s=s->next;
   }
}

UPDATE ha qualche problema con la tmp, che tiene traccia anche della stringa precedente (non capisco perchè)
Io sono il detective Arsenio Magret, e porto sempre la camicia TATUATA!
Avatar utente
Blallo
Packager
Packager
 
Messaggi: 3054
Iscritto il: ven ott 12, 2007 11:37
Località: Torino / Torremaggiore (FG)
Nome Cognome: Savino Liguori
Slackware: 14 x64 / 12.2
Kernel: 3.2.x
Desktop: Xfce

Re: Problema tema d'esame [URGENTISSIMO!]

Messaggioda Blallo » sab set 05, 2009 13:06

HO risolto il problema...grazie a tutti lo stesso..posto la soluzione a scopo didattico
Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLN 10
#define MAXWORD 30

typedef struct adv_l
{
   char email[MAXWORD];
   struct adv_l *next;
}adv_l;

void open_file(FILE **fp, char fileIN[MAXLN]);
void open_w_file(FILE **fp, char fileIn[MAXLN]);
void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD]);
void correct(char tmp[MAXWORD]);
adv_l *verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD]);
adv_l *put(adv_l *head, char tmp[MAXWORD]);
void destroy_list(adv_l *head);
void print_file(FILE *fp, adv_l *head);

int main()
{
   FILE *fp=NULL, *fpTMP, *fpOUT;
   adv_l *head=NULL;
   char fIN[MAXLN], fOUT[MAXLN], fBIS[MAXLN], tmp[MAXWORD];
   int found=0, em_1=0, em_2=0;

   printf("Inserire nome file input:\n");
   scanf("%s", fIN);
   open_file(&fp, fIN);
   printf("Inserire nome file output:\n");
   scanf("%s", fOUT);
   open_w_file(&fpOUT, fOUT);
   while(!feof(fp))
   {
      if((fscanf(fp, "%s", fBIS))<1)
            break;
      open_file(&fpTMP, fBIS);
      while(!feof(fpTMP))
      {
          strcpy(tmp,"0");
         search(fpTMP, &found, &em_1, tmp);
         if(found==1)
         {
            correct(tmp);
            head=verify_and_put(head, &em_2, tmp);
         }
      }
      fclose(fpTMP);
   }
   print_file(fpOUT, head);
   fclose(fp);
   fclose(fpOUT);
   printf("Indirizzi e-mail nel file input: %d\n", em_1);
   printf("Indirizzi e-mail nel file output: %d\n", em_2);
   return EXIT_SUCCESS;
}

void open_file(FILE **fp, char fileIN[MAXLN])
{
   if(((*fp)=fopen(fileIN,"r"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void open_w_file(FILE **fp, char fileIN[MAXLN])
{
   if(((*fp)=fopen(fileIN,"w"))==NULL)
   {
      printf("Errore apertura file\n");
      exit(1);
   }
}

void search(FILE *fp, int *found, int *em_1, char tmp[MAXWORD])
{
   int i=0;

    (*found)=0;
   if((fscanf(fp, "%s", tmp))<1)
        return ;

   while(tmp[i]!='\0')
   {
      if(tmp[i] == '@')
      {
         (*found)=1;
         (*em_1)++;
         printf("* %s\n", tmp);
         break;
      }
      i++;
   }
}

void correct(char tmp[MAXWORD])
{
   int cont=0, i=0, j=0;
   char *t;


   while(tmp[i]!='\0')
    {
        if(isalpha(tmp[i]) || tmp[i] == '@' || tmp[i]=='.')
         cont++;
        i++;
    }
   t=(char *)malloc(sizeof(char)*(cont + 1));
    i=0;
   while(tmp[i]!='\0')
   {
      if(isalpha(tmp[i]))
      {
         if(isupper(tmp[i]))
            tmp[i]=tolower(tmp[i]);
         t[j]=tmp[i];
         j++;
      }
      else if(tmp[i]=='@' || tmp[i]=='.')
      {
          t[j]=tmp[i];
         j++;
      }
      i++;
   }
   t[j]='\0';
   strcpy(tmp,t);
    free(t);
   printf("%s\n", tmp);
}

adv_l *verify_and_put(adv_l *head, int *em_2, char tmp[MAXWORD])
{
   int ver=0;
   adv_l *s;

   s=head;

   if(s == NULL)
   {
      head=put(head,tmp);
      (*em_2)++;
   }
   else
   {
      while(s!=NULL)
      {
         if(strcmp(s->email,tmp)==0)
         {
            ver=0;
            break;
         }
         else
            ver=1;
         s=s->next;
      }
   }

   if(ver==1)
   {
      head=put(head,tmp);
      (*em_2)++;
   }

   return head;
}

adv_l *put(adv_l *head, char tmp[MAXWORD])
{
   adv_l *t=NULL;

   t=(adv_l *)malloc(sizeof(adv_l));
   strcpy(t->email,tmp);
   t->next=head;
   head=t;

   return head;
}

void destroy_list(adv_l *head)
{
   adv_l *t;

   while(head!=NULL)
   {
      t=head;
      head=head->next;
      free(t);
   }

   head=NULL;
}

void print_file(FILE *fp, adv_l *head)
{
   adv_l *s;

    s=head;
   while(s!=NULL)
   {
      fprintf(fp, "%s\n", s->email);
      s=s->next;
   }
}
Io sono il detective Arsenio Magret, e porto sempre la camicia TATUATA!
Avatar utente
Blallo
Packager
Packager
 
Messaggi: 3054
Iscritto il: ven ott 12, 2007 11:37
Località: Torino / Torremaggiore (FG)
Nome Cognome: Savino Liguori
Slackware: 14 x64 / 12.2
Kernel: 3.2.x
Desktop: Xfce


Torna a Programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite