Newsgroup: it.comp.lang.c
Titolo: probz with C

il problema sta nel punto dello switch....non riesco a splittare la
stringa choice dopo lo spazio in un'altra stringa..mi fa una cosa
strana..mi considera la parte dopo lo spazio come pos 0..quindi ho 2 pos
0 sull'array di caratteri..strano probabilmente ho fatto errori..per
quanto riguarda le funzioni nn ci sono problemi..ma la parte che nn va č
nello switch...grazie a chi sa rispondermi...
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define size 20
struct listnode {
    int data;            /*definizione della struttura*/
    struct listnode *nextPtr;
    };
 
typedef struct listnode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void inserisci(LISTNODEPTR *, int);
char delete(LISTNODEPTR *, int);       /*Dichiarazione delle funzioni*/
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
main()
{
    LISTNODEPTR startPtr = NULL;
    char choice[size];
    char choice2[size];
    int item,x,m;
 
 
    scanf("%s", &choice);
 
    while(choice[0] != 'f')  {     /*switch per il vari comandi da
eseguire*/
 
        switch (choice[0])  {
 
 
            case 'a':
               for(x = 0;x < strlen(choice);x++)  {
               if(choice[x] == ' ')
                {
                  for(m = x+1;m < strlen(choice);m++)
                   sprintf(choice2,"%s",choice[m]);
                 }  
                   sscanf(choice2,"%d\n", &item);
                 }
                 inserisci(&startPtr, item);
 
                 break;
 
 
            case 'd':
                 if(!isEmpty(startPtr))  {
 
                 for(x = 0;x < strlen(choice);x++)  {
                  if(choice[x] == ' ')
                  {
                    for(m = x+1;m < strlen(choice);m++)
                     sprintf(choice2,"%d\n",choice[m]);
                  }  
                   sscanf(choice2,"%d", &item);
                  }
 
                 if(delete(&startPtr, item))  { 
                 }
                 else
                    printf("%d non trovato.\n", item);
               }
            else
              printf("Lista vuota\n");
              break;
 
 
            case 'p':
                  printList(startPtr);    
 
 
                   break;
             default:
                 printf("Invalid choice.\n\n");
                 break;
             }
 
 
         scanf("%s", &choice);
 
        }
 
   return 0;
}
 
/*Inserire ordinatamente un nuovo valore nella lista*/
void inserisci(LISTNODEPTR *sPtr, int value)
{
    LISTNODEPTR newPtr, previousPtr, currentPtr;
 
    newPtr = malloc(sizeof(LISTNODE));
 
    if(newPtr != NULL)  {
    newPtr->data = value;
    newPtr->nextPtr = NULL;
 
    previousPtr = NULL;
    currentPtr = *sPtr;
 
    while(currentPtr != NULL && value > currentPtr->data)  {
    previousPtr = currentPtr;
    currentPtr = currentPtr->nextPtr;
    }
 
    if(previousPtr == NULL)  {
    newPtr->nextPtr = *sPtr;
    *sPtr = newPtr;
    }
    else  {
    previousPtr-> nextPtr = newPtr;
    newPtr->nextPtr = currentPtr;
    }
   }
   else
     printf("%d not found.\n", value);
 }
 
 /*Elimina un elemento dalla lista*/
 
char delete(LISTNODEPTR *sPtr, int value)
 {
  LISTNODEPTR previousPtr, currentPtr, tempPtr;
 
  if(value == (*sPtr)->data)  {
    tempPtr = *sPtr;
    *sPtr = (*sPtr)->nextPtr;  /*Sfila il nodo*/
    free(tempPtr);
    return value;
  }
  else {
  previousPtr = *sPtr;
  currentPtr = (*sPtr)->nextPtr;
 
    while(currentPtr != NULL && currentPtr->data != value)  {
    previousPtr = currentPtr; /*Passa al...*/
    currentPtr = currentPtr->nextPtr;   /*...prossimo nodo*/
    }
    if(currentPtr != NULL)  {
        tempPtr = currentPtr;
        previousPtr->nextPtr = currentPtr->nextPtr;
        free(tempPtr);
        return value;
        }
     }
     return 0;
   }
 
   /* Se la lista č vuota restituisce 1 altrimenti 0*/
 
