示例#1
0
文件: search-2.c 项目: whatot/ma_c
/* main */
int main() {
	char dictionary[12][SPELL_SIZE];
	char lookup[SPELL_SIZE];
	char target[SPELL_SIZE];
	int i;

	/* Create a sorted dictionary of words. */
	strcpy(dictionary[ 0], "HOP");
	strcpy(dictionary[ 1], "HAT");
	strcpy(dictionary[ 2], "TAP");
	strcpy(dictionary[ 3], "BAT");
	strcpy(dictionary[ 4], "TIP");
	strcpy(dictionary[ 5], "MOP");
	strcpy(dictionary[ 6], "MOM");
	strcpy(dictionary[ 7], "CAT");
	strcpy(dictionary[ 8], "ZOO");
	strcpy(dictionary[ 9], "WAX");
	strcpy(dictionary[10], "TOP");
	strcpy(dictionary[11], "DIP");

	if (issort(dictionary, 12, SPELL_SIZE, compare_str) != 0) {
		return 1;
	}

	/* Perform spell checking. */
	strcpy(lookup, "hat");
	memset(target, 0, SPELL_SIZE);
	for (i = 0; i < (int)strlen(lookup); ++i) {
		target[i] = toupper(lookup[i]);
	}
	if (spell(dictionary, 12, target)) {
		fprintf(stdout, "%s is spelled correctly\n", lookup);
	} else {
		fprintf(stdout, "%s is not spelled correctly\n", lookup);
	}

	strcpy(lookup, "dop");
	memset(target, 0, SPELL_SIZE);
	for (i = 0; i < (int)strlen(lookup); ++i) {
		target[i] = toupper(lookup[i]);
	}
	if (spell(dictionary, 12, target)) {
		fprintf(stdout, "%s is spelled correctly\n", lookup);
	} else {
		fprintf(stdout, "%s is not spelled correctly\n", lookup);
	}

	strcpy(lookup, "dip");
	memset(target, 0, SPELL_SIZE);
	for (i = 0; i < (int)strlen(lookup); ++i) {
		target[i] = toupper(lookup[i]);
	}
	if (spell(dictionary, 12, target)) {
		fprintf(stdout, "%s is spelled correctly\n", lookup);
	} else {
		fprintf(stdout, "%s is not spelled correctly\n", lookup);
	}

	return 0;
}
示例#2
0
int main(int argc, char *argv[])
{
	int a[5] = {4,3,1,2,5};
	int i;
	
	issort(a, sizeof(a)/sizeof(int), sizeof(int), compare_int);
	
	for (i = 0; i < 5; i++)
	printf("%d\n", a[i]);
	
	return 0;
}
示例#3
0
int main(){
	setup();

	puts("Before ")
	show(nums, MAX_ITEM);

	puts("Sorted");
	issort()

	enddown();
	return 0;
}
示例#4
0
static int partition(void *data, int esize, int i, int k, int(*compare)(const void *key1, const void *key2))
{
	char *a = (char *)data;
	void *pval, *temp;
	int r[3];
	if((pval = malloc(esize)) == NULL)
		return -1;
	if((temp = malloc(esize)) == NULL)
	{
		free(pval);
		return -1;
	}

	r[0] = (rand() % (k - i + 1)) + i;
	r[1] = (rand() % (k - i + 1)) + i;
	r[2] = (rand() % (k - i + 1)) + i;
	issort(r, 3, sizeof(int), compare_int);
	memcpy(pval, &a[r[1] * esize], esize);

	i--;
	k++;

	while(1)
	{
		do
		{
			k--;
		}while(compare(&a[k * esize], pval) > 0);

		do
		{
			i++;
		}while(compare(&a[i * esize], pval) < 0);

		if(i >= k)
		{
			break;
		}
		else
		{
			memcpy(temp, &a[i * esize], esize);
			memcpy(&a[i * esize], &a[k * esize], esize);
			memcpy(&a[k * esize], temp, esize);
		}
	}

	free(pval);
	free(temp);

	return k;
}
示例#5
0
int
main ()
{
    int i;
    int a[SIZE], b[SIZE];
    srand (time (NULL));

    for (i = 0; i < SIZE; i++)
    {
        a[i] = rand() % 50;
        b[i] = a[i];
    }

    issort (&b, SIZE, sizeof(int), &compare_int);

    for (i = 0; i < SIZE; i++)
    {
        printf ("a[%d]=%d,\tb[%d]=%d\n", i, a[i], i, b[i]);
    }

    return 0;
}
示例#6
0
TEST(Sort_Insertion, issort)
{
	int size = 100;
	int *data = (int *)malloc(sizeof(int) * size);
	int i;
	int result;

	srand(time(NULL));

	for (i = 0; i < size; ++i) {
		data[i] = rand() % size;
		/* data[i] = 5 - i; */
		/* printf("%d\n", data[i]); */
	}

	result = issort(data, size, sizeof(int), compare);
	EXPECT_EQ(0, result);
	for (i = 0; i < size; ++i) {
		EXPECT_TRUE(data[i] >= data[i - 1]);
		/* printf("%d\n", data[i]); */
	}
	free(data);
}
示例#7
0
int main(int argc, char **argv) {

int                iarray[10],
                   marray[10],
                   qarray[10],
                   carray[10],
                   rarray[10];

char               sarray[10][STRSIZ];

int                size = 10;

/*****************************************************************************
*                                                                            *
*  Load the arrays with data to sort.                                        *
*                                                                            *
*****************************************************************************/

iarray[0] = 0;
iarray[1] = 5;
iarray[2] = 1;
iarray[3] = 7;
iarray[4] = 3;
iarray[5] = 2;
iarray[6] = 8;
iarray[7] = 9;
iarray[8] = 4;
iarray[9] = 6;

memcpy(marray, iarray, size * sizeof(int));
memcpy(qarray, iarray, size * sizeof(int));
memcpy(carray, iarray, size * sizeof(int));

rarray[0] = 11111323;
rarray[1] = 99283743;
rarray[2] = 98298383;
rarray[3] = 99987444;
rarray[4] = 43985209;
rarray[5] = 99911110;
rarray[6] = 11111324;
rarray[7] = 39842329;
rarray[8] = 97211029;
rarray[9] = 99272928;

strcpy(sarray[0], "ebcde");
strcpy(sarray[1], "ghidj");
strcpy(sarray[2], "ghiea");
strcpy(sarray[3], "abaae");
strcpy(sarray[4], "abaaa");
strcpy(sarray[5], "abcde");
strcpy(sarray[6], "abbcd");
strcpy(sarray[7], "ddaab");
strcpy(sarray[8], "faeja");
strcpy(sarray[9], "aaaaa");

/*****************************************************************************
*                                                                            *
*  Perform insertion sort.                                                   *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Before issort\n");
print_idata(iarray, size);

if (issort(iarray, size, sizeof(int), compare_int) != 0)
   return 1;

fprintf(stdout, "After issort\n");
print_idata(iarray, size);

/*****************************************************************************
*                                                                            *
*  Perform quicksort.                                                        *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Before qksort\n");
print_idata(qarray, size);

if (qksort(qarray, size, sizeof(int), 0, size - 1, compare_int) != 0)
   return 1;

fprintf(stdout, "After qksort\n");
print_idata(qarray, size);

/*****************************************************************************
*                                                                            *
*  Perform merge sort.                                                       *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Before mgsort\n");
print_sdata(sarray, size);

if (mgsort(sarray, size, STRSIZ, 0, size - 1, compare_str) != 0)
   return 1;

fprintf(stdout, "After mgsort\n");
print_sdata(sarray, size);

/*****************************************************************************
*                                                                            *
*  Perform counting sort.                                                    *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Before ctsort\n");
print_idata(carray, size);

if (ctsort(carray, size, 10) != 0)
   return 1;

fprintf(stdout, "After ctsort\n");
print_idata(carray, size);

/*****************************************************************************
*                                                                            *
*  Perform radix sort.                                                       *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Before rxsort\n");
print_idata(rarray, size);

if (rxsort(rarray, size, 8, 10) != 0)
   return 1;

fprintf(stdout, "After rxsort\n");
print_idata(rarray, size);

return 0;

}
示例#8
0
static int partition(void *data, int esize, int i, int k, int (*compare)
   (const void *key1, const void *key2)) {

char               *a = data;

void               *pval,
                   *temp;

int                r[3];

/*****************************************************************************
*                                                                            *
*  Allocate storage for the partition value and swapping.                    *
*                                                                            *
*****************************************************************************/

if ((pval = malloc(esize)) == NULL)
   return -1;

if ((temp = malloc(esize)) == NULL) {

   free(pval);
   return -1;

}

/*****************************************************************************
*                                                                            *
*  Use the median-of-three method to find the partition value.               *
*                                                                            *
*****************************************************************************/

r[0] = (rand() % (k - i + 1)) + i;
r[1] = (rand() % (k - i + 1)) + i;
r[2] = (rand() % (k - i + 1)) + i;
issort(r, 3, sizeof(int), compare_int);
memcpy(pval, &a[r[1] * esize], esize);

/*****************************************************************************
*                                                                            *
*  Create two partitions around the partition value.                         *
*                                                                            *
*****************************************************************************/

i--;
k++;

while (1) {

   /**************************************************************************
   *                                                                         *
   *  Move left until an element is found in the wrong partition.            *
   *                                                                         *
   **************************************************************************/

   do {

      k--;

   } while (compare(&a[k * esize], pval) > 0);

   /**************************************************************************
   *                                                                         *
   *  Move right until an element is found in the wrong partition.           *
   *                                                                         *
   **************************************************************************/

   do {

      i++;

   } while (compare(&a[i * esize], pval) < 0);

   if (i >= k) {

      /***********************************************************************
      *                                                                      *
      *  Stop partitioning when the left and right counters cross.           *
      *                                                                      *
      ***********************************************************************/

      break;

      }

   else {

      /***********************************************************************
      *                                                                      *
      *  Swap the elements now under the left and right counters.            *
      *                                                                      *
      ***********************************************************************/

      memcpy(temp, &a[i * esize], esize);
      memcpy(&a[i * esize], &a[k * esize], esize);
      memcpy(&a[k * esize], temp, esize);

   }

}

/*****************************************************************************
*                                                                            *
*  Free the storage allocated for partitioning.                              *
*                                                                            *
*****************************************************************************/

free(pval);
free(temp);

/*****************************************************************************
*                                                                            *
*  Return the position dividing the two partitions.                          *
*                                                                            *
*****************************************************************************/

return k;

}
示例#9
0
//-------------------------------------------------------------------------
// add_entry()
// Thu Jan 11 10:41:13 EST 2001
// Tue Apr  3 16:28:20 EDT 2001
//-------------------------------------------------------------------------
// Add a cts or crate entry to the appropriate db. The new entry is inserted 
// 	into the list alphabetically.
//
// input:	db type, 
// 			pointer to a c-string containing a complete entry
// output:	status
//-------------------------------------------------------------------------
int add_entry( int dbType, char *newEntry )
{
	void					*dbptr;						// re-usable pointer for dbs
	int						entrySize, i, numOfEntries;
	int						status = SUCCESS;			// assume the best
	extern struct MODULE	*CTSdb;		// pointer to in-memory copy of data file
	extern struct CRATE		*CRATEdb;	// pointer to in-memory copy of data file

	if( MSGLVL(FUNCTION_NAME) )
		printf( "add_entry()\n" );

//----------------------------
//	adding an entry 
//----------------------------
//-- 'critical section' start
//----------------------------
	// 'lock' with semaphore
	if( lock_file() != SUCCESS ) {
		status = LOCK_ERROR;
		goto AddEntry_Exit;
	}

	// get current number of entries
	if( (numOfEntries = get_file_count(dbType)) < 0 ) {
		status = FILE_ERROR;
		goto AddEntry_Exit;
	}

	// cull db specific info
	switch( dbType ) {
		case CTS_DB:
			dbptr = (void *)CTSdb;
			entrySize = MODULE_ENTRY;
			break;

		case CRATE_DB:
			dbptr = (void *)CRATEdb;
			entrySize = CRATE_ENTRY;
			break;
	}

	// shift current entries by one
	if( numOfEntries )		// ... only if any entries exist
		for( i = numOfEntries - 1; i >= 0; --i )
			memcpy( (char *)dbptr+((i + 1) * entrySize),
					(char *)dbptr+( i      * entrySize),
					entrySize
					);

	// put new entry at head of list
	memcpy( (char *)dbptr, newEntry, entrySize );

	// insertion sort
	if( numOfEntries > 0 ) 		// only insert if more than one entry exists already
		if( issort(dbptr, numOfEntries + 1, entrySize, compare_str) != 0 ) {
			status = ERROR;
			goto AddEntry_Exit;
		}

	// commit change to file
	if( commit_entry(dbType) != SUCCESS ) {
		status = COMMIT_ERROR;
		goto AddEntry_Exit;
	}

	// release semaphore
	if( unlock_file() != SUCCESS )
		status = UNLOCK_ERROR;

//----------------------------
//-- 'critical section' finish
//----------------------------

AddEntry_Exit:
	if( MSGLVL(DETAILS) ) {
		printf( "add_entry(): " ); ShowStatus( status );
	}

	return status;
}