AN Fondam98 Input Output Input Output I files standard di I/O.

40
AN Fondam98 Input Outpu t Input Output I files standard di I/O

Transcript of AN Fondam98 Input Output Input Output I files standard di I/O.

Page 1: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Input Output

I files standard di I/O

Page 2: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

I dispositivi di I/O

Tastiera (I) Video (O) Linee di trasmissione (I/O) Stampante, plotter, tavole grafica (O) Sensori (I) Attuatori (O)

Page 3: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

I/O : livelli di astrazione

Il sistema operativo

– fa vedere i dispositivi fisici come files

– permette di usare finestre video come dispostivi virtuali di I/O

java.io fa vedere i files come stream java.awt permette di realizzare dispositivi

virtuali grafici

Page 4: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

La dimensione interazione

Se il programma realizza un algoritmo, allora le sezioni di I/O devono essere distinte, per amor di modularita’ e risuabilita’, dalle sezioni di elaborazione

Programmi che mescolano operazioni di I/O all’elaborazione costituiscono una diversa categoria di sistemi rispetto agli algoritmi

Page 5: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Dispositivi standard

I dispositivi standard di I/O sono sequenza di caratteri– C: stdin, stdout– Java: System.in, System.out

Sono visti, grazie al Sistema Operativo, come files di caratteri (textfiles)

Page 6: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Dispositivi “testo” in javaCaller

stdOut: dispositivo standard di uscita

msgOut: dispositivo da usare per messaggi di errore e debugging

entrambi di tipo textArea– stdOut.append( “……” );

Page 7: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Operazioni di I/O in javaCaller

askInt(), askDouble(),askBoolean(), askString()

AskInt( String s), AskDouble(String s), AskBoolean(String s), AskString(String s)

write( String), writeln( String)

Page 8: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Finestre di dialogo in javaCaller

readArrayNum( String s, double m[], int n)

writeArrayNum( String s, double m[], int first, int last)

writeMatNum( String s, double m[][], int fr, int lr, int fc, int lc)

Page 9: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Dispositivo grafico in javaCaller Un’area predefinita

0,00,300

0,220 300,220

Asse x

Asse y

Page 10: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Grafica in javaCaller

Graphics getDraw() – permette di ottenere il dispositivo di disegno

sull’area predefinita

public boolean grafica(Graphics gg ) – viene automaticamente invocata dal sistema ogni

volta in cui la finestra riottiene il fuoco

Page 11: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Operazioni della classe Graphics

setColor(Color) – Sets this graphics context's current color to the

specified color.

drawString(String, int, int) – Draws the text given by the specified string, using

this graphics context's current font and color.

Page 12: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Operazioni della classe Graphics

drawLine(x1 int, y1 int, x2 int, y2 int)– Draws a line, using the current color, between the points (x1, y1)

and (x2, y2) in this graphics context's coordinate system. drawOval(x int, y int, w int, h int)

– Draws a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments

drawPolygon(int[], int[], int) – Draws a closed polygon defined by arrays of x and y coordinates

Page 13: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Grafico di funzioni

public boolean drawFun( String fname, double a, double b, boolean scaling) – disegna la funzione fname (Double-> Double)

nell'intervallo [a,b].

– Se scaling=true effettua una messa in scala e un posizionamento degli assi ottimale rispetto all'area di disegno disponibile.

– Se scaling=false mantiene la configurazione degli assi e il fattore di scala corrente, permettendo la sovrapposizione di grafici

Page 14: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Esempi javaCaller

Page 15: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Grafico permanente

public boolean grafica(Graphics gg){

drawArea.setBackground(Color.lightGray);

drawFun("f1b",-5, 5,true);

return true;

}

Page 16: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Scritte e lineepublic boolean hello(){

g = getDraw();Font ff = g.getFont(); int fs = ff.getSize(); drawArea.setBackground(Color.green);

g.setColor( Color.black );g.setFont( new Font ( ff.getName(), fs+20,fs+20) );g.drawString("hello world", 100,100 );g.drawLine(0,100,100,100);g.drawLine(100,100,100,50);g.drawLine(100,50,50,50);g.drawLine(50,50,50,100);

return true;}

Page 17: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Cerchi

public boolean oval(int x, int y, int w, int h){

g.drawOval( x,y,w,h );

return true;

}

Page 18: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Problema

Un operatore inserisce dal terminale di ingresso una sequenza di al piu’ 60 caratteri. Si vuole estrarre dalla sequenza la prima stringa che corrisponde alla rappresentazione esterna di un numero intero N e assegnare il valore N a una data variabile X

Page 19: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Impostazione della soluzione

Lettura dei caratteri della stringa S di ingresso fino al primo carattere (di indice i) che corrisponde a ‘-’.’+’ o una cifra (0,…,9)

Conversione in numero intero della stringa Si,Si+1,..,Sj, in cui Sj+1 e’ l’indice del primo carattere diverso da un digit

Page 20: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Input di caratteri

getch() #include <conio.h>– A single character from the console (stdin) is

