Link to home
Start Free TrialLog in
Avatar of korsila
korsila

asked on

Imladris- improving and converting C++ to C.

hi again Imladris..
just relised that this is C++ version
could you be able to change it in C....

and also could you update the codes for me to compare the name which user enters with name from
datafile..(name.dat)

and then use this this program algorithm to find whether name are matched ot not....

pretty similar to the one you have doine for me.,..-compname ()

also make sure no different between small and capital letter of names

p.s. take your time..
---------------------

Thanks again sory to keep you busy with all my requirement...

many thanks,

korsila


code is below...


////////////////////////////////////////////////////////////////////////////
//                                                                        //
//  File name: phonex.H                                     //
// ////////////////////////////////////////////////////////////////////////


#ifndef PHONEX_H
#define PHONEX_H

int     phonexMatch(char* name1, char* name2);
int     phonex(char* name);
int     isvowelY(char c);

#endif



////////////////////////////////////////////////////////////////////////////
//                                                                        //
//  File name: phonex.CPP                                //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

// #include "defs.h"
#include "phonex.h"
#include <ctype.h>
#include <string.h>
#include <iostream.h>

extern unsigned long comps;


int phonexMatch(char* nameA, char* nameB)
// calculates phonex codes for two names
// and returns 1 if codes match
{
  char  name1[MAXLINE]; strcpy(name1, nameA);
  char  name2[MAXLINE]; strcpy(name2, nameB);
  int   match = 0;

  // turn names into phonex code equivalents
  if (!phonex(name1))
  {
        cerr << "Error coding name1." << endl;
  }
  else if (!phonex(name2))
  {
        cerr << "Error coding name2." << endl;
  }
  else if (strcmp(name1, name2) == 0)
  {
        // phonex codes are the same
        match = 1;
  }

  return match;
}


/* The following C function to create Phonex codes is converted from
   SDX v1.1 coded by Michael Cooley. Released to the public domain, 1994. */
