Unidad II - 3 - Paradigma Imperativo - Pascal y C
Unidad II - 3 - Paradigma Imperativo - Pascal y C
Unidad II - 3 - Paradigma Imperativo - Pascal y C
/**********************************************/
/* OPERADOR.C */
/**********************************************/
/* Declaraciones globales */
int i=0,j=0,menor,mayor; /* inicializacion de variables i y j */
int main(void)
{ /* Declaraciones locales (para la funcion main) */
char c=5,d=0; /* Inicializacion de las variables c y d */
const unsigned int k=3; /* Declaracion de la constante k */
uses crt;
var
i:integer=0;
j:integer=0;
menor,mayor:integer;
c:shortint=5;
d:shortint=0;
const
k:word=3;
begin
clrscr;
i:=i+1; writeln(i,' ',j);
i:=i+1; writeln(i,' ',j);
i:=i+1; writeln(i,' ',j);
j:=i;i:=i+1; writeln(i,' ',j);
i:=i+1;j:=i; writeln(i,' ',j);
if i>j then
d:=c
else
d:=i; writeln(d);
readkey;
end.
Se obviaron las últimas líneas de sentencia, debido a que son semejantes a las anteriores.
Ej.:
#include <stdio.h>
char c=2;
int i=5;
float f= 2;
double d=1.5;
int main(void) {
d= c * i + i / c + f * d; /*(int=10 + int=2 +
double=3)=>(double=15)*/
printf("%5.2lf",d);
return 0;
}
Bajo el contexto del ejemplo anterior, el operador de la división "/", realiza una división
entera, debido a que tanto c como i son enteros (de 8 y 16 bits respectivamente). La forma de
obtener un resultado real de esta división a partir de enteros es forzar el cambio del tipo
(promover el tipo) de uno de los operandos. Para poder hacer esto, C se vale de los moldes o
promoción (o cast), estos se aplican precediendo a la variable que queremos convertir, con el
nombre del tipo entre paréntesis, como se muestra en el siguiente ej.
char c=2;
int i=5;
printf("%4.2f", (float) i /c);
La salida será: 2.50
Ej. :
i=4;
j=5;
printf("Si multiplico %d x %d obtengo: %d", i, j,
i*j);
La salida será:
Si multiplico 4 x 5 obtengo: 20
scanf: Es la función complementaria del printf. Esta permite recibir una cadena formateada
desde stdin (Dispositivo de entrada estándar). Como se verá es muy parecida a printf.
La salida será:
Especificadores de Formato
Los especificadores de formato son los símbolos incluidos en Cadena_con_formato, que
permiten formatear la salida, no solo en las funciones printf y scanf, sino también en
funciones análogas, como:
DATOS
127 3.7 "abc" a'
El especificador de formato ancho indica los lugares que a lo sumo ocupará la salida,
mientras que la precisión indica la cantidad de dígitos decimales, que se mostrarán.
La utilización del asterisco para el ancho y/o la precisión, indica que el siguiente argumento
de la función es el ancho y/o la precisión. Por ejemplo:
pi= 3.14159265359;
printf("%*.*f",5,2,pi);
mostrará:
3 . 1 4
6
Los códigos \f y \v no tienen ningún efecto en la salida por pantalla.
La salida:
Hola Mundo!!!
Format
Sintaxis:
function Format(const Fmt: string;const Args: array of
Const):string;
La función Format reemplaza todos los marcadores de posición en Fmt con los
argumentos pasados en Args y devuelve la cadena resultante. Un marcador de posición
tiene la siguiente sintaxis:
Var P : Pointer;
fmt,S : string;
procedure HolaMundo;
const cte: string= 'Mundo';
var vrbl: string;
begin
vrbl := '!!!';
clrscr;
Writeln('Hola ',cte,vrbl);
write('Pulse enter ');
readln;
end;
Procedure TestInteger;
begin
clrscr;
Fmt:='[%d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
writeln('[%d]':12, ' => ', Format (Fmt,[10]));
Fmt:='[%%]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%.4d]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%10.4d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10.4d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10.4d]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%-*.*d]';S:=Format (fmt,[4,5,10]);writeln(Fmt:12,' => ',s);
write('Pulse enter ');
readln;
end;
Procedure TestHexaDecimal;
begin
clrscr;
Fmt:='[%x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%10.4x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10.4x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10.4x]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s);
Fmt:='[%-*.*x]';S:=Format (fmt,[4,5,10]);writeln(Fmt:12,' => ',s);
write ('Pulse enter ');
readln;
end;
Procedure TestPointer;
begin
clrscr;
P:=Pointer(1234567);
Fmt:='[0x%p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Fmt:='[0x%10p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Fmt:='[0x%10.4p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Fmt:='[0x%0:p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Fmt:='[0x%0:10p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Fmt:='[0x%0:10.4p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Fmt:='[0x%0:-10p]';S:=Format (Fmt,[P]);writeln(Fmt:13,' => ',s);
Procedure TestString;
begin
clrscr;
Fmt:='[%s]';S:=Format(fmt, ['Un string']);Writeln(fmt:12,'=> ',s);
fmt:='[%0:s]';s:=Format(fmt, ['Un string']);Writeln(fmt:12,'=> ',s);
fmt:='[%0:18s]';s:=Format(fmt, ['Un string']);Writeln(fmt:12,'=> ',s);
fmt:='[%0:-18s]';s:=Format(fmt,['Un string']);Writeln(fmt:12,'=> ',s);
fmt:='[%0:18.12s]';s:=Format(fmt,['Un string']);Writeln(fmt:12,'=> ',s);
fmt:='[%-*.*s]';s:=Format(fmt,[18,12,'Unstring']);Writeln(fmt:12,'=>',s);
write ('Pulse enter ');
readln;
end;
Procedure TestExponential;
begin
clrscr;
Fmt:='[%e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10.4e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10.4e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10.4e]';S:=Format (fmt,[1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,1.234]);writeln(Fmt:12,' => ',s);
write ('Pulse enter ');
readln;
end;
Procedure TestNegativeExponential;
begin
clrscr;
Fmt:='[%e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10.4e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10.4e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10.4e]';S:=Format (fmt,[-1.234]);writeln(Fmt:12,' => ',s);
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,-1.234]);writeln(Fmt:12,' => ',s);
write ('Pulse enter ');
readln;
end;
Procedure TestSmallExponential;
begin
clrscr;
Fmt:='[%e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10.4e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10.4e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10e]';S:=Format (Fmt,[0.0123]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10.4e]';S:=Format (fmt,[0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,0.01234]);writeln(Fmt:12,' => ',s);
write ('Pulse enter ');
readln;
Procedure TestSmallNegExponential;
begin
clrscr;
Fmt:='[%e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%10.4e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:10.4e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%0:-10.4e]';S:=Format (fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,-0.01234]);writeln(Fmt:12,' => ',s);
write ('Pulse enter ');
readln;
end;
begin
HolaMundo;
TestInteger;
TestHexadecimal;
TestPointer;
teststring;
TestExponential;
TestNegativeExponential;
TestSmallExponential;
TestSmallNegExponential;
end.