Solved

explaining compname () algorithms

Posted on 2001-08-02
6
190 Views
Last Modified: 2010-04-15
Dear Expert, especially to imladris who has implemented these following codes...

I just had a chance to have a closer look at all of your codes -Imladris...and i found out that 3 of your codes (3 files below) which u have implemented, are quiet different especially the output I got....
could you comment and explain each code  and please illustrate how they are different from each other....am a bit confused and it was even more when I got 3 differnt outputs from them..
p.s. please explain clearly especially how different of each codes....

/*file1 */
                  int compname(char *nm1,char *nm2)
                 {   int i,l1,l2,len;
                    float same,diff;

                    l1=strlen(nm1);
                    l2=strlen(nm2);
                     len=(l1>l2?l1:l2);
                     same=0.;
                     diff=0.;
                    for(i=0; i<len; ++i)
                    {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                        else diff+=1.;
                     }
                    return(same/(same+diff)>0.5);
                 }

                   int compchar(char a,char b)
                   {     return(toupper(a)==toupper(b));
                   }


--------------------
/*file2 */
                   float compname(char *nm1,char *nm2)
                   {   int i,j,k,l1,l2,len;
                     float same,diff;

                     same=diff=0.;
                     l1=strlen(nm1);
                     l2=strlen(nm2);
                     j=0;
                     for(i=0; i<l1; ++i)
                     {   if(compchar(nm1[i],nm2[j]))
                         {   same+=1.;
                             ++j;
                         }
                         else
                         {   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
                             if(k<l2)
                             {   same+=1.;
                                 diff+=(float)(k-j);
                                 j=k+1;
                             }
                         }
                     }
                     if(same==0. && diff==0.)return(0.);
                     return(same/(same+diff));
                   }

--------------------
/* file3 */
int compname(char *nm1,char *nm2)
                 {   int i,l1,l2,len;
                    float same,diff;

                    l1=strlen(nm1);
                    l2=strlen(nm2);
                     len=(l1>l2?l1:l2);
                     same=0.;
                     diff=0.;
                    for(i=0; i<len; ++i)
                    {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                        else diff+=1.;
                     }
                    return(same/(same+diff)>0.5);
                 }
-----------------------

0
Comment
Question by:korsila
  • 4
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 30 total points
ID: 6345942
Yes, these compnames are from when we started with different algorithms for determining whether names matched or not. I don't remember what the programs were called, but here is what they appear to do:

/*file1 */

// return true, indicating that nm1 matches nm2 if
// the number of identical letters, divided by total
// letters is greater than 1/2.
// This algorithm simply compares characters by position
// i.e. smith and smath have 4 same and 1 diff
// and  smith and smeeth have 2 same and 3 diff
                 int compname(char *nm1,char *nm2)
                {   int i,l1,l2,len;
                   float same,diff;

                   // get length of names
                   l1=strlen(nm1);
                   l2=strlen(nm2);

                    //use greatest length for loop
                    //initialize same and different
                    //letter counters
                    len=(l1>l2?l1:l2);
                    same=0.;
                    diff=0.;

                   //for each letter in the longest name
                   //  if both names have letters and letters
                   //      are the same, increment same
                   //  else increment diff
                   for(i=0; i<len; ++i)
                   {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                       else diff+=1.;
                    }

                   // return match if same/(same+diff) > 0.5
                   return(same/(same+diff)>0.5);
                }

                  //compare two characters, ignoring case
                  //  done by making both uppercase
                  int compchar(char a,char b)
                  {     return(toupper(a)==toupper(b));
                  }



/*file2 */
//return match value for nm1 and nm2 this compname
//This algorithm tracks the last matching letters
//i.e. smith and smath have 4 same and 1 diff
//and smith and smeeth also have 4 same and 2 diff
                  float compname(char *nm1,char *nm2)
                  {   int i,j,k,l1,l2,len;
                    float same,diff;

                    //initialize same and diff letter counters
                    //and nm lengths
                    //initialize j: last matched position
                    same=diff=0.;
                    l1=strlen(nm1);
                    l2=strlen(nm2);
                    j=0;

                    //for all letters in nm1
                    for(i=0; i<l1; ++i)
                    {   //check letters in nm1 versus nm2
                        //if they're the same
                        //   increment same and j
                        if(compchar(nm1[i],nm2[j]))
                        {   same+=1.;
                            ++j;
                        }

                        //else
                        //  compare current nm1 character
                        //  against all characters in nm2
                        //  from last matched position to end
                        //  if match found
                        //    increment same
                        //    increase diff by number of
                        //       letters skipped
                        //       set j to new last matched
                        else
                        {   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
                            if(k<l2)
                            {   same+=1.;
                                diff+=(float)(k-j);
                                j=k+1;
                            }
                        }
                    }

                    //if null case and both same and diff
                    //are 0, return match value of 0
                    //else return same / (same + diff)
                    if(same==0. && diff==0.)return(0.);
                    return(same/(same+diff));
                  }


/* file3 */
//This one looks the same as file1 to me
int compname(char *nm1,char *nm2)
                {   int i,l1,l2,len;
                   float same,diff;

                   l1=strlen(nm1);
                   l2=strlen(nm2);
                    len=(l1>l2?l1:l2);
                    same=0.;
                    diff=0.;
                   for(i=0; i<len; ++i)
                   {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                       else diff+=1.;
                    }
                   return(same/(same+diff)>0.5);
                }

0
 

Author Comment

by:korsila
ID: 6351490
oops sory "file3" is look like this ..a bit different from "file2"

                float compname(char *nm1,char *nm2)
                 {   int i,j,k,l1,l2,len;
                   float same,diff;

                   same=diff=0.;
                   l1=strlen(nm1);
                   l2=strlen(nm2);
                   j=0;
                   for(i=0; i<l1; ++i)
                   {   if(compchar(nm1[i],nm2[j]))
                       {   same+=1.;
                           ++j;
                       }
                       else
                       {   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
                           if(k<l2)
                           {   same+=1.;
                               diff+=(float)(k-j);
                               j=k+1;
                           }
                       }
                   }
                   if(j<l2)diff+=(float)(l2-j);----here the different from file 2
                   return(same/(same+diff));
                 }

0
 

Author Comment

by:korsila
ID: 6351500
explain here a bit more when u get back from holiday...
  if(j<l2)diff+=(float)(l2-j);

thanxxxxxxxxxxxxxxx....
0
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
LVL 16

Expert Comment

by:imladris
ID: 6380232
Ah, j is the last matched letter. That line takes care of the case when a trailing set of letters in name2 produces no matches. In that case all the letters from j until the end of the string are different and should be added to diff.
0
 

Author Comment

by:korsila
ID: 6382211
0
 

Author Comment

by:korsila
ID: 6382217
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now