int phonex(char* name)
{
        char last,code,*p;
        int y=1;

        //
        // PHONEX PRE-PROCESSING ...
        //

        // Deletions effected by replacing with next letter which
        // will be ignored due to duplicate handling of Soundex code.
        // This is faster than 'moving' all subsequent letters.

        // Remove any trailing Ss
        while (name[strlen(name)-1] == 'S')
        {
                name[strlen(name)-1] = '\0';

                comps += 1;
        }

        // Phonetic equivalents of first 2 characters
        // Works since duplicate letters are ignored
        if (strncmp(name, "KN", 2) == 0)
        {
                *name = 'N';            // KN.. == N..

                // AMENDMENT
                comps += 2;
        }
        else if (strncmp(name, "PH", 2) == 0)
        {
                *name = 'F';    // PH.. == F.. (H ignored anyway)

                // AMENDMENT
                comps += 2;
        }
        else if (strncmp(name, "WR", 2) == 0)
        {
                *name = 'R';            // WR.. == R..

                // AMENDMENT
                comps += 2;
        }

        // Special case, ignore H first letter (subsequent Hs ignored anyway)
        // Works since duplicate letters are ignored
        if (*name == 'H')
        {
                *name = *(name+1);

                // AMENDMENT
                comps += 1;
        }

        // Phonetic equivalents of first character
        switch(*name)
        {
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                case 'Y':
                        // vowel equivalence A = E, I, O, U, Y
                        *name = 'A';
                                // AMENDMENT
                                comps += 5;
                        break;
                case 'P':
                        // consonant equivalence B = P
                        *name = 'B';
                                // AMENDMENT
                                comps += 6;
                        break;
                case 'V':
                        // consonant equivalence F = V
                        *name = 'F';
                                // AMENDMENT
                                comps += 7;
                        break;
                case 'K':
                case 'Q':
                        // consonant equivalence C = K
                        *name = 'C';
                                // AMENDMENT
                                comps += 9;
                        break;
                case 'J':
                        // consonant equivalence G = J
                        *name = 'G';
                                // AMENDMENT
                                comps += 10;
                        break;
                case 'Z':
                        // consonant equivalence S = Z
                        *name = 'S';
                                // AMENDMENT
                                comps += 11;
                        break;
                default:
                        // no equivalence for letter, no change
                        break;
        }

        //
        // MODIFIED SOUNDEX CODE
        //

        p=name;
        for ( ;
                *p!='\0'        /* not at end of name */
                && *p!=' '      /* terminate if encountered */
                && *p!=','      /* terminate if encountered */
                && y < 4;       /* code no more than 4 characters */
                p++)
        {
                switch(*p)
                {
                        case 'B':
                        case 'P':
                        case 'F':
                        case 'V':
                                code='1';
                                        // AMENDMENT
                                        comps += 4;
                                break;
                        case 'C':
                        case 'S':
                        case 'K':
                        case 'G':
                        case 'J':
                        case 'Q':
                        case 'X':
                        case 'Z':
                                code='2';
                                        // AMENDMENT
                                        comps += 12;
                                break;
                        case 'D':
                        case 'T':
                                if (*(p+1) != 'C')
                                {
                                        code='3';
                                }
                                // else do not code: TC.. = DC.. = C..
                                        // AMENDMENT
                                        comps += 14;
                                break;
                        case 'L':
                                if (isvowelY(*(p+1)) || (*(p+1) == '\0'))
                                {
                                        // only code if followed by vowel of end
of name
                                        code='4';
                                }
                                        // AMENDMENT
                                        comps += 15;
                                break;
                        case 'M':
                        case 'N':
                                if ((*(p+1) == 'D') || (*(p+1) == 'G'))
                                {
                                        // NG.. = ND.. = N..
                                        *(p+1) = *p;
                                }
                                code='5';
                                        // AMENDMENT
                                        comps += 17;
                                break;
                        case 'R':
                                if (isvowelY(*(p+1)) || (*(p+1) == '\0'))
                                {
                                        // only code if followed by vowel of end
of name
                                        code='6';
                                }
                                        // AMENDMENT
                                        comps += 18;
                                break;
                        default:
                                code=0;
                                break;
                }

                // AMENDMENT
                comps += 3;

                if(
                   last!=code                   /* not the same as previous
position */
                   && code!=0                   /* is not a vowel, etc. */
                   && p!=name)                  /* is not the first letter
*/
                        name[y++]=code; /* then code it */

                last=name[y-1];
                if (y==1) last=code;            // special case for 1st letter
        }

        while(y<4) name[y++]='0';               /* fill in with trailing zeros */
        name[4]='\0';                           /* NULL terminate after 4
characters */
        return 1;
}


int isvowelY(char c)
// returns 1 if c is a vowel or Y, 0 otherwise
{
  // convert to upper case
  c = toupper(c);

  // AMENDMENT: average no. of comparisons
  comps += 3;

  return ( (c == 'A') ||
           (c == 'E') ||
           (c == 'I') ||
           (c == 'O') ||
           (c == 'U') ||
           (c == 'Y') );
}
Avatar of korsila
korsila

ASKER

if you think it's quiet difficult the point would be added later..!!

korsila
That wasn't too bad. Changed the C++ specific stuff (handful of lines) and glued in the match code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


int     phonexMatch(char* name1, char* name2);
int     phonex(char* name);
int     isvowelY(char c);
void    getinput(char *prompt,char n[]);

void main(int argc,char *argv[])
{   int matched,msave,usave,mc;
  char name[150],line[500];
  float LIG;
  FILE *namefile,*mfile,*ufile;

  printf("Save matched names in matched.dat (y or n)?\n");
  gets(line);
  msave=(toupper(line[0])=='Y');
  printf("Save unmatched names in unmatched.dat (y or n)?\n");
  gets(line);
  usave=(toupper(line[0])=='Y');
  namefile=fopen("names.dat","r+");
  if(msave)mfile=fopen("matched.dat","w");
  if(usave)ufile=fopen("unmatched.dat","w");
  do
  {   printf("Please enter name to match:\n");
      gets(name);
      fseek(namefile,0L,0);
      matched=0;
      mc=0;
      while(fgets(line,500,namefile)!=NULL)
      {   line[strlen(line)-1]='\0';
          if(phonexMatch(name,line))
          {    printf("%s\n",line);
               ++mc;
               if(msave)
               {   if(matched==0)fprintf(mfile,"Match name %s\n",name);
                   fprintf(mfile,"%s\n",line);
               }
               matched=1;
          }
      }
      if(matched==1)
       {   if(msave)fprintf(mfile,"----------------------------------------\n");
           printf("Number of Matches: %d\n",mc);
       }
      else
      {   printf("No Matches Found.\n");
          if(usave)
          {   fprintf(ufile,"Match name %s\n",name);
              fprintf(ufile,"No Matches found\n");
              fprintf(ufile,"--------------------\n");
          }
      }
      printf("Compare again (y or n)?\n");
      gets(line);
  } while(toupper(line[0])=='Y');
  fclose(namefile);
  if(msave)fclose(mfile);
  if(usave)fclose(ufile);
}


