
Funciones Varias
C/Visual C
Publicado el 12 de Abril del 2005 por Francisco Herrera
40.782 visualizaciones desde el 12 de Abril del 2005
Código que engloba las funciones de:
- Sumar, restar, multiplicar y dividir
- Tabla de multiplicar
- Factorial
- Par o impar
- Longitud de una cadena
- Copiar, comparar, juntar y invertir cadenas
- Promedio
- Ordenar arreglos
- Arreglos con valores aleatorios
- Piramide
- Contar caracteres (espacios)
- Cambiar números por letras
- Saber si una palabra es polindromo
- Agenda
- Volumen de un cubo
Realizado en Borland Turbo C++
- Sumar, restar, multiplicar y dividir
- Tabla de multiplicar
- Factorial
- Par o impar
- Longitud de una cadena
- Copiar, comparar, juntar y invertir cadenas
- Promedio
- Ordenar arreglos
- Arreglos con valores aleatorios
- Piramide
- Contar caracteres (espacios)
- Cambiar números por letras
- Saber si una palabra es polindromo
- Agenda
- Volumen de un cubo
Realizado en Borland Turbo C++
Comentarios sobre la versión: Versión 1 (11)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nx {
char name[20];
int value;
};
struct nx numarr[] = {
{ \"Cero\", 0 }, { \"Uno\", 1 }, { \"Dos\", 2 }, { \"Tres\", 3 }, { \"Cuatro\", 4 }, { \"Cinco\", 5 },
{ \"Seis\", 6 }, { \"Siete\", 7 }, { \"Ocho\", 8 }, { \"Nueve\", 9 }, { \"Diez\", 10 }, { \"Once\", 11 }, { \"Doce\", 12 },
{ \"Trece\", 13 }, { \"Catorce\", 14 }, { \"Quince\", 15 }, { \"Dieciseis\", 16 }, { \"Diecisiete\", 17 }, { \"Dieciocho\", 18 },
{ \"Diecinueve\", 19 },{ \"Veinte\", 20 }, { \"Veintiuno\", 21 }, { \"Veintidos\", 22 }, { \"Veintitres\", 23 },{ \"Veinticuatro\", 24 },
{ \"Veinticinco\", 25 }, { \"Veintiseis\", 26 }, { \"Veintisiete\", 27 }, { \"Veintiocho\", 28 }, { \"Veintinueve\", 29 },
{ \"Treinta\", 30 }, { \"Cuarenta\", 40 }, { \"Cincuenta\", 50 }, { \"Sesenta\", 60 }, { \"Setenta\", 70 }, { \"Ochenta\", 80 }, { \"Noventa\", 90 }};
/* denominations for large numbers */
char *denom[]=
{ \"Cien\", \"Mil\", \"Millones\", \"Billones\", \"Trillones\", \"Quatrillones\", \"Quintillones\" };
/* take a two-digit number and cvt to words. */
char *cvt2(int val)
{
int i=0;
static char word[80];
while( numarr[++i].value <= val)
/* nothing */;
strcpy(word,numarr[i-1].name);
val -= numarr[i-1].value;
if (val > 0)
{
strcat(word,\" y \");
strcat(word,numarr[val].name);
}
return (word);
}
/* take a 3 digit number and cvt it to words */
char *cvt3(int val)
{
int rem, mod;
static char word[80];
word[0] = \'\\0\';
mod = val % 100;
rem = val / 100;
if ( rem > 0 )
{
strcat(word,numarr[rem].name);
if (strcmp(word,\"Uno\")== 0) strcpy(word,\"Ciento\");
else if (strcmp(word,\"Cinco\")== 0) strcpy(word,\"Quinientos\");
else if (strcmp(word,\"Siete\")== 0) strcpy(word,\"Setecientos\");
else if (strcmp(word,\"Nueve\")== 0) strcpy(word,\"Novecientos\");
else strcat(word,\"cientos\");
if (mod > 0)
strcat(word,\" \");
}
if ( mod > 0 )
{
strcat(word, cvt2(mod));
}
if ( mod == 0 && val == 100) strcpy(word,\"Cien\");
return(word);
}
/* here\'s the routine that does the rest */
char *itowords(long val)
{
int tri; /* last three digits */
int place = 0; /* which power of 10 we are on */
int neg=0; /* sign holder */
char temp[255]; /* temporary string space */
static char word[255];
char phrase[100];
word[0] = \'\\0\';
/* check for negative int */
if (val < 0 )
{
neg = 1;
val = -val;
}
if ( val == 0 )
{
strcpy(word,\"Cero\");
return(word);
}
/* what we do now is break it up into sets of three, and add the */
/* appropriate denominations to each. */
while (val > 0 )
{
phrase[0] = \'\\0\';
tri = val % 1000; /* last three digits */
val = val / 1000; /* base 10 shift by 3 */
if (tri > 0 )
{
if (strcmp(word,\"Uno\")!= 0) strcat(phrase,cvt3(tri));
strcat(phrase,\" \");
}
if ((place > 0 ) && (tri > 0))
{
strcat(phrase,denom[place]);
if (strlen(phrase) == 7) strcpy(phrase,denom[place]);
strcpy(temp,strstr(phrase,\"Uno\"));
if (strlen(phrase) == 12 && strcmp(phrase,\"Uno Billones\") == 0) strcpy(phrase,\"Un Billon\");
if (strcmp (\"Uno Mil\",temp) == 0)
{
temp[0] = \'\\0\';
strncat(temp,phrase,(strlen(phrase)-5));
strcat(temp,\" Mil\");
strcpy(phrase,temp);
temp[0] = \'\\0\';
} else if (strcmp(\"Uno Millones\",temp) == 0)
{
temp[0] = \'\\0\';
strncat(temp,phrase,(strlen(phrase)-10));
strcat(temp,\" Millon\");
strcpy(phrase,temp);
temp[0] = \'\\0\';
}
temp[0] = \'\\0\';
strcpy(temp,strstr(phrase,\"Ciento\"));
if (strcmp(\"Ciento Mil\",temp) == 0)
{
temp[0] = \'\\0\';
strncat(temp,phrase,(strlen(phrase)-6));
strcat(temp,\" Mil\");
strcpy(phrase,temp);
temp[0] = \'\\0\';
}
}
place++;
/* got the phrase, now put it in the string */
strcpy(temp,word);
if ((val > 0) && (tri > 0))
{
strcpy(word,\" \");
strcat(word,phrase);
}
else
strcpy(word,phrase);
strcat(word,temp);
}
/* remember that minus sign */
if (neg)
{
strcpy(temp,word);
strcpy(word,\"- \");
strcat(word,temp);
}
return(word);
}
main(int argc, char **argv)
{
int i=0;
while (++i < argc )
printf(\"%s\\n\",itowords(atol(argv[i])));
}
Esta muy bien hecho tu programita y que aportes los clasicos programitas hechos en clase, te pongo un tache por lo siguiente:
1.- Tu programita no esta hecho para anti-usuarios. Es muy incomodo usar números para las alternativas multiples.
2.- No tienes opcion para regresar a una página anterior
3.-
Esta muy bien hecho tu programita y que aportes los clásicos programitas hechos en clase, te pongo un tache por lo siguiente:
1.- Tu programita no esta hecho para anti-usuarios. Es muy incomodo usar números para las alternativas multiples.
2.- No tienes opcion para regresar a una página anterior.
Espero algún día me puedas responder, para ayudarnos en este agradable mundo de la informática
*/
El programa salvo o mi punto de Programacion....
Saludos desde El Salvador... XD *=) @>-''--(