Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/wikka.php on line 315 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/libs/Wakka.class.php on line 176 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/libs/Wakka.class.php on line 463 Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/demetres/public_html/didattica/ae/wikka.php on line 120 Deprecated: Function ereg() is deprecated in /home/demetres/public_html/didattica/ae/libs/Wakka.class.php on line 648 Ingegneria degli Algoritmi: Lezione 4 - Mercoledì 5 marzo 2014 (90 min)

Ingegneria degli Algoritmi

Corso di Laurea in Ingegneria Informatica e Automatica - A.A. 2014-2015

HomePage | Avvisi | Diario lezioni | Programma | Materiale didattico | Esami | Forum | Login
Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/safehtml.php on line 308 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 159 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 161 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 162 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 163 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 165 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 166 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 167 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 243 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 250 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 259 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 266 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 273 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 280 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 467 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 469 Deprecated: Assigning the return value of new by reference is deprecated in /home/demetres/public_html/didattica/ae/3rdparty/core/safehtml/classes/HTMLSax.php on line 471

Lezione 4 - Mercoledì 5 marzo 2014 (90 min)



[ << Lezione precedente | ^ Diario delle lezioni | >> Lezione successiva ]

Argomenti: Tipi array, stringhe, precedenza degli operatori.

Descrizione dettagliata: Riferimenti utili sul C, precedenza degli operatori, stringhe C



Appunti della lezione


Regole di precedenza e associatività degli operatori

Si tenga sempre a portata di mano la tabella delle precedenze degli operatori C.

Precedenza di un operatore rispetto a un altro stabilisce quale operatore va eseguito per primo.

Esempio: in base alla precedenza maggiore del * rispetto al +, la seguente espressione aritmetica a+b*c è equivalente a a+(b*c).

Associatività di un operatore: ordine in cui vengono eseguiti operatori con la stessa precedenza. Si hanno due possibilità: da sinistra verso destra o da destra verso sinistra.

Esempio: in base all'associatività da destra verso sinistra dell'operatore ternario ? (espressione condizionale), in C l'espressione:

a ? b : c ? d : e

è equivalente a:

a ? b : (c ? d : e).


Stringhe in C

[...]


Materiale didattico per la lezione



Esercizi del giorno


Esercizio 1

Scrivere una funzione C che, data una stringa, restituisce una nuova stringa ottenuta da quella di partenza rovesciandola. Si consideri ad esempio il seguente programma di prova:

main.c
#include <stdio.h>

int main(){
    char* s = rovescia("roma");
    printf("%s\n", s); // stampa amor
    free(s);
}


Nota: lo specificatore di formato %s serve per stampare una stringa il cui indirizzo è passato come argomento alla printf.

Esercizio 2

Modificare la funzione scritta nell'esercizio 1 in modo che non dia l'indirizzo della nuova stringa come valore restituito, ma come parametro passato per riferimento. Si veda il seguente programma di prova:

main.c
#include <stdio.h>

int main(){
    char* s;
    rovescia("roma", &s);
    printf("%s\n", s); // stampa amor
    free(s);
}


Esercizio 3

Scrivere una funzione void concat(char* s1, char* s2, char** p) per concatenare due stringhe in un nuovo blocco restituito per parametro.

Esercizio 4

Scrivere una funzione C void occorrenze(char* s, char c, char*** occp, int* np) che, data una stringa s e un carattere c, conta il numero di occorrenze di c in s, che viene restituito in *np, e restituisce in *occp l'indirizzo di un nuovo array di char* contentente gli indirizzi di ciascuna delle occorrenze di c in s.

Si consideri ad esempio il seguente programma di prova:

main.c
#include <stdio.h>

int main(){
    int i, n;
    char** occ;
    occorrenze("ingegneria", 'g', &occ, &n);
    for (i=0; i<n; ++i) printf("%s\n", occ[i]); // stampa gegneria e gneria
    free(occ);
}


Esercizio 5

Scrivere una funzione int tokenize(char* s, char* f, char*** tp, int* np) che, data una stringa s, crea un array di stringhe che contiene tutti i token di s separati da un qualche carattere della stringa f. La funzione deve passare al chiamante in *tp l'indirizzo dell'array dei token creato e in *np la sua dimensione. Inoltre, deve restituire 0 se l'operazione ha avuto successo e -1 altrimenti. La funzione deve richiedere tempo O(strlen(s)*strlen(f)) e usare spazio O(strlen(s)+strlen(f)).

Si consideri ad esempio il seguente programma di prova:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char** t;
    int n, i;
    if (tokenize("uno, due, tre:\n fante cavallo e re", " ,:\n", &t, &n)==-1)
        exit((fprintf(stderr, "error in tokenize\n"), 1));
    for (i=0; i<n; i++) { printf("%s\n", t[i]); free(t[i]); }
    free(t);
    return 0;
}


Il programma dovrebbe stampare:

uno
due
tre
fante
cavallo
e
re


Esercizio 6

Scrivere una funzione void short2binary(short x, char* s) che, dato uno short x e un puntatore s a un buffer (cioè zona di memoria libera) di almeno 8*sizeof(short)+1 byte, scrive in quel buffer una stringa C formata dai caratteri '0' e '1' corrispondenti ai bit di x, con s[0] il bit più significativo di x.

Si consideri ad esempio il seguente programma di prova:

short2binary.c
#include <stdio.h>
#include <stdlib.h>

int main(){
    char* s = malloc(8*sizeof(short)+1);
    short2binary(22971, s);
    printf("%s\n", s); // stampa: 0101100110111011
    free(s);
}


Esercizio 7

In base alle regole di precedenza e associatività degli operatori C, scrivere per ciascuna delle seguenti espressioni un'espressione equivalente che usa parentesi per esplicitare l'ordine di valutazione degli operatori (es: x+y*z => x+(y*z)):

  1. *p++
  2. y = ~x - x + z / 2 * k ^ w
  3. x = y = z
  4. x < y + z
  5. x == y < z
  6. x + y << z ? 1 : x, y
  7. x < y <= z
  8. *p == NULL ? -1 : 0
  9. p = malloc(n*sizeof(int)) == NULL

Esercizio 8

Completare il seguente programma in modo che stampi su schermo i parametri passati al programma (ad esempio da riga di comando):

main.c
int main(int argc, char** argv) {
    ...
    return 0;
}


Si tenga presente che:

  1. argv è un array di stringhe che contiene i parametri passati al programma;
  2. argc è la dimensione di argv.

Sctrivere la soluzione in modo da ottenere il seguente risultato:

$ gcc main.c
$ ./a.out pippo paperino pluto
pippo
paperino
pluto



Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.3
Page was generated in 0.0711 seconds