Lab 0: 23 Marzo 2015 - Intranet...

45
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Lab 0: 23 Marzo 2015 Marco D. Santambrogio – [email protected] Gianluca Durelli - [email protected] Ver. aggiornata al 19 Marzo 2015

Transcript of Lab 0: 23 Marzo 2015 - Intranet...

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Lab 0: 23 Marzo 2015

Marco D. Santambrogio – [email protected] Gianluca Durelli - [email protected] Ver.  aggiornata  al  19  Marzo  2015  

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Obiettivi

•  Imparare ad usare l’ambiente di sviluppo §  Cosa è e come funziona un compilatore §  Sistemazione degli ambienti di sviluppo §  Comandi base da linea di comando

•  Esercitarsi sui costrutti visti a lezione §  Inserimento dati da tastiera §  Controllo dei dati §  Uso delle funzioni

2

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Lab 1: Agenda

•  Informazioni sui laboratori

•  Benvenuti nel fantastico mondo del C

•  Esercizi Lab1

3

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Lab 1: Agenda

•  Informazioni sui laboratori

•  Benvenuti nel fantastico mondo del C

•  Esercizi Lab1

4

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Calendario dei Labs

•  Dove: EG 7/L.26.14

•  Quando §  Lunedì: 15.30 – 18.30

•  Macchine per il lab: §  Il vostro portatile § Macchina lab: virtualdesktop

5

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Struttura

•  Tolte le slide di “contesto” su §  Obiettivi §  Elenco esercizi

•  Approccio dritto al sodo, niente "fronzoli": §  Introduzione all'uso di un editor di testo e di una shell §  Non useremo un IDE (e.g., code::blocks, eclipse, ...)

•  È una proposta, ci sono molti modi diversi di ottenere gli stessi risultati

•  Per info, fare riferimento al sito del corso: Home: http://home.dei.polimi.it/santambr/dida/ieim/2015/ Calendario: http://home.dei.polimi.it/santambr/dida/ieim/calendario.htm

6

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Installation Party: Agenda

•  Informazioni sui laboratori

•  Benvenuti nel fantastico mondo del C §  Ambiente di sviluppo § Un primo programma

•  Esercizi Lab1

7

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Ambienti di sviluppo

•  C §  Un compilatore C

•  g++, gcc •  MinGW: http://www.mingw.org/

§  Un editor/IDE •  Code::Blocks: http://www.codeblocks.org/ •  Geany: http://www.geany.org/ •  DevC++: http://www.bloodshed.net/devcpp.html

•  Virtual Machine già pronte §  http://home.deib.polimi.it/santambr/dida/

ieim/2015/tools.htm •  Ambiente PoliMI

8

OR

OR OR

OR

AND

(

)

OR

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Scriviamo il programma

9

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Scriviamo il programma

10

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Scriviamo il programma

•  Virtual Desktop §  https://virtualdesktop.polimi.it

11

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Per macchine PoliMI

https://virtualdesktop.polimi.it

12

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Scriviamo il programma

•  Virtual Desktop §  https://virtualdesktop.polimi.it

•  Lanciando CodeBlock §  http://tinyurl.com/1st-codeblock

13

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Scriviamo il programma

•  Virtual Desktop §  https://virtualdesktop.polimi.it

•  Lanciando CodeBlock §  http://tinyurl.com/1st-codeblock

•  Usando la VM (e.g. Didattica) e quindi lanciando Geany

14

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Desktop “VM”

15

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Come eseguire Geany

16

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Come eseguire Geany

17

“Cliccando” l’icona

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Come eseguire Geany

18

Dal menù

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Ecco Geany

19

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Scriviamo il programma

•  Virtual Desktop §  https://virtualdesktop.polimi.it

•  Lanciando CodeBlock §  http://tinyurl.com/1st-codeblock

•  Usando la VM (e.g. Didattica) e quindi lanciando Geany

•  Usando la VM (e.g. Didattica) e aprendo un editor di testo

20

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Come si lanciano…

21

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Ambiente – “Linux” o VM

22

Shell: bash