int isEmpty(LISTNODEPTR sPtr)
{
  return sPtr == NULL;
  }
 
/*Visualizza la lista*/
void printList(LISTNODEPTR currentPtr)
{
  if(currentPtr == NULL)
   printf("Vuota");
  else  {
    printf("\n");
 
      while(currentPtr != NULL)  {
            printf("%d\n",currentPtr->data);
            currentPtr = currentPtr->nextPtr;
        }
 
      }                          
 
    printf("\n");
 
}
-- 
Posted via Mailgate.ORG Server - 
http://www.Mailgate.ORG


 [CUT]
Ho corretto i tuoi errori. Buon lavoro.
</CODE>
#include <stdio.h>
#include <stdlib.h>
struct listnode {
    int data;
    struct listnode *nextPtr;
};
 
typedef struct listnode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void inserisci(LISTNODEPTR *, int);
void delete(LISTNODEPTR *, int);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
int main()
{
    LISTNODEPTR listPtr = NULL;
    char flag;
    int item;
    while ((flag = getchar()) != 'f') { 
        switch (flag) {
        case 'a':
            scanf("%d", &item);
            inserisci(&listPtr, item);
            break;
        case 'd':
            if(isEmpty(listPtr))
	        printf("Lista vuota\n", item);
            else {
	        scanf("%d", &item);
	        delete(&listPtr, item);
            }
        break;
        case 'p':
            printList(listPtr);    
            break;
        default:
	    printf("Scelta non valida\n");
	    break;
        }
        while (getchar() != '\n');
    }
 
    return 0;
}
 
/*Inserire ordinatamente un nuovo valore nella lista*/
void inserisci(LISTNODEPTR *sPtr, int value)
{
    LISTNODEPTR newPtr, previousPtr, currentPtr;
    newPtr = malloc(sizeof(LISTNODE));
 
    if(newPtr != NULL) {
        newPtr->data = value;
        newPtr->nextPtr = NULL;
 
        previousPtr = NULL;
        currentPtr = *sPtr;
 
        while(currentPtr != NULL && value > currentPtr->data)  {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextPtr;
        }
 
        if(previousPtr == NULL)  {
            newPtr->nextPtr = *sPtr;
            *sPtr = newPtr;
        } else  {
            previousPtr-> nextPtr = newPtr;
            newPtr->nextPtr = currentPtr;
        }
    }
}
 
/*Elimina un elemento dalla lista*/
void delete(LISTNODEPTR *sPtr, int value)
{
    LISTNODEPTR previousPtr, currentPtr, tempPtr;
 
    if(value == (*sPtr)->data) {
        tempPtr = *sPtr;
        *sPtr = (*sPtr)->nextPtr; 
        free(tempPtr);
    } else {
         previousPtr = *sPtr;
         currentPtr = (*sPtr)->nextPtr;
 
         while(currentPtr != NULL && currentPtr->data != value) {
             previousPtr = currentPtr;
             currentPtr = currentPtr->nextPtr;
         }
         if(currentPtr != NULL) {
             tempPtr = currentPtr;
             previousPtr->nextPtr = currentPtr->nextPtr;
             free(tempPtr);
         } else printf("%d non trovato\n", value);
    }   
}
 
int isEmpty(LISTNODEPTR sPtr)
{
    return sPtr == NULL;
}
 
void printList(LISTNODEPTR currentPtr)
{
    if(currentPtr == NULL)
        printf("Lista vuota\n");
    else  {
        while(currentPtr != NULL) {
            printf("%d\n",currentPtr->data);
            currentPtr = currentPtr->nextPtr;
        }
    }
}
</CODE>
-- 
Ciao,
    Giuseppe