All pastes #638411 Raw Edit

Something

public text v1 · immutable
#638411 ·published 2007-07-29 20:18 UTC
rendered paste body
#include <stdio.h>
#include <string.h>

int  update_phonebook (char** bookNames, long* bookNumbers, int bookSize, 
                      char* newABOrder);
int  check_name (char* checking, char* abOrder);
int  build_new_list (char** bookNames, int bookSize);
void swap (char** firstStr, char** nextStr);
void max_sort (char** bookNames, int bookSize, char* abOrder);
int  find_max (char** bookNames, int last_id, char* abOrder);
int  str_cmp (char* compared, char* max_id_inBook, char* abOrder);
int  cmp_by_abc  (char* abOrder, char char_in_compared, char char_max_id_inBook );


int main ()
{
    char* names[3] = {"moshe", "boris", "ahmad"};
    long numbers[3] = {123, 456, 789};
    char abOrder[]= "ambosrhed";
    char checking []="boris"; 
    int count=0;
    int i=0;

    count=update_phonebook (names, numbers, 3, abOrder);
    printf ("Amount of names in new PB is: %d", count);
    
   /*for (i=0; i<3; i++)
    {
     printf ("\nname %d is %s", i, names [i]);
    }
    printf ("maxid is %s", names[find_max (names, 2, abOrder)]);
    for (i=0; i<3; i++)
    {
     printf ("\nname %d is %s", i, names [i]);
    }
    max_sort (names, 3, abOrder);*/
    for (i=0; i<3; i++)
    {
     printf ("\nname %d is %s", i, names [i]);
    }
     // for (i=0; i<3; i++)
    //{
    // check_name (checking, abOrder);
     //}
    //for (i=0; i<3; i++)
    //{
     //printf ("\nname %d is %s", i, names [i]);
    //}
    
    
 
   getchar ();
    return 0;
}
/*update phonebook*/
int update_phonebook (char** bookNames, long* bookNumbers, int bookSize, 
                      char* abOrder)
{
   int i=0, newBookSize=0;
     
   for (i=0; i<bookSize; i++)
   {
      if (check_name(bookNames[i], abOrder)==0) 
         newBookSize++;      
   }
   build_new_list (bookNames, bookSize); 
   max_sort (bookNames, newBookSize, abOrder);
   
   return newBookSize;              
}

/*check_name*/

int check_name (char* checking, char* abOrder)
{
    int i = 0,j = 0, k = 0;

    for  (i=0;(checking[i]) != '\0'; i++)
    {
       
       for (j=0; (abOrder [j]) != '\0'; j++)
       {
	       if (checking [i] == abOrder [j])
		      break;	      
       }
       
       if (abOrder[j]=='\0')
       {
          for (k=0; *(checking+k)!='\0'; k++)
              {
                  printf ("\nchecking[%d] is %c", k,*(checking+k));
                  *(checking+k)='\0'; // is there a problem here?
                  printf ("\nchecking[%d] is %c", k,*(checking+k));
              }     
          //printf ("Letters in name do not exist in NEW phonebook\n");
          return -1;
       }
    }
    return 0;
}
/*build new list*/
int build_new_list (char** bookNames, int bookSize)
{
    int i=0, j=0, k=0 ;
    
    for (i=0; i<bookSize; i++)
    {
        if (bookNames [i][0]!=0)
           continue;
           
        else if (bookNames [i][0]==0)
        {
           swap(&bookNames[i], &bookNames[i+1]);
        }
    }
}
/*swap*/
void swap (char** firstStr, char** nextStr)//ok?
{
   char* temp, i=0;

     temp=*firstStr;
     *firstStr=*nextStr;
     *nextStr=temp;
}
/*max_sort*/
void max_sort (char** bookNames, int bookSize, char* abOrder)
{
     int last_id, max_id;
     
     for ( last_id=(bookSize-1); last_id>=1; last_id--)
     {
         max_id=find_max (bookNames, last_id, abOrder);
         if (max_id!=last_id)
            swap (&bookNames[max_id], &bookNames[last_id]);
     } 
}

/*find_max*/
int find_max (char** bookNames, int last_id, char* abOrder)
{
    int max_id=0, i=0;
    int compare=0;
    
    for (i=1; i<=last_id; i++)//
    {
        if (str_cmp(bookNames[i],bookNames[max_id], abOrder) > 0)
        {
           max_id=i;
        }
    }
    return  max_id;
}

/*str_cmp*/
int str_cmp (char* compared, char* max_id_inBook, char* abOrder)
{
  int i=0, n=0,m=0;
  int match=0;

  n=strlen(compared);
  m=strlen(max_id_inBook);

  for (i=0; (i<n)||(i<m); i++)
  {
      match=cmp_by_abc (abOrder, compared[i], max_id_inBook[i]);
      if (match!=0)
      {
      break;
      }
  }

  if ( (match==0)&&(n!=m) ) //matching chars but one words is longer than the other
  {
     if (n>m)
     {
	  match= 1;
     }
     else if (n<m)
     {
	  match= -1;
     }
  }

  return match;
}
/*cmp_by_abc*/
int cmp_by_abc  (char* abOrder, char char_in_compared, char char_max_id_inBook )
{

 int i=0;

 if (char_in_compared == char_max_id_inBook)
 {
    return 0;
 }

 for (i=0; abOrder[i] != '\0'; i++)
 {
    if (char_in_compared == abOrder[i])
    {
       return -1;
    }
    if (char_max_id_inBook == abOrder[i])
    {
       return 1;
    }
 }
}