it:informatica:linguaggio_c:argc_e_argv_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/argc_e_argv_1?rev=1551188125&do=diff
Differences
This shows you the differences between two versions of the page.
— | it:informatica:linguaggio_c:argc_e_argv_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Argc e Argv (Esempio 1) ====== | ||
+ | **Concetti: | ||
+ | Passaggio di argomenti da linea di comando. | ||
+ | **Testo: | ||
+ | Si scriva un programma C che riceve in ingresso sulla linea di comando una stringa. Il programma dovrà stampare su schermo il contenuto della stringa convertito in maiuscolo. | ||
+ | L' | ||
+ | |||
+ | Esempi: | ||
+ | <code bash> | ||
+ | C:> nome_prog.exe una_stringa_123 | ||
+ | A_STRING_123 | ||
+ | C:> nome_prog.exe -c 4 una_stringa_123 | ||
+ | UNA_ | ||
+ | </ | ||
+ | |||
+ | |||
+ | **Soluzioni: | ||
+ | Verranno fornite due soluzioni. | ||
+ | |||
+ | **Soluzione 1:**\\ | ||
+ | Nella prima soluzione, meno complessa, non si effettua nessun controllo sui parametri passati da linea di comando. Si ipotizza perciò che l' | ||
+ | |||
+ | <file C argc_argv_1a.c> | ||
+ | /* | ||
+ | Implementare un programma in C che riceva da linea di comando una stringa. | ||
+ | Il programma dovrà trasformare la stringa in lettere maiuscole e stamparla sullo schermo. | ||
+ | L' | ||
+ | e stampare solo i primi < | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define LUN 50 | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[]) { | ||
+ | int i; | ||
+ | char s[LUN+1]; | ||
+ | int lunghezza_stringa; | ||
+ | |||
+ | /* Un esempio di esecuzione del programma e': | ||
+ | | ||
+ | le variabili *argv[] e argc conterranno i seguenti valori: | ||
+ | |||
+ | argv[0]=" | ||
+ | argv[1]=" | ||
+ | argv[2]=" | ||
+ | argv[3]=" | ||
+ | argc=4 | ||
+ | |||
+ | E l' | ||
+ | */ | ||
+ | |||
+ | | ||
+ | if(argc==4){ /* Nel caso in cui l' | ||
+ | |||
+ | lunghezza_stringa = atoi(argv[2]); | ||
+ | strncpy(s, argv[3], lunghezza_stringa); | ||
+ | s[lunghezza_stringa] = ' | ||
+ | |||
+ | }else{ /* Nel caso in cui l' | ||
+ | |||
+ | strcpy(s, argv[1]); | ||
+ | lunghezza_stringa = strlen(s); | ||
+ | |||
+ | } | ||
+ | |||
+ | for(i=0; i< | ||
+ | s[i] = toupper(s[i]); | ||
+ | } | ||
+ | | ||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Soluzione 2:**\\ | ||
+ | Nella seconda soluzione, sono stati effettuati tutti i possibili controlli sugli argomenti passati dalla linea di comando. Sfortunatamente, | ||
+ | |||
+ | <file C argc_argv_1b.c> | ||
+ | /* | ||
+ | Implementare un programma in C che riceva da linea di comando una stringa. | ||
+ | Il programma dovrà trasformare la stringa in lettere maiuscole e stamparla sullo schermo. | ||
+ | L' | ||
+ | e stampare solo i primi < | ||
+ | */ | ||
+ | |||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | |||
+ | #define LUN 50 | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | int i; | ||
+ | char s[LUN+1]; | ||
+ | int lunghezza_stringa; | ||
+ | |||
+ | /* Un esempio di esecuzione del programma e': | ||
+ | | ||
+ | le variabili *argv[] e argc conterranno i seguenti valori: | ||
+ | |||
+ | argv[0]=" | ||
+ | argv[1]=" | ||
+ | argv[2]=" | ||
+ | argv[3]=" | ||
+ | argc=4 | ||
+ | |||
+ | E l' | ||
+ | */ | ||
+ | |||
+ | |||
+ | /* Controllo sul numero di argomenti */ | ||
+ | if(argc!=2 && argc!=4) { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | exit(-1) ; /* Come il comando return 1; , ma con exit() il programma viene stoppato | ||
+ | anche se exit() viene chiamata all' | ||
+ | } | ||
+ | | ||
+ | if(argc==4) { /* Nel caso in cui l' | ||
+ | |||
+ | /* Controllo se argv[1] contiene -c */ | ||
+ | if(strcmp(argv[1], | ||
+ | printf(" | ||
+ | exit(-2); | ||
+ | } | ||
+ | |||
+ | /* Controllo se il secondo argomento e' un numero */ | ||
+ | for(i=0; i< | ||
+ | if(isdigit(argv[2][i])==0){ | ||
+ | printf(" | ||
+ | exit(-3); | ||
+ | } | ||
+ | } | ||
+ | lunghezza_stringa = atoi(argv[2]); | ||
+ | /* lunghezza_stringa = atoi(argv[2]); | ||
+ | /* sscanf(argv[2], | ||
+ | printf(" | ||
+ | |||
+ | /* Controllo se il numero (cioe' la variabile lunghezza_stringa) e' positiva */ | ||
+ | if(lunghezza_stringa < 0){ | ||
+ | printf(" | ||
+ | exit(-4); | ||
+ | } | ||
+ | /* Se la stringa e' piu' piccola di lunghezza_stringa, | ||
+ | a lunghezza_stringa la lunghezza effettiva di < | ||
+ | if (lunghezza_stringa > strlen(argv[3])) lunghezza_stringa = strlen(argv[3]); | ||
+ | |||
+ | /* Controllo se la stringa contenuta in argv[3] puo' essere salvata in s */ | ||
+ | if(strlen(argv[3])> | ||
+ | printf(" | ||
+ | exit(-5); | ||
+ | } | ||
+ | /* La stringa e' stata salvata nel vettore s (una soluzione migliore era l' | ||
+ | strncpy(s, argv[3], lunghezza_stringa); | ||
+ | s[lunghezza_stringa] = ' | ||
+ | |||
+ | }else{ /* Nel caso in cui l' | ||
+ | |||
+ | /* Controllo se la stringa contenuta in argv[3] puo' essere salvata in s */ | ||
+ | if(strlen(argv[1])> | ||
+ | printf(" | ||
+ | exit(-5) ; | ||
+ | } | ||
+ | |||
+ | /* Salvataggio della stringa in s */ | ||
+ | strcpy(s, argv[1]); | ||
+ | lunghezza_stringa = strlen(s); | ||
+ | } | ||
+ | |||
+ | for(i=0; i< | ||
+ | if(!isprint(s[i])){ | ||
+ | printf(" | ||
+ | exit(-6); | ||
+ | } | ||
+ | | ||
+ | s[i] = toupper(s[i]); | ||
+ | } | ||
+ | | ||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/argc_e_argv_1?rev=1551188125&do=diff