Chachi ^^. Un paso curioso sería hacerlas animadas (borrando y reescribiendola con otra posición cada x tiempo).
Yo de florituras en la pantalla, ni se me dan bien ni me gustan mucho. Por no dejar el tema a medias he hecho algo más de mi gusto: Una class Triangulo_Pascal:
De la cual, podemos hacer algunas pruebas:
El mayor fallo que tiene es la función para pasarlo a string, que aunque siempre genera el triangulo, lo hace un poco "raro" para algunos valores de N.
Yo de florituras en la pantalla, ni se me dan bien ni me gustan mucho. Por no dejar el tema a medias he hecho algo más de mi gusto: Una class Triangulo_Pascal:
Código (cpp) [Seleccionar]
/** Resumen de los metodos públicos:
- Constructor(int n = 0): Genera un triangulo de pascal de tamanyo N
- Constructor(triangulo_pascal): Constructor copia.
- getPosicion(int x,int y): Devuelve el valor en la posicion en la posicion x,y. Lanza una excepcion si es una posición incorrecta
- getSize(): Devuelve el tamanyo actual del triangulo
- resize(int n): Reconstruye el triangulo para un ancho n
- clear(): Borra el triangulo dejandolo con un tamanyo 0.
- toString(): Obtiene una expresion escrita del triangulo.
Operadores:
triangulo_pascal = triangulo_pascal /** asignacion *
triangulo_pascal == triangulo_pascal /** iguales? *
triangulo_pascal != triangulo_pascal /** diferentes? *
**/
class triangulo_pascal
{
int** Matriz;
int TAM;
string texto;
int contar_cifras() const
{
int cifras = 1;
int aux = Matriz[TAM-1][TAM/2];
while (aux > 9)
{
cifras++;
aux /= 10;
}
return cifras;
}
void generar(const int n)
{
TAM = n;
Matriz = new int*[n];
Matriz[0] = new int[1];
Matriz[0][0] = 1;
for (int i = 1; i < n;i++)
{
Matriz[i] = new int[i+1];
Matriz[i][0] = 1;
for (int j = 1; j < i;j++)
{
Matriz[i][j] = Matriz[i-1][j-1]+Matriz[i-1][j];
}
Matriz[i][i] = 1;
}
generarString();
}
void generarString()
{
stringstream ss;
const int size = contar_cifras();
for (int i = 0; i < TAM;i++)
{
for (int k = 0; k <= (TAM-i-2);k++)
ss<<" ";
ss<<Matriz[i][0];
for (int j = 1; j <= i;j++)
{
ss<<" ";
ss<<setw(size)<<Matriz[i][j];
}
ss<<endl;
}
texto = ss.str();
}
public:
triangulo_pascal(int n = 0)
{
if (n != 0)
{
generar(n);
}
else Matriz = NULL;
}
triangulo_pascal(const triangulo_pascal &a)
{
if (a.getSize() != 0)
generar(a.getSize());
else Matriz = NULL;
}
triangulo_pascal& operator=(const triangulo_pascal &a)
{
if (a.getSize() != 0)
generar(a.getSize());
else Matriz = NULL;
}
bool operator==(const triangulo_pascal &b)
{
return TAM == b.TAM;
}
bool operator!=(const triangulo_pascal &c)
{
return TAM != c.TAM;
}
int getPosicion(int x,int y) const
{
if (y < TAM || (x > y)) throw "Error, fuera de rango";
return Matriz[x][y];
}
int getSize() const { return TAM;}
void resize(int n)
{
if (n < 0) throw "Error, tamanyo negativo";
clear();
if (n == 0){return;}
generar(n);
}
void clear()
{
if (Matriz == NULL) return;
for (int i = 0; i < TAM;i++)
delete[] Matriz[i];
delete[] Matriz;
Matriz = NULL;
TAM = 0;
}
string toString() const
{
return texto;
}
operator string()
{
return toString();
}
~triangulo_pascal()
{
clear();
}
};
De la cual, podemos hacer algunas pruebas:
Código (cpp) [Seleccionar]
int main()
{
triangulo_pascal mi_triangulo(5);
cout<<mi_triangulo.toString()<<endl;
mi_triangulo.resize(4);
cout<<mi_triangulo.toString()<<endl;
triangulo_pascal segundo(1);
if (mi_triangulo != segundo)
{
cout<<"Son diferentes :("<<endl<<endl;
}
cout<<(string)segundo<<endl; // otra forma de pasarlo a string es usando los casts
return 0;
}
El mayor fallo que tiene es la función para pasarlo a string, que aunque siempre genera el triangulo, lo hace un poco "raro" para algunos valores de N.