Soluzioni esercizi proposti il 21 e 23/11/2006
Esercizio 1
program Esercizio1;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
n=5;
m=3;
var
frase:string[n];
parola1,parola2:string[m];
y,i:Integer;
function CercaContenuto(strContenuto,strContenitore:string): integer;
begin
cercaContenuto:= pos(strContenuto,strContenitore)
end;
begin
writeln('Inserisci una frase di ',n,' caratteri');
readln(frase);
Writeln('Adesso inserisci una parola di ',m,' caratteri');
readln(parola1);
if cercacontenuto(parola1,frase)<> 0 then
begin
writeln('E''stata trovata un occorrenza della parola a partire dal carattere ',cercacontenuto(parola1,frase));
writeln('inserisci la stringa con la quale vuoi sostituire la prima parola');
writeln('Sempre ',m,' caratteri');
readln(parola2);
y:= cercacontenuto(parola1,frase);
delete(frase,cercacontenuto(parola1,frase),m);
insert(parola2,frase,y);
writeln('La frase modificata e'': ',frase);
end
else
Writeln('La frase cercata non e'' presente');
readln;
end.
La versione modificata dell'esercizio non l'avevo salvata...chi ne ha una versione è pregato di pubblicarla.
Esercizio 2
program Esercizio2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
n=7;
var
strTest: string[n];
function TestPalidroma(str: string):boolean;
var
i:integer;
begin
i:=1;
while (str[i]=str[length(str)+1-i]) and (i<=(length(str)div 2)) do
i:= i+1;
if (i>(length(str)div 2)) then
TestPalidroma:=true
else
TestPalidroma:=false;
end;
begin
Writeln('Inserisci una stringa di ',n,' caratteri');
readln(strTest);
writeln('Verifica Palindroma: ',TestPalidroma(strTest));
readln
end.
Esercizio 3
program Esercizio3;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
n=2;
type
Vettore=array[1..n] of integer;
var
V,M,R:vettore;
j,K: integer;
procedure FILL(var X:vettore);
var i: integer;
begin
randomize;
for i:=1 to length(X) do
X[i]:=((random(10))+1);
end;
function MCD(x,y: integer):integer;
var
k,r:integer;
begin
if x<y then
begin
K:=y;
y:=x;
x:=k;
end;
repeat
r:=x mod y;
x:= y;
y:= r;
until r=0;
MCD:=x;
end;
begin
fill(v);
fill(M);
r[1]:= v[1]*m[1];
r[2]:=v[2]*m[2];
for j:=1 to n do
begin
writeln(v[j]:2,' ',M[j]:2,' ',r[j]);
if j=1 then writeln('-- * -- = --');
end;
K:=mcd(r[1],r[2]);
writeln;
writeln('Il MCD e'': ',k);
r[1]:=(r[1]div K);
r[2]:=(r[2]div k);
writeln;
writeln('La frazione ridotta ai minimi termini e'': ');
writeln(r[1]);
writeln('--');
writeln(r[2]);
readln
end.
Esercizio 4
program Esercizio4;
{$APPTYPE CONSOLE}
const
n=10;
var
v:Array[1..n] of integer;
i,inversione:integer;
begin
for i:=1 to n do
V[i]:=random(100);
for i:=1 to n do
write(V[i],' ');
inversione:=1;
for i:=1 to n-1 do
if (V[i] > V[i+1]) then inversione:=inversione+1;
writeln('Il numero delle inversioni e'': ',inversione);
readln;
end.
Esercizio 5
program Esercizio5;
{$APPTYPE CONSOLE}
uses
SysUtils;
Const
n=5;
type
Matrice= Array[1..n,1..n]of integer;
Var
M:matrice;
Procedure Fill(var M:matrice); {Riempie la matrice}
var
i,j:integer;
begin
randomize;
for i:=1 to n do
for j:=1 to n do
begin
M[i,j]:=(random(2));
{writeln(m[i,j]);}
end;
end;
{************************************}
Procedure Print(M:matrice); {Stampa la matrice}
var
i,j: integer;
begin
for i:=1 to n do
begin
for j:=1 to n do
Write(M[i,j],' ');
Writeln;
End;
end;
{*********************************************}
function TestaMatrice(X:Matrice):boolean;
var
i,j: integer;
trovato: boolean;
begin
i:=1;
trovato:=false;
while (trovato=false) and (i<n) do
begin
j:=1;
while (trovato=false) and (j<n) do
begin
{writeln(x[i,j]);
writeln(x[i,j+1]); }
if (X[i,j]+X[i,(j+1)]=0) or (X[i,j]+X[(i+1),j]=0)then
trovato:= true;
j:=j+1;
end;
i:=i+1;
end;
{verifico perchè è uscito dal ciclo}
writeln('Ho controllato fino al termine di coordinate: ',(i-1),' : ',(j-1));
if trovato= false then
begin
writeln('Non ci sono coppie contigue di zeri.');
TestaMatrice:=false;
end
else
begin
testamatrice:=true;
writeln('Nella matrice ci sono almeno due zeri contigui');
end;
end;
{**********************************************************}
begin
fill(M);
print(M);
writeln;
writeln(testamatrice(m));
Readln
end.
Esercizio 5 - VARIANTE SLBB
Una
interessante variante di questo esercizio pubblicata da SLBB
mi permetto di far notare che la funzione ABS
in questo caso non era necessaria, in quanto il testo dell'esercizio stabiliva che la matrice fosse composta da zero e uno, ma ciò rende utilizzabile la funzione anche in altri esercizi.
{Inizializzazione}
trovato:=1;
i:=5;
j:=1;
{Controlliamo prima l'ultima riga}
while ((trovato<>0) or (j<=4)) do
begin
trovato:=(abs(M[i,j])+abs(M[i,j+1]));
j:=j+1;
end;
{Reinizializziamo gli indici}
i:=1;
j:=1;
{Controlliamo contemporaneamente i due vicini di un elemento M[i,j]}
{cioè quello che sta alla sua destra e quello che sta sotto}
if (trovato<>0) then
while ((trovato<>0) or (i<=4)) do
begin
{la seguente funzione restituisce 0 se due vicini hanno valore nullo}
trovato:=(abs(M[i,j])+abs(M[i,j+1]))*(abs(M[i,j])+abs(M[i+1,j]));
if (j=4) then
begin
j:=1;
i:=i+1;
end
else
j:=j+1;
end;
if (trovato=0) then
writeln('La matrice contiene 2 zeri consecutivi')
else
writeln('La matrice non contiene 2 zeri consecutivi');
readln;
end;
Esercizio 6
Torna a Programmazione e Lab.
Non ci sono commenti in questa pagina. [Scrivi commento]