CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Feb 2006
    Posts
    1

    Exclamation Problem with sort algorithm

    I have a problem with the sorting algorithm which is at the edn of the code.
    Can someone help me !!!

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    #define MAX_STRING_SPACE        1000
    #define MAX_NUM_STRINGS         250
    #define MAX_STRING_SIZE         300
    
    int bmsearch(char *text,char *pat,int *no,int *pos)
    {
    	int i,table[256];
    	int len=strlen(pat);
    	*no=0;
    	int compcount=0;
    	for(i=0;i<256;i++)table[i]=len;
    	for(i=0;i<len;i++)table[pat[i]]=len-(i+1);
    	int ptct=len-1;
    	while(ptct<strlen(text))
    	{
    		int count=0;
    		while(count<len)
    		{
    			if(text[ptct-count]!=pat[len-1-count]){compcount++;break;}
    			else count++;
    		}
    		if(count==len)
    		{
    			(*no)++;
    			*(pos+(*no)-1)=(ptct-count+1);
    			ptct+=len;
    		}
    		else
    		{ptct+=(table[text[ptct-count]]-count);}
    	}
    	return compcount;
    }
    
    main()
    {
    	char text[300],pat[100];
    	int no,count,pos[20],i;
    	cout<<"\nEnter the text string:";
    	gets(text);
    	cout<<"Enter the searched pattern:";
    	gets(pat);
    	count=bmsearch(text,pat,&no,pos);
    	cout<<"\n\nPattern string occoured "<<no<<" times.";
    	cout<<"\n"<<count<<" comparisons required";
    	cout<<"\nPositions of occourence:\n";
    	for(i=0;i<no;i++)
    		cout<<pos[i]<<"\n";	
    	cout<<"\nThe index of the words"<<endl;
    	for (int j=0;j<300;j++)
    	{
    		if (j==0) cout<<j<<" - ";
    		if(text[j]==' '||text[j]=='.'||text[j]==','||text[j]=='!'&&(j+2)<strlen(text))
    			if (text[j+1]==' '||text[j+1]=='.'||text[j+1]==','||text[j]=='!'&&(j+2)<strlen(text))
    			{cout<<"\n"<<j+3<<" - ";j++;}
    			else 
    				cout<<"\n"<<j+2<<" - ";
    		else
    			if (j<strlen(text))
    				cout<<text[j];
    	}
    // Till here everything is ok. But i cannot make this sorting algorithm work	
            char string_space[MAX_STRING_SPACE];
            char *strings[MAX_NUM_STRINGS];
    
            /* Read the strings. */
            char *next_space = string_space;
            int inloc = 0;
            while(text) {
                    /* Find the length of the string and see if it fits. */
    			int length = strlen(text) + 1;
    			if(next_space + length >= string_space + MAX_STRING_SPACE)
    				break;
    			if(inloc >= MAX_NUM_STRINGS)
    				break;
    
    			/* Place the string into the structure. */
    			strings[inloc++] = next_space;
    			strcpy(next_space, text);
    			next_space += length;
    		}
    
    		cout<<"\n--------------------------------------------------\n";
    
    		/* Perform the sort.  Outer loop goes through destination of the minimum string. */
    		int strloc;
    		for(strloc = 0; strloc < inloc - 1; ++strloc) 
    		{
    			/* Scan the remaining strings for ones smaller. */
    			int scan;
    			for(scan = strloc + 1; scan < inloc; ++scan) 
    			{
    				if(strcmp(strings[strloc], strings[scan]) > 0) 
    				{
    					/* Exchange the strings. */
    					char *tmp = strings[strloc];
    					strings[strloc] = strings[scan];
    					strings[scan] = tmp;
    				}
    			}
    		}
    
    		/* Print 'em. */
    		for(strloc = 0; strloc < inloc; ++strloc) 
    		{
    			cout<<strings[strloc]<<endl;
    		}
    	
    	
    
    	   return 0;
    }
    *Edit* : Added code tag
    Last edited by JeffB; February 23rd, 2006 at 01:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured