Solved

Imladris improvement algorithm-compname()

Posted on 2001-06-05
2
197 Views
Last Modified: 2010-04-15
Dear imladris,

as the first question of mine if you could stil remember :
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cprog&qid=10345372
I would like to improve and make a few changes..
my requirement is to change this program into the last one you made for me...

compare name with datafile (names.dat) and then print the name matches and then return  number of matched names...get ird of the output header and all information like death date, marriage, last name , first name..only keep name  to compare with datafile...(anyway you would understand this easily since it's eaxtly the same requirements as you have done be4)

many thanks,
Korsila

p.s. I need to think for a good requirement to challenge your skill :) and i would be deluighted if you coud move your position of expert to second  ^_^
hope to see your pix one day wining expert of the month heheheh


0
Comment
Question by:korsila
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 6162416
This should do it:

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

int compname(char *,char *);
int compchar(char a,char b);
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+"); //open file in read/write mode
  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); //seek to start of file
      matched=0;
      mc=0;
      while(fgets(line,500,namefile)!=NULL)
      {   line[strlen(line)-1]='\0';
          if(compname(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);
}

int compname(char *nm1,char *nm2)
{   int i,l1,l2;

   l1=strlen(nm1);
   l2=strlen(nm2);
   for(i=0; i<l1; ++i)
   {   if(i<l2 && compchar(nm1[i],nm2[i]))continue;
       if(i+1<l2 && compchar(nm1[i],nm2[i+1]))continue;
       if(i+2<l2 && compchar(nm1[i],nm2[i+2]))continue;
       if(i-1<l2 && i>0 && compchar(nm1[i],nm2[i-1]))continue;
       if(i<l2 && i>0 && compchar(nm1[i-1],nm2[i]))continue;
       if(i+1<l1 && i<l2 && compchar(nm1[i+1],nm2[i]))continue;
       if(i+2<l1 && i<l2 && compchar(nm1[i+2],nm2[i]))continue;
       if(i+1<l1 && i+1<l2 && compchar(nm1[i+1],nm2[i+1]))continue;
       if(i+2<l1 && i+2<l2 && compchar(nm1[i+2],nm2[i+2]))continue;
       return(0);
   }
   return(1);
}

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


// 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;
}
0
 

Author Comment

by:korsila
ID: 6164122
thanks...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 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

18 Experts available now in Live!

Get 1:1 Help Now