Nombre Password [ Regístrate ]

Diccionario numérico (OIE 5 - 2001) - Código en C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENTRADA "ING.DAT"
#define SALIDA  "ING.RES"

FILE *FileIn,*FileOut;
int fin=0;

int LeerNum();

int main()
{
  FileIn=fopen(ENTRADA,"r");
  FileOut=fopen(SALIDA,"w");
  if (FileIn==NULL || FileOut==NULL) exit(1);
  int x;
  while (!fin)
  {
    x=LeerNum();
    if (!fin) fprintf(FileOut,"%d\n",x);
  }
  fclose(FileIn);
  fclose(FileOut);
  return 0;
}

int LeerNum()
{
  char Cad[80],Aux[80],c;
  int neg=1,num=0,nt=0;
  int st=0;
  while (fscanf(FileIn,"%c",&c)!=EOF)
  {
    if (c==' ') continue;
    if (c=='\n')
      if (st==1) break;
      else continue;
    if (c==EOF) { fin=1; break; }
    fscanf(FileIn,"%s",&Aux);
    memset(Cad,0,sizeof(Cad));
    Cad[0]=c;
    strcat(Cad,Aux);
    st=1;
    if (strcmp(Cad,"negative")==0) neg=-1;
    else if (strcmp(Cad,"zero")==0) num=0;
    else if (strcmp(Cad,"one")==0) num+=1;
    else if (strcmp(Cad,"two")==0) num+=2;
    else if (strcmp(Cad,"three")==0) num+=3;
    else if (strcmp(Cad,"four")==0) num+=4;
    else if (strcmp(Cad,"five")==0) num+=5;
    else if (strcmp(Cad,"six")==0) num+=6;
    else if (strcmp(Cad,"seven")==0) num+=7;
    else if (strcmp(Cad,"eight")==0) num+=8;
    else if (strcmp(Cad,"nine")==0) num+=9;
    else if (strcmp(Cad,"ten")==0) num+=10;
    else if (strcmp(Cad,"eleven")==0) num+=11;
    else if (strcmp(Cad,"twelve")==0) num+=12;
    else if (strcmp(Cad,"thirteen")==0) num+=13;
    else if (strcmp(Cad,"fourteen")==0) num+=14;
    else if (strcmp(Cad,"fifteen")==0) num+=15;
    else if (strcmp(Cad,"sixteen")==0) num+=16;
    else if (strcmp(Cad,"seventeen")==0) num+=17;
    else if (strcmp(Cad,"eighteen")==0) num+=18;
    else if (strcmp(Cad,"nineteen")==0) num+=19;
    else if (strcmp(Cad,"twenty")==0) num+=20;
    else if (strcmp(Cad,"thirty")==0) num+=30;
    else if (strcmp(Cad,"forty")==0) num+=40;
    else if (strcmp(Cad,"fifty")==0) num+=50;
    else if (strcmp(Cad,"sixty")==0) num+=60;
    else if (strcmp(Cad,"seventy")==0) num+=70;
    else if (strcmp(Cad,"eighty")==0) num+=80;
    else if (strcmp(Cad,"ninety")==0) num+=90;
    else if (strcmp(Cad,"hundred")==0) { num*=100; }
    else if (strcmp(Cad,"thousand")==0) { num*=1000; nt+=num; num=0; }
    else if (strcmp(Cad,"million")==0) { num*=1000000; nt+=num; num=0; }
  }
  if (st==0) fin=1;
  nt+=num;
  nt*=neg;
  return nt;
}


© (2001-2008) ALGORITMIA.NET - Política de privacidad