returned. The input is not line-buffered. If there is a character pending from ungetch, it is returned instead. The character is not echoed to the screen.

getchar() #include <stdio.h>– The same as `fgetc(stdin)’.

Page 21: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Output di caratteri

int putch(int _c); #include <conio.h>– Put the character _C on the screen at the current cursor

position. The special characters return, linefeed, bell, and backspace are handled properly, as is line wrap and scrolling. The cursor position is updated. The character is returned.

putchar(char c) #include <stdio.h>– This is the same as `fputc(c, stdout)'.

Page 22: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Output di stringhe

#include <stdio.h> int puts(const char *string);

– Writes STRING to `stdout', and then writes a newline character.

– Return nonnegative for success, or `EOF' on error.

Page 23: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Input di stringhe

#include <stdio.h> char *gets(char *buffer);

– Reads characters from `stdin', storing them in BUFFER, until either end of file or a newline is encountered.

– If any characters were stored, the BUFFER is then `NULL' terminated and its address is returned, else `NULL' is returned.

Page 24: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Buffer, stringhe, puts e gets

Eseguire il programma selezionando 4

Teststr.exe sorgente

Page 25: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Input output di caratteri su files

int fgetc(FILE *file); int getc(FILE *file);

– Return the next character in the given FILE as an unsigned char (value 0..255) or `EOF' at end-of-file

int putc(int CH, FILE *file); int fputc(int CH, FILE *file);

– Writes the given CH to the given `file'. Return the char written

Page 26: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

EOF

Stdio.h

#define EOF (-1)

Page 27: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Scans formatted text from `stdin' and stores it in the variables pointed to by the arguments. – The format string contains regular characters which

much match the input exactly as well as a conversion specifiers, which begin with a percent symbol.

Return the number of items successfully matched and assigned. If input ends before first item is assigned, EOF is returned.

int scanf(const char *format, &x1,…,pn);

Page 28: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

int scanf(const char *format, &x1,…,&xn); Any whitespace in the format string matches

zero or more of any whitespace characters in the input. Thus, a single space may match a newline and two tabs in the input.

All conversions except `c' and `[’ also skip leading whitespace automatically.

Page 29: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Campi dei conversion specifiers *

– indicates that the input should be converted according to the conversion spec, but not stored anywhere.

A width specifier, – which specifies the maximum number of input

characters to use in the conversion. An optional conversion qualifier,

– may be `h' to specify `short', `l' to specify long ints, or `L' to specify long doubles. Long long type can be specified by `L' or `ll'.

Page 30: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

conversion specifiers `c'

– Copy the next character (or WIDTH characters) to the given buffer.

`d'– Convert the input to a signed integer.

`e’ `E’ `f’ `g’ `G'– Convert the input to a floating point number.

`i'– Convert the input, determining base automatically by

the presence of `0x' or `0' prefixes.

Page 31: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

conversion specifiers `n’

– Store the number of characters scanned so far into the integer pointed to.

`o'– Convert the input to a signed integer, using base 8.

`p'– Convert the input to a pointer. This is like using the

`x' format. `%'

– This must match a percent character in the input.

Page 32: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

conversion specifiers `s'

– Copy the input to the given string, skipping leading whitespace and copying non-whitespace characters up to the next whitespace. The string stored is then `NULL'-terminated.

`u'– Convert the input to an unsigned integer.

`x' `X’– Convert the input to an unsigned integer, using base

16.

Page 33: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

conversion specifiers `[...]'

– Like the `c' format, except only certain characters are copied. The characters between the brackets determine which characters are allowed, and thus when the copying stops. These characters may be regular characters (example: `[abcd]') or a range of characters (example: `[a-d]'). If the first character is a caret (`^'), then the set specifies the set of characters that do not get copied (i.e. the set is negated). To specify that the set contains a close bracket (`]'), list that as the first regular character.

Page 34: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Una nuova soluzione al problema

int x;

scanf( “%d”, &x );

Page 35: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Una nuova soluzione al problema

Oggi e’ il 15 Novembre 1998

int x;

scanf( “%d”, &x );

non modifica x,

scanf restituisce 0

Page 36: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Una nuova soluzione al problema

15 Novembre 1998

int x;

scanf( “%d”, &x );

a x viene attribuito il valore 15

scanf restituisce 1

Page 37: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

int printf(const char *format, x1,…,xn); Sends formatted output from the arguments (...)

to `stdout'.– The format string contains regular characters to print,

as well as conversion specifiers, which begin with a percent symbol.

Returns the number of characters written.

Page 38: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Printf: Conversion specifiers Si veda l’help o il manuale

Page 39: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Lettura-stampa di stringhe

Stringhe come array di caratteri Eseguire il programma selezionando 1

Per l’uso di una variabile globale selezionare 2

Teststr.exe sorgente

Page 40: AN Fondam98 Input Output Input Output I files standard di I/O.

AN Fondam98 Input Output

Lettura-stampa di stringhe

Stringhe come puntatori a caratteri Eseguire il programma selezionando 3

Teststr.exe sorgente