DIPARTIMENTO DI INFORMATICA Università di Torino |
|
ESERCIZI
SVOLTI IN LABORATORIO TRADUZIONE NUMERI IN BASE 10 10. Il programma deve chiedere all'utente la base del
numero da tradurre, il numero di cifre da cui e' composto
e il numero stesso, una cifra alla volta. E' necessario
inoltre validare tutti gli input chiesti all'utente, e
cioe' che la base b sia 2 <= b <= 9, che il numero
di cifre non superi la dimensione massima e che le
singole cifre siano comprese fra 0 e b-1. program valuta; const BASEMAX = 9; NUMMAXCIFRE = 9; type TipoBase = 2..BASEMAX; TipoCifra = 0..9; TipoIndiceC = 1.. NUMMAXCIFRE; TipoC = array [TipoIndiceC] of TipoCifra; var baseX: Integer; numCifre: Integer; i: TipoIndiceC; c: TipoC; numero,p,cifra: Integer; {longinteger} procedure valutaDaBaseX(c: TipoC; k: TipoIndiceC; b: TipoBase; var n: Integer); var i: TipoIndiceC; begin n:=0; p:=1; for i := 1 to k do begin n:= n + p*c[i]; p:=p*b; end; end; begin writeln('Inserisci base'); baseX := 0; while (baseX<2) or (baseX>9) do readln(baseX); writeln('Inserisci lunghezza'); repeat readln(numCifre); until (numCifre<NUMMAXCIFRE) and (numCifre>0); for i:= 1 to NumCifre do begin writeln('Inserisci cifra # ', i); readln(cifra); while (cifra < 0) or (cifra >= baseX) do begin writeln('La cifra deve essere compresa fra 0 e ', baseX-1); readln(cifra); end; c[NumCifre - i + 1]:= cifra; end; valutaDaBaseX(c,numCifre,baseX,numero); writeln('in base 10 il numero e'':'); writeln(numero); readln; end.
TRADUZIONE NUMERI DA UNA BASE ALL'ALTRA Tradurre un numero in una base inferiore o uguale a 9 in un numero in una base sa 2 a 16.
program valuta; const BASEMAX = 9; NUMMAXCIFRE = 9; type TipoBase = 2..BASEMAX; TipoCifra = 0..9; TipoIndiceC = 1.. NUMMAXCIFRE; TipoC = array [TipoIndiceC] of Integer; Rappres = array[1..15] of Integer; Base= 1..16; var baseX: TipoBase; baseY: Base; numCifre: TipoIndiceC; i: TipoIndiceC; c: TipoC; numero,p,n,j: Integer; {longinteger} tradotto: Rappres; procedure converti(m: Integer; b: Base; var cif: Rappres); var i,restante: integer; begin i:= 15; restante:= m; {INVARIANTE: m=restante+sommatoria j=(i-1)..15 cif[j]*b^(15-j)} while (restante>0) and (i>=0) do begin cif[i]:= restante mod b; restante:= restante div b; i:= i -1 ; end; {POSTCONDIZIONE: m=sommatoria j=1..15 cif[j]*b^(15-j)} end; procedure valutaDaBaseX(c: TipoC; k: TipoIndiceC; b: TipoBase; var n: Integer); var i: TipoIndiceC; begin n:=0; p:=1; for i := 1 to k do begin n:= n + p*c[i]; p:=p*b; end; end; begin for j:= 1 to 15 do tradotto[j]:=0; writeln('Base di partenza:'); readln(baseX); writeln('Base di arrivo:'); readln(baseY); writeln('numero di cifre:'); readln(numCifre); for i:= 1 to NumCifre do begin readln(c[NumCifre - i + 1]); end; valutaDaBaseX(c,numCifre,baseX,numero); writeln('In decimale: ', numero); converti(numero,baseY,tradotto); for j:= 1 to 15 do write(tradotto[j],' '); readln; end.
MEDIE CON UNA MATRICE Consideriamo una tabella le cui righe rappresentano i
voti degli studenti e le colonne gli esami sostenuti da
loro. Variante:
program medieStudenti; const MAXSTUD = 2; const MAXEX = 3; type TabType = array [1..MAXSTUD] of array [1..MAXEX] of integer; MedType = array [1..MAXSTUD] of real; var tabella: TabType ; medie: MedType; i,j: integer; procedure mediaMatrice(tabella: TabType; riga: Integer; medie: MedType); var h: integer; somma, contatore: integer; begin somma := 0; contatore := 0; writeln; for h:= 1 to 3 do begin write(tabella[riga,h],' '); if tabella[riga,h]>0 then begin somma:= somma + tabella[riga,h]; contatore := contatore + 1; end; end; {Devo evitare divisione per 0} if contatore > 0 then medie[riga] := somma / contatore else medie[riga] := 0; end; begin for j:= 1 to MAXSTUD do for i:= 1 to MAXEX do begin writeln('Inserisci l''esame #', i, ' dello studente #', j); readln(tabella[j,i]); end; for j:= 1 to MAXSTUD do begin writeln; writeln('Esami dello studente # ', j); for i:= 1 to MAXEX do begin write(tabella[j,i], ' '); end; end; writeln('Calcolo media'); for i:= 1 to MAXSTUD do mediaMatrice(tabella,i,medie); writeln; for i:= 1 to MAXSTUD do writeln('Media studente #', i, ':', medie[i]); readln; end.
MEDIE CON UNA MATRICE con cancellazione voto piu' alto e piu' basso (21-11-00) Modificare l'esercizio precedente in modo da non tener
conto del voto piu' alto e piu' basso di ogni studente.
Il programma deve essere strutturato nel modo seguente:
program medieStudenti; const MAXSTUD = 2; const MAXEX = 3; type VetType = array [1..MAXEX] of integer; TabType = array [1..MAXSTUD] of VetType; MedType = array [1..MAXSTUD] of real; var tabella: TabType ; medie: MedType; i,j: integer; {ATTENZIONE: dovrei escludere gli zeri} function minindice(riga: VetType): integer; var indice, min, h: Integer; begin min:=30; for h:= 1 to MAXEX do if riga[h] < min then begin min := riga[h]; indice := h; end; minindice:= indice; end; function maxindice(riga: VetType): integer; var indice, max, h: Integer; begin max:= riga[1]; indice:=1; for h:= 2 to MAXEX do if riga[h] > max then begin max := riga[h]; indice := h; end; maxindice:= indice; end; procedure mediaVett( riga: VetType; var media: Real); var h, min, max, somma, contatore: Integer; begin somma := 0; contatore := 0; writeln; min:= minindice(riga); max:= maxindice(riga); riga[min]:= 0; riga[max]:= 0; {ERRORE: min:= minindice(riga); riga[min]:= 0; max:= maxindice(riga); riga[max]:= 0; } for h:= 1 to 3 do begin write(riga[h],' '); if riga[h]>0 then begin somma:= somma + riga[h]; contatore := contatore + 1; end; end; {Devo evitare divisione per 0} if contatore > 0 then media := somma / contatore else media := 0; end; procedure stampa; var j,i: Integer; begin for j:= 1 to MAXSTUD do begin writeln; writeln('Esami dello studente # ', j); for i:= 1 to MAXEX do begin {ERRORE: COPIA PER CUT E PASTE: tabella e' globale} write(tabella[j,i], ' '); end; end; end; begin for j:= 1 to MAXSTUD do for i:= 1 to MAXEX do begin writeln('Inserisci l''esame #', i, ' dello studente #', j); readln(tabella[j,i]); end; stampa; writeln; writeln('Calcolo media'); for i:= 1 to MAXSTUD do mediaVett(tabella[i],medie[i]); writeln; for i:= 1 to MAXSTUD do writeln('Media studente #', i, ':', medie[i]); stampa; readln; end.
MEDIE CON UNA MATRICE con lettura e scrittura da file Modificare l'esercizio precedente in modo da salvare i dati su file e leggerli successivamente. Le diverse funzioni (inserimento dati, stampa matrice, calcolo media, lettura e scrittura da file) devono essere richiamate attraverso un menu' a caratteri.
program medieStudenti; const MAXSTUD = 2; const MAXEX = 3; type VetType = array [1..MAXEX] of integer; TabType = array [1..MAXSTUD] of VetType; MedType = array [1..MAXSTUD] of real; StudType = array[1..MAXSTUD] of string; ExType = array [1..MAXEX] of string; var tabella: TabType ; medie: MedType; studenti: StudType; esami: ExType; i,j: integer; ch: char; dati: file of integer; procedure leggiDati; var i: Integer; begin i:= 1; assign(dati, 'dati.txt');
reset(dati); while (not eof(dati)) and (i<=MAXSTUD) do begin for j:= 1 to MAXEX do read(dati, tabella[i,j] ); i:= i + 1; end; close(dati); end; procedure scriviDati; begin assign(dati, 'dati.txt'); reset(dati); for j:= 1 to MAXSTUD do begin for i:= 1 to MAXEX do begin write(dati,tabella[j,i]); end; end; close(dati); end; procedure nomiEsami; begin for i:= 1 to MAXEX do begin writeln('Inserisci il nome dell''esame #',i); readln(esami[i]); end; end; procedure stampaDati; begin write(' '); for i:= 1 to MAXEX do begin write(esami[i], ' '); end; for j:= 1 to MAXSTUD do begin writeln; write(studenti[j]:5,' '); for i:= 1 to MAXEX do begin write(tabella[j,i]:3, ' '); end; end; end; procedure inserisciDati; begin for j:= 1 to MAXSTUD do begin writeln('Inserisci nome dello studente #', j); readln(studenti[j]); for i:= 1 to MAXEX do begin writeln('Inserisci l''esame ', esami[i], ' dello studente ', studenti[j]); readln(tabella[j,i]); end; end; end; procedure mediaVett(tabella: VetType; var media: real); var h: integer; somma, contatore: integer; begin somma := 0; contatore := 0; for h:= 1 to 3 do begin if tabella[h]>0 then begin somma:= somma + tabella[h]; contatore := contatore + 1; end; end; {Devo evitare divisione per 0} if contatore > 0 then media := somma / contatore else media := 0; end; begin writeln; ch:=' '; while (ch<>'e') do begin writeln; writeln('Scegli una opzione: '); writeln(' e: termina'); writeln(' n: inserisci nomi esami'); writeln(' i: inserisci dati'); writeln(' l: leggi i dati su file'); writeln(' s: salva i dati su file'); writeln(' p: stampa la tabella'); writeln(' m: stampa le medie'); readln(ch); case ch of 'e': writeln('Bye'); 'i': inserisciDati; 'p': stampaDati; 'n': nomiEsami; 'l': leggiDati; 's': scriviDati; else begin writeln; writeln('Calcolo media'); for i:= 1 to MAXSTUD do mediaVett(tabella[i],medie[i]); writeln; for i:= 1 to MAXSTUD do writeln('Media studente ', studenti[i], ':', medie[i]:5); end; end; end; readln; end. MEDIE CON UNA MATRICE con lettura e scrittura da file (versione con record)
program medieStudenti; uses Crt; const MAXSTUD = 2; const MAXEX = 3; type VetType = array [1..MAXEX] of integer; VotiRec = record nome: string; voti: VetType; end; TabType = array [1..MAXSTUD] of VotiRec; MedType = array [1..MAXSTUD] of real; ExType = array [1..MAXEX] of string; var tabella: TabType ; medie: MedType; esami: ExType; i,j: integer; ch: char; dati: file of votiRec; procedure leggiDati; var i: Integer; begin i:= 1; assign(dati, 'dati.txt'); reset(dati); while (not eof(dati)) and (i<=MAXSTUD) do begin read(dati, tabella[i] ); i:= i + 1; end; close(dati); end; procedure scriviDati; begin assign(dati, 'dati.txt'); reset(dati); for j:= 1 to MAXSTUD do write(dati,tabella[j]); close(dati); end; procedure nomiEsami; begin for i:= 1 to MAXEX do begin writeln('Inserisci il nome dell''esame #',i); readln(esami[i]); end; end; procedure stampaDati; begin write(' '); for i:= 1 to MAXEX do begin write(esami[i], ' '); end; for j:= 1 to MAXSTUD do begin writeln; write(tabella[j].nome:5,' '); for i:= 1 to MAXEX do begin write(tabella[j].voti[i]:3, ' '); end; end; end; procedure inserisciDati; begin for j:= 1 to MAXSTUD do begin writeln('Inserisci nome dello studente #', j); readln(tabella[j].nome); for i:= 1 to MAXEX do begin writeln('Inserisci l''esame ', esami[i], ' dello studente ', tabella[j].nome); readln(tabella[j].voti[i]); end; end; end; procedure mediaVett(tabella: VetType; var media: real); var h: integer; somma, contatore: integer; begin somma := 0; contatore := 0; for h:= 1 to 3 do begin if tabella[h]>0 then begin somma:= somma + tabella[h]; contatore := contatore + 1; end; end; {Devo evitare divisione per 0} if contatore > 0 then media := somma / contatore else media := 0; end; begin writeln; ch:=' '; while (ch<>'e') do begin writeln; writeln('Scegli una opzione: '); writeln(' e: termina'); writeln(' n: inserisci nomi esami'); writeln(' i: inserisci dati'); writeln(' l: leggi i dati su file'); writeln(' s: salva i dati su file'); writeln(' p: stampa la tabella'); writeln(' m: stampa le medie'); ch:= readkey; case ch of 'e': writeln('Bye'); 'i': inserisciDati; 'p': stampaDati; 'n': nomiEsami; 'l': leggiDati; 's': scriviDati; else begin writeln; writeln('Calcolo media'); for i:= 1 to MAXSTUD do mediaVett(tabella[i].voti,medie[i]); writeln; for i:= 1 to MAXSTUD do writeln('Media studente ', tabella[i].nome, ':', medie[i]:5); end; end; end; readln; end. OCCORRENZE - 1 Leggere 12 numeri compresi fra 0 e 9 da tastiera e
memorizzarli in un array. OCCORRENZE - 2 Leggere 12 numeri compresi fra 0 e 9 da tastiera e
memorizzarli in un array.
Program Occorrenze; const Lunghezza = 3; Primo = 0; Ultimo = 9; type IndiceSequenze = 1..Lunghezza; Sequenze = array[IndiceSequenze] of integer; IndiceOccorrenze = Primo..Ultimo; TipoOccorrenze = array[IndiceOccorrenze] of integer; var sequenza : Sequenze; i : IndiceSequenze; occorrenze : TipoOccorrenze; j : IndiceOccorrenze; begin {Leggere da tastiera x numeri decimali di una sola cifra e memorizzarli in un array.} writeln('introdurre ', Lunghezza, ' numeri decimali di una cifra, uno per riga:'); for i := 1 to Lunghezza do {Input con controllo di consistenza} repeat readln(sequenza[i]); until (sequenza[i] <= Ultimo) and (sequenza[i] >= Primo); writeln('Premi enter per procedere.'); readln; {Calcolare le occorrenze dei numeri 0..9} for j := Primo to Ultimo do occorrenze[j] := 0; for i := 1 to Lunghezza do begin j := sequenza[i]; occorrenze[j] := occorrenze[j] + 1; {In ciascuna componente occorrenze[j], da 1 a Lunghezza ho messo il numero di occorrenze del numero j} end; {Stampare le occorrenze calcolate} writeln('La sequenza di input e'':'); for i := 1 to Lunghezza do write(sequenza[i], ' '); writeln; writeln('Le occorrenze dei numeri in input sono:'); for j := Primo to Ultimo do writeln(j, ' compare ', occorrenze[j], ' volte;'); writeln; writeln('Premi enter per terminare.'); readln; end.
OCCORRENZE - 3 Leggere 12 numeri compresi fra 0 e 9 da tastiera e
memorizzarli in un array. 0 xxxx ecc.
Program Occorrenze; const Lunghezza = 5; Primo = 0; Ultimo = 9; type IndiceSequenze = 1..Lunghezza; Sequenze = array[IndiceSequenze] of integer; IndiceOccorrenze = Primo..Ultimo; TipoOccorrenze = array[IndiceOccorrenze] of integer; var sequenza : Sequenze; i : IndiceSequenze; occorrenze : TipoOccorrenze; j : IndiceOccorrenze; begin {Leggere da tastiera x numeri decimali di una sola cifra e memorizzarli in un array.} writeln('introdurre ', Lunghezza, ' numeri decimali di una cifra, uno per riga:'); for i := 1 to Lunghezza do {Input con controllo di consistenza} repeat readln(sequenza[i]); until (sequenza[i] < 9) and (sequenza[i] > 0); writeln('Premi enter per procedere.'); readln; {Calcolare le occorrenze dei numeri 0..9} for j := Primo to Ultimo do occorrenze[j] := 0; for i := 1 to Lunghezza do begin j := sequenza[i]; occorrenze[j] := occorrenze[j] + 1; {In ciascuna componente occorrenze[j], da 1 a Lunghezza ho messo il numero di occorrenze del numero j} end; {Stampare le occorrenze calcolate} writeln('La sequenza di input e'':'); for i := 1 to Lunghezza do write(sequenza[i], ' '); writeln('Le occorrenze dei numeri in input sono:'); for i := 1 to Lunghezza do begin for j := Primo to Ultimo do begin if occorrenze[j] >= i then write('*') else write('_'); end; writeln; end; for j := Primo to Ultimo do write(j); writeln; writeln('Premi enter per terminare.'); readln; end.
OCCORRENZE - 4 (opzionale) Leggere 12 numeri compresi fra 0 e 9 da tastiera e
memorizzarli in un array. x
Program Occorrenze; const Lunghezza = 10; Primo = 0; Ultimo = 9; type IndiceSequenze = 1..Lunghezza; Sequenze = array[IndiceSequenze] of integer; IndiceOccorrenze = Primo..Ultimo; TipoOccorrenze = array[IndiceOccorrenze] of integer; var sequenza : Sequenze; i : IndiceSequenze; occorrenze : TipoOccorrenze; j : IndiceOccorrenze; begin {Leggere da tastiera x numeri decimali di una sola cifra e memorizzarli in un array.} writeln('introdurre ', Lunghezza, ' numeri decimali di una cifra, uno per riga:'); for i := 1 to Lunghezza do {Input con controllo di consistenza} repeat readln(sequenza[i]); until (sequenza[i] <= Ultimo) and (sequenza[i] >= Primo); writeln('Premi enter per procedere.'); readln; {Calcolare le occorrenze dei numeri 0..9} for j := Primo to Ultimo do occorrenze[j] := 0; for i := 1 to Lunghezza do begin j := sequenza[i]; occorrenze[j] := occorrenze[j] + 1; {In ciascuna componente occorrenze[j], da 1 a Lunghezza ho messo il numero di occorrenze del numero j} end; {Stampare le occorrenze calcolate} writeln('La sequenza di input e'':'); for i := 1 to Lunghezza do write(sequenza[i], ' '); writeln('Le occorrenze dei numeri in input sono:'); for i := 1 to Lunghezza do begin for j := Primo to Ultimo do begin if occorrenze[j] >= Lunghezza - i + 1 then write('*') else write('_'); end; writeln; end; for j := Primo to Ultimo do write(j); writeln; writeln('Premi enter per terminare.'); readln; end.
USO DI FOR E WHILE ESERCIZIO N.1 ESERCIZIO N.2 ESERCIZIO N.3
Gestione di un conto corrente bancario. Si sviluppi un programma iterativo che permette ad un
utente di |
Last update: Sep 27, 2000 | |