Professional Documents
Culture Documents
AGUSTN
FACULTAD DE INGENIERA DE PRODUCCIN Y
SERVICIOS
TRABAJO
Ejercicios Capitulo 1 y 2
DOCENTE:
Paz Valderrama, Alfredo
INTEGRANTES:
Luna Calderon, Angel
Valdivia Berrio, Freddy
Yanque Churo, Luis
ABRIL, 2015
Ejercicio 1.
Ejercicio 3.
#include <stdio.h>
main(){
printf( "La expresion da: %d\n", getchar() != EOF );
}
/* La expresion da: 1 */
#include <stdio.h>
main(){
printf( "Valor de EOF: %d\n", EOF );
}
/* Valor de EOF: -1 */
#include <stdio.h>
main(){
int c, n, d;
n = 0;
d = 0;
while( ( c = getchar() ) != EOF ){
if( c != '\t' && n != 0){
printf( "\\t%c", c );
n = 0;
}
else if( c != '/' && d != 0 ){
printf( "\\\\%c", c );
d = 0;
}
else if( c == '\t' ){
n++;
}
else if( c == '/' ){
d++;
}
else{
printf( "%c", c);
}
}
}
#include<stdio.h>
#define IN 1 /* en una palabra*/
#define OUT 0 /*fuera de una palabra*/
main(){
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while( ( c = getchar() ) != EOF ){
if( c == ' ' || c == '\n' || c == '\t') state = OUT;
else if( state == OUT){
state = IN;
++nw;
printf( "\n%c", c );
}
else{
printf( "%c", c );
}
}
}
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXTAM 255
main(){
int i, j, c, estado, longitud, histo[ MAXTAM ];
longitud = 0;
estado = OUT;
#include <stdio.h>
#define LIM 255
main(){
int c, i, j;
int f[ LIM ] = { 0 }; //frecuencia
while( ( c = getchar() ) != EOF)
f[ c ]++;
for ( i = 0; i < LIM; ++i )
if ( f[i] > 0 ) {
if ( i == '\t' )
else if ( i == '\n' )
else if ( i == '\b' )
else
printf( " %c: ", i);
for ( j = 0; j < f[ i ]; ++j )
putchar( '*' );
putchar( '\n' );
}
}
#include <stdio.h>
#define LOWER 0 //lmite inferior de la tabla
#define UPPER 300 // lmite superior
#define STEP 20 //tamao del incremento
float convercion( float );
main(){
float fahr;
printf( "%s\t%s\n", "Fahrenheit", "Celsius" );
for ( fahr = LOWER; fahr <= UPPER; fahr += STEP)
#include <stdio.h>
#define MAXLINE 1000
int getlines( char s[ ], int lim );
void copy( char to[ ], char from[ ] );
int main(){
int len, max;
char line [MAXLINE];
char longest [MAXLINE];
max = 0;
while( ( len = getlines( line, MAXLINE) ) > 0 ){
if( len > max ){
max = len;
copy ( longest, line );
}
}
if( max > 0 )
//para que imprima la longitud correcta le restamos uno por el 0
printf( "%s\n %d caracteres\n", longest, max - 1 );
return 0;
}
int getlines( char s[], int lim ){
int c, i, j;
j= 0;
#include <stdio.h>
#define MAXLINE 1000
#define LOG 10
int getlines( char s[], int lim );
void copy( char to[], char from[] );
int main(){
int len;
char line [MAXLINE];
char longest [MAXLINE];
while( ( len = getlines( line, MAXLINE ) ) > 0 ){
if( len > LOG ){
copy( longest, line );
if ( c == '\n' ){
s[ j ] = c;
++j;
++i;
}
s[ j ] = '\0';
return i;
}
void copy( char to[], char from[] ){
int i;
i = 0;
while( ( to[ i ] = from[ i ] ) != '\0')
++i;
}
#include<stdio.h>
#include <stdio.h>
#define MAXLINE 1000
int getlines( char s[], int lim );
void copy( char to[], char from[],int t );
int main(){
int len;
char line[ MAXLINE ];
char longest[ MAXLINE ];
while( ( len = getlines( line, MAXLINE ) ) > 0 ){
copy( longest, line, len );
printf( "%s", longest );
}
return 0;
}
int getlines( char s[], int lim ){
int c, i, j;
j= 0;
for( i = 0; ( c = getchar() ) != EOF && c != '\n'; i++ )
if( i < lim - 2 ){
s[ j ] = c;
++j;
}
if ( c == '\n' ){
s[ j ] = c;
++j;
++i;
}
s[ j ] = '\0';
return i;
}
void copy( char to[], char from[],int l ){
int ta = l-1;
int i;
i = 0;
while( ( to[ i ] = from[ ta ]) != '\0' ){
++i;
--ta;
}
}
main(){
int c;
int i;
int nu;
while( ( c = getchar() ) != EOF ){
if( c == ' ' ) nu++;
if( c != ' ' ){
if( nu >= N ){
for( i = 0; i < nu / N; i++ ){
printf( "%c", '\t' );
}
for( i = 0; i < nu % N; i++ ){
printf( "%c", ' ' );
}
nu = 0;
}
else if( nu < N ){
for( i = 0; i < nu % N; i++ ){
printf( "%c", ' ' );
}
nu = 0;
printf( "%c", c );
}
else{
nu = 0;
printf( "%c", c );
}
}
}
}
#include <stdio.h>
#define N 8
main(){
int c;
int i;
int nu;
while( ( c = getchar() ) != EOF ){
if( c == '\t' || c == ' ' ){
nu ++;
if( nu >= N ){
c = '\n';
nu = 0;
}
}
putchar( c );
}
}
#include<stdio.h>
#define MAXLINE 1000
int getlines(char s[], int lim );
main(){
int i, len, max;
char line[ MAXLINE ];
int fl = 0;
while( ( len = getlines( line, MAXLINE ) ) > 0 ){//recorre lineas
for( i = 0; i < len - 1; i++ ){//recorre caracteres de la linea
if( line[ i ] == '/' ){
if( line[ i + 1 ] == '/' ){
line[ i ] = '\n';
line[ i + 1 ] = '\0';
}
if( line[ i + 1 ] == '*' ){
fl = 1;
}
}
if( line[ i ] == '*' ){
if( line[ i + 1 ] == '/' ){
line[ i ] = ' ';
line[ i + 1 ] = ' ';
fl = 0;
}
}
if( fl == 1 ){
line[ i ] = ' ';
}
}
printf( "%s", line );
}
return 0;
}
int getlines( char s[], int lim ){
int c, i;
for( i = 0; i < lim - 1 && ( c = getchar() ) != EOF && c != '\n'; i++ )
s[ i ] = c;
if ( c == '\n' ){
s[ i ] = c;
++i;
}
s[ i ] = '\0';
return i;
}
#include<stdio.h>
main(){
int c;
int fp = 0;
int fl = 0;
while( ( c = getchar() ) != EOF ){
if( c == '{' ){
fl++;
}
if( c == '}' ){
fl--;
}
if( c == '(' ){
fp++;
}
if( c == ')' ){
fp--;
}
putchar( c );
}
if( fl != 0 || fp != 0 ){
printf( "%s", "ERROR " );
}
}
n =
int
int
int
}
void imprimir(char nom[]){
int i;
for(i = 0; nom[i] != '\0'; i++){
printf("%c", nom[i]);
}
printf("\n");
}
Ejercicio 7. Escriba una funcin invert(x,p,n) que regresa x con los n bits
que principian en la posicin p invertidos (esto es, 1 cambiado a 0 y
viceversa), dejando los otros sin cambio.
#include<stdio.h>
unsigned invert(unsigned x, int p, int n);
main(){
int n, p;
n = 2; //numero a invertir
p = 2; //posicion
unsigned x; //cadena original
x = 5;
unsigned y; //cadena cambiada
y = getbits(x, p, n);
printf("%x %d %d %x\n", x, p, n, y);
}
unsigned invert(unsigned x, int p, int n){
return x ^ (~(~0 << n) << p);
}