miércoles, 18 de septiembre de 2013

Ejemplos de C/C++ sencillos: ASCII Art


Hola a todos,

ASCII Art es el hecho de hacer dibujos y formas mediante caracteres puestos en la consola.
El ejercicio de hoy es: pintar por consola un circulo(relleno), una X, un marco  y un rectangulo (relleno)

Es fácil y os servirá para ir practicando los bucles y los condicionales.

Os dejo el código y el como os debería quedar.

#include <iostream>
#include <stdlib.h>
#include <math.h>

void linea_ASCII(char car,int ancho)
{
int i;

for(i = 0 ; i < ancho ; ++i)
{
std::cout << car;
}

std::cout << std::endl;
}

void marco_ASCII(char car,int ancho, int alto)
{
int i,j;

for(i = 0 ; i < alto; ++i )
{
for(j = 0 ; j < ancho  ; ++j )
{
if( (i == 0) || (i==(alto-1)) )
{
std::cout << car;
}
else
{
if( (j == 0) || (j == (ancho-1)) )
std::cout << car;
else
std::cout << " ";
}
}
std::cout << std::endl;
}
}

void rectangulo_ASCII(char car,int ancho, int alto)
{
int i,j;

for(i = 0 ; i < alto; ++i )
{
for(j = 0 ; j < ancho  ; ++j )
std::cout << car;

std::cout << std::endl;
}
}

void X_ASCII(char car,int ancho)
{
int i,j;

for(i = 0 ; i < ancho; ++i )
{
for(j = 0 ; j < ancho  ; ++j )
{
if( ( i == j) || (ancho-1-i == j) )
{
std::cout << car;
}
else
{
std::cout << " ";
}
}
std::cout << std::endl;
}
}

void circulo_ASCII(char car,int ancho)
{
int i,j;
int centro = ancho/2;
for(i = 0 ; i < ancho+1; ++i )
{
for(j = 0 ; j < ancho+1  ; ++j )
{
if( sqrt(( i - centro)*( i - centro) +  ( j - centro)*( j - centro)) <= centro )
{
std::cout << car;
}
else
{
std::cout << " ";
}
}
std::cout << std::endl;
}
}

int main(int argc, char** argv)
{
circulo_ASCII('O',10);
linea_ASCII('-',40);
std::cout << std::endl;

X_ASCII('x',10);
linea_ASCII('-',40);
std::cout << std::endl;

marco_ASCII('e',10,4);
linea_ASCII('-',40);

rectangulo_ASCII('y',10,4);
linea_ASCII('-',40);
system("pause");
return 0;
}

Aquí tenéis la imagen por si queréis que os sirva de guía
:
Nos vemos,

LordPakusBlog

0 comentarios :

Publicar un comentario

Entradas populares