|
Mostrar Temas
|
Páginas: 1 [2]
|
11
|
Programación / Programación C/C++ / Sistema de reservacion de aerolinea[C]
|
en: 11 Diciembre 2010, 23:51 pm
|
Hola foreros! hacia mucho q no posteaba nada debido a q cursando en la facu no tenia mucho tiempo libre para programar. Como ya termine volvi a codear en c y queria subir un programa de reserva de aerolinea q pide como ejercicio un libro q estoy leyendo. Espero comentarios y sugerencias. Saludos! #include<stdio.h> #include<stdlib.h> #include<time.h> #define SIZE_ASIENTOS 10 int asignarAsiento(int [], int, int); void pasaje(int); void diagrama(int [], int); main() { int asientos[SIZE_ASIENTOS] = {0}; int i, num, cont = 0; printf("SISTEMA DE RESERVACION DE AEROLINEAS\n\n"); diagrama(asientos, SIZE_ASIENTOS); while (cont <= SIZE_ASIENTOS - 1){ printf("Por favor ingrese 1 para sector fumadores\n" "Por favor ingrese 2 para sector no fumadores\n"); while (num > 2){ printf("Numero no valido\n\n"); printf("Por favor ingrese 1 para sector fumadores\n" "Por favor ingrese 2 para sector no fumadores\n"); } if ((asignarAsiento(asientos, SIZE_ASIENTOS, num)) != 0) cont++; diagrama(asientos, SIZE_ASIENTOS); } printf("Avion lleno\nProximo vuelo en 3 horas\n"); return 0; } int asignarAsiento(int a[], int size, int n) { char rta[1]; int i, status = 0; static int cont1 = 0, cont2 = 0; switch(n){ case 1: //SECTOR FUMADORES if (cont1 < size / 2){ while (status == 0){ if(a[i] == 0){ a[i] = 1; pasaje(i); cont1++; status = 1; } } } else{ printf("La sección fumadores esta llena. " "Le gustaria q le asignemos un lugar en la sección " "de no fumadores(s/n)"); if(rta[0] == 's' || rta[0] == 'S'){ while (status == 0){ i = size / 2 + rand() % size / 2; if(a[i] == 0){ a[i] = 1; pasaje(i); cont2++; status = 1; } } } else{ printf("\nEl proximo vuelo sale en 3 horas\n\n"); return 0; } } break; case 2: //SECTOR NO FUMADORES if (cont2 < size / 2){ while (status == 0){ i = size / 2 + rand() % size / 2; if (a[i] == 0){ a [i] = 1; pasaje(i); cont2++; status = 1; } } } else{ printf("La sección de no fumadores esta llena. " "Le gustaria q le asignemos un lugar en la sección " "de fumadores(s/n)"); if(rta[0] == 's' || rta[0] == 'S'){ while (status == 0){ if(a[i] == 0){ a[i] = 1; pasaje(i); cont1++; status = 1; } } } else{ printf("\nEl proximo vuelo sale en 3 horas\n\n"); return 0; } } break; } } void pasaje(int numAsiento) { printf("\n************************\n"); if (numAsiento >= 0 && numAsiento <= 4){ printf("Asiento nº %d\n", numAsiento + 1); } else{ printf("Asiento nº %d\n", numAsiento + 1); printf("Sector no fumadores\n"); } printf("\n************************\n"); } void diagrama(int a[], int size) { int i; printf("%30s\n\n", "DIAGRAMA AVION"); printf("%15s %26s\n", "FUMADORES", "NO FUMADORES"); printf("/-------------------\\"); printf(" /-------------------\\\n"); for (i = 0; i <= size - 1; i++) }
|
|
|
12
|
Programación / Programación C/C++ / [C]Terna pitagorica(Fuerza bruta)
|
en: 12 Agosto 2010, 17:12 pm
|
Hola, dejo un programa q saca ternas pitagoricas(a²+b²=c²) mediante fuerza bruta.. El problema q tengo es q no puedo hacer q descarte las ternas q son iguales(ej: 3²+4² = 5² -- 4²+3² = 5²). En el codigo hay varias sentencias q estan de mas pq las hice para armar la funcion q tendria q descartar los repetidos, las dejo para q vean lo q trataba de hacer, pero si se le sacan el codigo anda igual.. #include<stdio.h> #include<math.h> #define LIMITE 500 int compara_vector(double vec[], double num, int indice); main() { int i, j, k, m = 1; double lado1 = (double)1, lado2 = (double)1, hipotenusa = (double)1; double num, rtdoH, rtdoLs, rtdoM[LIMITE*2]; for (i = 1; i <= LIMITE; i++){ lado2 = 1; for ( j = 1; j <= LIMITE; j++){ hipotenusa = 1; for (k = 1; k <= LIMITE; k++){ rtdoH = pow((hipotenusa ), (double)2); rtdoLs = (pow(lado1 , (double)2)) + (pow(lado2 , (double)2)); rtdoM[m] = lado1 * lado2 * hipotenusa; if (rtdoH == rtdoLs){ num = rtdoM[m]; if((compara_vector(rtdoM, num, m)) == 0) printf("\t%.0f\t%.0f\t=\t%.0f\n", lado1 , lado2 , hipotenusa ); m++; } hipotenusa++; } lado2++; } lado1++; } return 0; } int compara_vector(double vec[], double num, int indice) { int i; for(i = indice; i >= 0; i--){ if(vec[i] != num){ return 0; } } }
Bueno espero sugerencias!!
|
|
|
13
|
Programación / Programación C/C++ / [C]Calcular pi
|
en: 10 Agosto 2010, 19:47 pm
|
Hola gente!! dejo unos programitas que hice para calcular pi con distintas series infinitas.. y ya q los posteo aprovecho para dejar unas dudas q tengo.. Algoritmo Gottfried Wilhem von Leibniz = 4(1/1-1/3+1/5-1/7+1/9...) #include<stdio.h> main() { int i, j = 1; double pi = 0, rtdo; for (i = 0; i < 1000000000; i++){ rtdo = 4 / (double)j; if (i % 2 == 1) pi -= rtdo; else pi += rtdo; j += 2; } printf("Valor de Pi: %.16f", pi ); return 0; }
Algoritmo John Wallis = 2 ( 2/1 x 2/3 x 4/3 x 4/5 x 6/5 x 6/7 ....) #include<stdio.h> main() { int i, j = 2, k = 1; double pi = 2; for (i = 0; i < 1000000000; i++){ pi *= (j / (double)k); if (i % 2 == 0) k += 2; else j += 2; } printf("Valor de Pi: %.16f", pi ); return 0; }
Estos 2 funcionan bien pero me devuelven solo 16 decimales, desp de ese devuelve todo 0.. pq?? Algoritmo Leonhard Euler = 1 + 1/3 + 1x2 / 3x5 + 1x2x3 / 3x5x7 +.... #include<stdio.h> main() { int i, cte = 1, cte2 = 1; double pi = 1, j = 1, k = 1; for (i = 0; i < 100; i++){ j *= cte++; k *= (cte2 += 2); pi += (j / k); } return 0; }
En este tengo el problema de q como se forman nº tan grandes no puedo hacer un bucle de mas de 100 vueltas y ademas no da el rtdo, no pude encontrarle solucion, creo q el codigo esta bien hecho.. Saludos!
|
|
|
14
|
Programación / Programación C/C++ / [C]Factorial de un numero
|
en: 9 Agosto 2010, 17:11 pm
|
Hola foreros!! aca dejo un programita que calcula el factorial de un numero. El problema que tiene es que cuando se ingresa un numero mayor a 12 da un resultado erroneo, declare la variable factorial con todos los tipos pero en ningun caso funciono.. como se podria solucionar?? #include<stdio.h> main() { int i, num; double factorial = 1; printf("FACTORIAL DE UN NUMERO\n\n"); printf("Ingrese un numero: "); for (i = 1; i <= num; i++) factorial *= i; printf("\n%d! = %.0f\n", num , factorial ); return 0; }
P.D: Acabo de modificar el codigo, ahora si factorea cualquier nº. Grax rob1104!
|
|
|
15
|
Programación / Programación C/C++ / [C]Fuerza Bruta
|
en: 11 Julio 2010, 10:07 am
|
Hola a todos!! como primer post quiero compartir con uds un programita q estuve haciendo en C q simule un programa de fuerza bruta, solo q mas sencillo.. estoy aprendiendo a programar en C asiq soy novato, cualquier sugerencia, propuesta, o lo q sea para mejorar el codigo y aprender sera bienvenida .. #include<stdio.h> #include<stdlib.h> #include<time.h> int mystrlen(char *s); void mystrcpy(char *s, char *t); int mystrcmp(char *s, char *t); int main() { char pass[]="MONO"; int i, num; num = mystrlen(pass); char bf[num]; //srand(time(NULL)); mystrcpy(pass, bf); while(bf != pass) { for(i=0; bf[i] != '\0'; i++){ bf [i ] = 'A'+rand()% ('A'-'Z'); } if((mystrcmp(pass, bf)) == 1) break; } return 0; } int mystrlen(char *s) { int i; for(i=0; *s != '\0'; i++, s++); return i; } void mystrcpy(char *s, char *t) { for(; *s != '\0'; s++, t++){ *t = *s; } *t = '\0'; } int mystrcmp(char *s, char *t) { int r; while(*s != '\0'){ r = *s - *t; if(*t == '\0') break; else{ if(r == 0) s++, t++; else s++; } } if(*t == '\0') return 1; }
Algo q no pude lograr sin un strcpy es q copie los mismos lugares en el string bf, pq si lo hacia con un strlen siempre me copiaba 3 por el '\0', la unica forma en la q pude hacerlo fue con un strcpy.. bueno un saludo, espero que les guste!!
|
|
|
|
|
|
|