/* prompt user for input */
/* get input */
/* copy it safely into provided variable */

void getinput(char *prompt,char n[])
{   char ipc[150];

  printf("%s:\n",prompt);
  gets(ipc);
  strncpy(n,ipc,19);
  n[19]='\0';
  return;
}

#define MAXLINE 150

unsigned long comps;


int phonexMatch(char* nameA, char* nameB)
/* calculates phonex codes for two names */
/* and returns 1 if codes match */
{
 int   match;
 char  name1[MAXLINE];
 char  name2[MAXLINE];

 strcpy(name1, nameA);
 strcpy(name2, nameB);
 strupr(name1);
 strupr(name2);
 match=0;
 /* turn names into phonex code equivalents */
 if (!phonex(name1))
 {
       printf("Error coding name1.\n");
 }
 else if (!phonex(name2))
 {
       printf("Error coding name2.\n");
 }
 else if (strcmp(name1, name2) == 0)
 {
       /* phonex codes are the same */
       match = 1;
 }

 return match;
}


/* The following C function to create Phonex codes is converted from
  SDX v1.1 coded by Michael Cooley. Released to the public domain, 1994. */
int phonex(char* name)
{
       char last,code,*p;
       int y=1;

       /*
       / PHONEX PRE-PROCESSING ...
       /

       / Deletions effected by replacing with next letter which
       / will be ignored due to duplicate handling of Soundex code.
       / This is faster than 'moving' all subsequent letters.

       / Remove any trailing Ss */
       while (name[strlen(name)-1] == 'S')
       {
               name[strlen(name)-1] = '\0';

               comps += 1;
       }

       /* Phonetic equivalents of first 2 characters */
       /* Works since duplicate letters are ignored */
       if (strncmp(name, "KN", 2) == 0)
       {
               *name = 'N';            /* KN.. == N..*/

               /* AMENDMENT */
               comps += 2;
       }
       else if (strncmp(name, "PH", 2) == 0)
       {
               *name = 'F';    /* PH.. == F.. (H ignored anyway) */

               /* AMENDMENT */
               comps += 2;
       }
       else if (strncmp(name, "WR", 2) == 0)
       {
               *name = 'R';            /* WR.. == R.. */

               /* AMENDMENT */
               comps += 2;
       }

       /* Special case, ignore H first letter (subsequent Hs ignored anyway) */
       /* Works since duplicate letters are ignored */
       if (*name == 'H')
       {
               *name = *(name+1);

               /* AMENDMENT */
               comps += 1;
       }

       /* Phonetic equivalents of first character */
       switch(*name)
       {
               case 'E':
               case 'I':
               case 'O':
               case 'U':
               case 'Y':
                       /* vowel equivalence A = E, I, O, U, Y */
                       *name = 'A';
                               /* AMENDMENT */
                               comps += 5;
                       break;
               case 'P':
                       /* consonant equivalence B = P */
                       *name = 'B';
                               /* AMENDMENT */
                               comps += 6;
                       break;
               case 'V':
                       /* consonant equivalence F = V */
                       *name = 'F';
                               /* AMENDMENT */
                               comps += 7;
                       break;
               case 'K':
               case 'Q':
                       /* consonant equivalence C = K */
                       *name = 'C';
                               /* AMENDMENT */
                               comps += 9;
                       break;
               case 'J':
                       /* consonant equivalence G = J */
                       *name = 'G';
                               /* AMENDMENT */
                               comps += 10;
                       break;
               case 'Z':
                       /* consonant equivalence S = Z */
                       *name = 'S';
                               /* AMENDMENT */
                               comps += 11;
                       break;
               default:
                       /* no equivalence for letter, no change */
                       break;
       }

       /*
       / MODIFIED SOUNDEX CODE
       */

       p=name;
       for ( ;
               *p!='\0'        /* not at end of name */
               && *p!=' '      /* terminate if encountered */
               && *p!=','      /* terminate if encountered */
               && y < 4;       /* code no more than 4 characters */
               p++)
       {
               switch(*p)
               {
                       case 'B':
                       case 'P':
                       case 'F':
                       case 'V':
                               code='1';
                                       /* AMENDMENT */
                                       comps += 4;
                               break;
                       case 'C':
                       case 'S':
                       case 'K':
                       case 'G':
                       case 'J':
                       case 'Q':
                       case 'X':
                       case 'Z':
                               code='2';
                                       /* AMENDMENT */
                                       comps += 12;
                               break;
                       case 'D':
                       case 'T':
                               if (*(p+1) != 'C')
                               {
                                       code='3';
                               }
                               /* else do not code: TC.. = DC.. = C.. */
                                       /* AMENDMENT */
                                       comps += 14;
                               break;
                       case 'L':
                               if (isvowelY(*(p+1)) || (*(p+1) == '\0'))
                               {
                                       /* only code if followed by vowel of end of name */
                                       code='4';
                               }
                                       /* AMENDMENT */
                                       comps += 15;
                               break;
                       case 'M':
                       case 'N':
                               if ((*(p+1) == 'D') || (*(p+1) == 'G'))
                               {
                                       /* NG.. = ND.. = N.. */
                                       *(p+1) = *p;
                               }
                               code='5';
                                       /* AMENDMENT */
                                       comps += 17;
                               break;
                       case 'R':
                               if (isvowelY(*(p+1)) || (*(p+1) == '\0'))
                               {
                                       /* only code if followed by vowel of end of name */
                                       code='6';
                               }
                                       /* AMENDMENT */
                                       comps += 18;
                               break;
                       default:
                               code=0;
                               break;
               }

               /* AMENDMENT */
               comps += 3;

               if(
                  last!=code                   /* not the same as previous
position */
                  && code!=0                   /* is not a vowel, etc. */
                  && p!=name)                  /* is not the first letter
*/
                       name[y++]=code; /* then code it */

               last=name[y-1];
               if (y==1) last=code;            /* special case for 1st letter */
       }

       while(y<4) name[y++]='0';               /* fill in with trailing zeros */
       name[4]='\0';                           /* NULL terminate after 4
characters */
       return 1;
}


