Solved

Imladris improvement algorithm-compname()

Posted on 2001-06-05
2
201 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing a array as parameter - C 2 95
Problem to show menu 5 88
Why isn't object file created? 6 57
Memory going from 12gb to 64gb or 96gb. worth it? 15 172
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements 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.

785 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