Upload
giovanna-giusti
View
273
Download
0
Embed Size (px)
Citation preview
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)
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
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
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)
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( “……” );
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)
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)
AN Fondam98 Input Output
Dispositivo grafico in javaCaller Un’area predefinita
0,00,300
0,220 300,220
Asse x
Asse y
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
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.
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
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
AN Fondam98 Input Output
Esempi javaCaller
AN Fondam98 Input Output
Grafico permanente
public boolean grafica(Graphics gg){
drawArea.setBackground(Color.lightGray);
drawFun("f1b",-5, 5,true);
return true;
}
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;}
AN Fondam98 Input Output
Cerchi
public boolean oval(int x, int y, int w, int h){
g.drawOval( x,y,w,h );
return true;
}
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
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
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)’.
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)'.
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.
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.
AN Fondam98 Input Output
Buffer, stringhe, puts e gets
Eseguire il programma selezionando 4
Teststr.exe sorgente
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
AN Fondam98 Input Output
EOF
Stdio.h
#define EOF (-1)
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);
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.
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'.
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.
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.
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.
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.
AN Fondam98 Input Output
Una nuova soluzione al problema
int x;
scanf( “%d”, &x );
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
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
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.
AN Fondam98 Input Output
Printf: Conversion specifiers Si veda l’help o il manuale
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
AN Fondam98 Input Output
Lettura-stampa di stringhe
Stringhe come puntatori a caratteri Eseguire il programma selezionando 3
Teststr.exe sorgente