Il while ripete la chiamata a getchar finche' la getchar restituisce un valore, ovvero c'e' un tasto nel buffer. Termina quando il buffer e' vuoto ...
Io ho capito che la funzione getchar,che non prende parametri,o meglio,come parametro usa void,blocca il programma e aspetta un carattere da tastiera mettendolo in un buffer.
Qualunque altro carattere segue viene messo nel buffer fino a quando non si digita invio.
A quel punto la funzione cede il controllo al programma e restituisce un intero che corrisponde al primo carattere digitato.Pero' ancora non ne ho capito l'uso nella condizione. !getchar equivale a non getchar.Oppure !getchar significa getchar=0.
Ma getchar=0 in realta' a quale corrisponde,dato che e' gia presente nel ciclo for?
Di seguito riporto il contenuto di quello che fa il programma:
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
void reverse(char s[]) {
int c, i, j;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int n,char s[]) {
int i,segno;
if ((segno=n) < 0)
n= -n; i=0; do {
s[i++]= n % 10 + '0';
} while ((n /= 10) > 0);
if (segno < 0) s[i++]= '-';
s[i]= '\0'; reverse(s);
}
int ParseString(char * s, int * a, int * nMin, int * nMax) {
int nRet = 0;
int i = 0;
int j = 0;
char strInt[12];
int num;
while ( 1 ) {
/* if ( isdigit( *(s + i) ) ) {
*(strInt + j) = *(s + i);
++j;
} */
if ( isdigit( *(s + i) ) || *(s + i) == '-' ) {
if ( (*(s + i) == '-') && (i > 0) ) {
if ( *(s + i - 1) != ' ' ) return 0;
}
*(strInt + j) = *(s + i);
j++;
}
else if ( *(s + i) == ' ' || *(s + i) == '\n' ) {
*(strInt + j) = '\0';
if ( strlen(strInt) > 0 ) {
if ( a ) {
num = atoi(strInt);
a[nRet] = num;
if ( nRet == 0 ) {
*nMin = num;
*nMax = num;
}
else {
*nMin = num < *nMin ? num : *nMin;
*nMax = num > *nMax ? num : *nMax;
}
}
++nRet;
j = 0;
*strInt = '\0';
}
}
else {
return 0;
}
if ( *(s + i) == '\n' ) break;
++i;
}
return nRet;
}
void BubbleSort(int * pInt, int Count)
{
register int a, b;
register int temp;
for (a = 1; a < Count; ++a) {
for (b = Count; b >= a; --b) {
if ( pInt[b - 1] > pInt[b] ) {
temp = pInt[b - 1];
pInt[b - 1] = pInt[b];
pInt[b] = temp;
}
}
}
}
main()
{
int * pInt = NULL;
int Count = 0;
char str[512];
int nMin, nMax;
int indice,k;
char strArray[512];
char strNum[12];
char x;
printf("\n\tMENU DI PROVA\n\n\ta) Per immettere dati\n\tb) Per determinare il maggiore\n\tc) Per determinare il minore\n\td) Per ordinare\n\te) Per visualizzare\n\n");
printf("[PER USCIRE PREMERE q]\t\tScelta(a,b,c,d,e): ");
x=getchar();
do{
fflush(stdin);
switch(x) {
case 'a':
while(!getchar());
printf("In esecuzione l'opzione a..\n");
printf("Immettere dei numeri interi(separati da uno spazio):");
for(indice=0;( str[indice]=getchar() )!='\n';indice++);
Count = ParseString(str, NULL, NULL, NULL);
if ( Count > 0 ) {
if ( pInt ) free(pInt);
pInt = (int*)malloc(Count * sizeof(int));
if ( !pInt ) {
printf("Memoria insufficiente.\n");
break;
}
ParseString(str, pInt, &nMin, &nMax);
printf("Totale numeri immessi: %d\nUltimo numero immesso: %d\n", Count, pInt[Count - 1]);
}
else printf("Input non valido.\n");
break;
case 'b':
printf("In esecuzione l'opzione b..\n");
printf("Il maggiore é: %d",nMax);
break;
case 'c':
printf("In esecuzione l'opzione c..\n");
printf("Il minore é: %d",nMin);
break;
case 'd':
printf("In esecuzione l'opzione d..\n");
if ( Count > 0 ) {
BubbleSort(pInt, Count);
printf("Array ordinato:\n");
strcpy(strArray, "");
for ( k = 0; k < Count; k++ ) {
itoa(pInt[k], strNum);
strcat(strArray, strNum);
strcat(strArray, " ");
}
printf(strArray);
}
else {
printf("L'array e' vuoto.\n");
}
break;
case 'e':
printf("In esecuzione l'opzione e..\n");
if ( Count > 0 ) {
printf("Numeri immessi:\n");
strcpy(strArray, "");
for ( k = 0; k < Count; k++ ) {
itoa(pInt[k], strNum);
strcat(strArray, strNum);
strcat(strArray, " ");
}
printf(strArray);
}
else {
printf("L'array e' vuoto.\n");
}
break;
default:
printf("Opzione inesistente\n");
break;
}
printf("\n");
printf("\n\tMENU DI PROVA\n\n\ta) Per immettere dati\n\tb) Per determinare il maggiore\n\tc) Per determinare il minore\n\td) Per ordinare\n\te) Per visualizzare\n\n");
printf("[PER USCIRE PREMERE q]\t\tScelta(a,b,c,d,e): ");
x = getchar();
}
while(x!='q');
if(pInt) free(pInt);
}