Editor: gedit

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Il primo programma: ciao mondo

23

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

24

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

25

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

•  Come faccio il #?

26

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

•  Come faccio il #? §  ALT + à

27

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

•  Come faccio il #? §  ALT + à

•  Come faccio le graffe?

28

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

•  Come faccio il #? §  ALT + à

•  Come faccio le graffe? §  (shift)+(alt gr)+(è) per la { §  (shift)+(alt gr)+(+) per la }

29

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Provate voi…

•  Come faccio il #? §  ALT + à

•  Come faccio le graffe? §  (shift)+(alt gr)+(è) per la { §  (shift)+(alt gr)+(+) per la }

•  Ora scrivete il vostro primo programma!

30

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Tutorial: bash e gcc

•  Ecco alcuni comandi utili per utilizzare la shell bash: §  $ pwd - (Print Working Directory) visualizza la directory

corrente §  $ ls - elenca i file presenti nella cartella corrente §  $ cd CARTELLA - cambia la cartella corrente a CARTELLA §  $ ./PROG - esegue il programma specificato (se è nella

cartella corrente)

•  Questi pochi comandi sono sufficienti per individuare ed eseguire un programma §  Per compilare un sorgente, abbiamo usato il comando gcc

•  gcc FILE.c -o PROG §  Questo comando traduce il codice sorgente presente in

FILE.c e lo salva nel file eseguibile PROG

31

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

HowTo: compilare ed eseguire

•  Scritto e salvato il file sorgente (e.g., sul Desktop, nel file primo.c),

•  Dobbiamo compilare ed eseguire un programma utilizzando una shell (e.g., bash) $ cd Desktop $ gcc primo.c –o exe $ ./exe

32

�������������� �

�������������� ���� �������� ������������������������������������������� �������������������������������� ������������������������������������������������ ���������������������� ������������������������������� ��������� ������������������ ������ �� ����!������������� ��� ��� �"�#!#�� ������ ���$�������������� ����"�#!#�� ��$

������%

&����%���� ������������

����������

'���������()�

)� �(*������������+�!��� ��

•  Per poter eseguire il nostro programma, bisogna tradurlo in codice macchina §  Questa operazione viene eseguita dal compilatore (e.g.,

gcc)

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Installation Party: Agenda

•  Informazioni sui laboratori •  Benvenuti nel fantastico mondo del C

•  Esercizi Installation Party

33

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Da Lezione: Inserimento dati

•  Problema §  Richiedi all’utente la sua altezza in

centrimentri e mostrala a video in metri

•  Pseudocodice 1. Scrivi “quanto sei alto?” 2. Leggi altezzacm 3. Altezzam = alteccacm/100 4. Scrivi “sei alto: altezzam”

34

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Pseudocodice vs Codice C

•  Pseudocodice

1.  Scrivi “quanto sei alto?” 2.  Leggi altezzacm 3.  Altezzam = alteccacm/100 4.  Scrivi “sei alto: altezzam”

35

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Un primo errore

36

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Un secondo errore

37

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Un terzo errore

38

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Soluzione corretta

39

L’importanza dei tipi di dato

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Exe 1: Stampa a video

•  Scrivere un programma in C che visuallizzi a video la seguente scritta: IEIM - 2015 Laboratorio 1 Studente: Nome Cognome Matricola: #mat

40

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Exe 2: Trova errori

•  Il seguente programma non funziona…

41

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Exe 3: Mostra caratteri

•  Si scriva un programma che richieda l’inserimento di un carattere e lo mostri a video, con anche la sua codifica ASCII

42

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Exe 4: Gestione errori sui caratteri

•  Si scriva un programma in C che richiede l’inserimento di due caratteri e li mostra a video

43

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Exe 5: Inserimento dati

•  Si scriva un “convertitore” di unità dal sistema SI al US customary units § Un programma per conversione

•  Ci interessano solo le seguenti conversioni §  piedi in metri (1ft= 0.3048 m) §  pound in Kg (1lb = 453.59237g) §  yard in metri (1yd = 0.9144m)

44

DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE

Fine…

45