int isvowelY(char c)
/* returns 1 if c is a vowel or Y, 0 otherwise */
{
 /* convert to upper case */
 c = toupper(c);

 /* AMENDMENT: average no. of comparisons */
 comps += 3;

 return ( (c == 'A') ||
          (c == 'E') ||
          (c == 'I') ||
          (c == 'O') ||
          (c == 'U') ||
          (c == 'Y') );
}
Avatar of korsila

ASKER

Dear imladris..
I could compile your codes but could not be able to run it since the unexpect mesage I got below after i compiled the codes..
--------
usr/ccs/bin/ld: Unstaisfied smybols:
 strupr (code)
--------

I think in the function below could be caused the problem:  
int phonexMatch(char* nameA, char* nameB)
....
strcpy(name1, nameA);
strcpy(name2, nameB);
strupr(name1);
strupr(name2);
...
any idea???

many thanks, we're almost there..
korsila
Avatar of korsila

ASKER

forgot to tell you that after compiled this codes the file "phonex.o" has come up as well..!!!

ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of korsila

ASKER

absolutely brilliant!!..Imlradis...seems like you understand C compiler perfectly..!!:)

you really gave me eveything you know..:)

a million thank...

Korsila
Avatar of korsila

ASKER

Dear Imladris,
I got the ergent problems and would like you to sort them out for me...I will put a questions and hope you could get back to me as soon as you can...!!!!

the question based on my old requiements and your old answers...

thanks,
Korsila