sintassi gestione stringhe e numeri
program cambio1; (* uso di Str,+,Concat,Length,Val,Ord *) (* trasforma numero in stringa con Str *) (* trasforma stringa in numero con Val *) (* calcola lunghezza stringa con Length o con Ord *) (* somma numeri o stringhe con + *) (* somma stringhe con Concat *) uses crt; var a1,a2,a12,st1,st2,st12,h: string; n1,n2,n12,p12,vs1,vs2,codice,lunga1,lunga2,lunga12:INTEGER; begin clrscr; h:='-------------------------------------'; (* numeri da trasformare in stringhe *) n1:=10; n2:=20; str(n1,a1); str(n2,a2); writeln('stampa numeri e stringhe'); writeln('stringa =',a1); writeln('stringa =',a2); writeln('cifra =',n1); writeln('cifra =',n2); writeln(h); writeln('somma stringhe con + o concat'); a12:=a1+a2; st12:=concat(a1,a2); writeln(st12); writeln(h); writeln('stampa lunghezza stringhe '); lunga1:=ord(a1[0]); lunga2:=ord(a2[0]); lunga12:=ord(a12[0]); writeln(length(a1)); writeln(length(a2)); writeln(length(a12)); writeln(h); writeln(lunga1); writeln(lunga2); writeln(lunga12); writeln(h); writeln('somma e moltiplica cifre'); n12:=n1+n2; p12:=n1*n2; writeln('somma numeri =',n12); writeln('prodotto numeri =',p12); writeln('somma stringhe =',a12); (* numeri stringa da cambiare in numeri *) writeln('stringhe numeriche '); st1:='10'; st2:='20'; writeln(st1); writeln(st2); writeln('stringhe trasformate in cifre'); val(st1,vs1,codice); val(st2,vs2,codice); (* stampa numeri come stringa e come cifre *) writeln('stringa = ',st1); writeln('stringa = ',st2); writeln('cifra = ',vs1); writeln('cifra = ',vs2); (* stampa somma stringhe numeriche e cifre *) writeln('somma stringhe=',st1+st2); writeln('somma cifre =',vs1+vs2); writeln('prodotto cifre=',vs1*vs2); readln; end.
stampa numeri e stringhe stringa =10 stringa =20 cifra =10 cifra =20 ------------------------------------- somma stringhe con + o concat 1020 -------------------------------------
stampa lunghezza stringhe 2 2 4 ------------------------------------- 2 2 4 ------------------------------------- somma e moltiplica cifre somma numeri =30 prodotto numeri =200 somma stringhe =1020 stringhe numeriche 10 20 stringhe trasformate in cifre stringa = 10 stringa = 20 cifra = 10 cifra = 20 somma stringhe=1020 somma cifre =30 prodotto cifre=200
cambio2