Last active
February 4, 2016 21:01
-
-
Save vfrico/f14e837cc00b068a4028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Based on a tweet from @joselucross (https://twitter.com/joselucross/status/694487120182837248) | |
Generates a triangle on ASCII | |
*/ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
void triangulo (int n) | |
{ | |
int m = 2 * n - 1; //longitud de un lado | |
int l = (m+1)/2; // Posicion central de cualquier línea | |
int i = 1; //Linea actual | |
int j = 1; //Columna actual | |
while(i < n) { | |
while(j < (m+1)){ | |
if(j == (l+i-1)){ | |
printf("X"); | |
} | |
else if( j == (l-i+1)){ | |
printf("X"); | |
} | |
else { | |
printf(" "); | |
} | |
j = j+1; | |
} | |
j = 1; // Resetea la columna | |
i = i + 1; // Salto de línea | |
printf("\n"); | |
} | |
while(j < (m + 1)){ | |
printf("X"); | |
j = j + 1; | |
} | |
printf("\n"); | |
} | |
void muestra_ayuda() { | |
printf("El programa admite las siguientes opciones:\n -o\t Fichero de salida\n -a\t Altura del triángulo\n"); | |
} | |
int main(int argc, char * argv[]){ | |
int n = 0; //Altura del triángulo | |
int out; //Fichero de salida | |
int i; | |
for( i = 0; i < argc; i++) { | |
//printf("%s\n",argv[i]); | |
if(strcmp(argv[i],"-o") == 0) { | |
out = open(argv[i+1],O_RDWR); | |
int status = dup2(out,1); | |
if(status == -1) { | |
printf("%d: No se pudo abrir el fichero \n", status); | |
return 1; | |
} | |
i++; | |
} | |
if(strcmp(argv[i], "-a") == 0) { | |
n = atoi(argv[i+1]); | |
i++; | |
} | |
if(strcmp(argv[i], "-h") == 0) { | |
muestra_ayuda(); | |
return 0; | |
} | |
} | |
if (n == 0) { | |
printf("Introduce un numero que sera la altura del triangulo: "); | |
scanf("%i", &n); | |
} | |
triangulo(n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment