#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//This program is a sorting application that reads a sequence of numbers
//from a file and prints them on the screen . The reading from the file here,
//is a call back function .
typedef int (*CompFunc)(const char* , const char*);
typedef int (*ReadCheck)(char nullcheck);
char array[100];
//Let this function be done in the library itself . It doesn't care as to
//where the compare function and how is it implemented . Meaning suppose
//the function wants to do sort in ascending order or in descending order
//then the changes have to be done by the client code in the "COMPARE" function
//who will be implementing the lib code .
void ReadFile(FILE *fp,ReadCheck rc)
{
char a;
char d[100];
int count = 0,count1=0;
a=fgetc(fp);
while(1 != (*rc)(a))
{ if(a=='\0')
{
//d[count1]='\0';
strcpy(&array[count],d);
count=count+1;
}
else
{
d[count1]=a;
count1=count1+1;
}
}
}
void Bubblesort(char* array , int size , int elem_size , CompFunc cf)
{ int i,j;
int *temp;
for(i=0;i < size ;i++)
{
for (j=0;j < size -1 ; j++)
{
// make the callback to the comparision function
if(1 == (*cf)(array+j*elem_size,array+ (j+1)*elem_size))
{
//interchanging of elements
temp = malloc(sizeof(int *) * elem_size);
memcpy(temp , array+j*elem_size,elem_size);
memcpy(array+j*elem_size,array+(j+1)*elem_size,elem_size);
memcpy(array + (j+1)*elem_size , temp , elem_size);
free(temp);
}
}
}
}
//Let these functions be done at the client side
int Compare(const char* el1 , const char* el2)
{
int element1 = *(int*)el1;
int element2 = *(int*)el2;
if(element1 < element2)
return -1;
if(element1 > element2)
return 1 ;
return 0;
}
int ReadChecked(char nullcheck)
{
if (nullcheck=='\n')
return 1;
else
return 0;
}
int main()
{
FILE *fp1;
int k;
fp1=fopen("readdata.txt","r");
ReadFile(fp1,&ReadChecked);
Bubblesort((char*)array,5,sizeof(array[0]),&Compare);
printf("after sorting \n");
for (k=0;k<5;k++)
printf("%d",array[k]);
return 0;
}
-1
A
回答
1
1
while(1 != (*rc)(a))
相關問題
- 1. Python代碼不會暫停
- 2. 爲什麼在AppFabric中恢復暫停的工作流會給出錯誤?
- 3. 爲什麼我的swift代碼在加載屏幕上暫停?
- 4. 暫停代碼
- 5. 爲什麼代碼會拋出java.sql.SQLException:ORA-01438?
- 6. 這段代碼爲什麼會出錯
- 7. 這段代碼爲什麼會出錯?
- 8. 爲什麼此映射代碼會給出空指針異常?
- 9. 爲什麼此CSV到Python字典代碼會給出IndexError?
- 10. VB.Net暫停代碼
- 11. AlertView暫停代碼
- 12. 線程在睡眠中暫停後會發生什麼?
- 13. 運行此代碼後,爲什麼會出現Ajax錯誤?
- 14. 執行這部分C#代碼後會有什麼行爲?
- 15. 如何從C++代碼中暫停FFmpeg?
- 16. 爲什麼LotusScript在循環中暫停?
- 17. C++:爲什麼這段代碼會出錯?多線性迴歸
- 18. 爲什麼PauseWorkoutSession健康存儲不會暫停會話?
- 19. 爲什麼Chrome會在jQuery中的某些行上暫停?
- 20. 爲什麼在變身之前不會暫停Fx?
- 21. 爲什麼servlet在併發請求中暫停一會兒
- 22. C++爲什麼會拋出?
- 23. 爲什麼JS在某行後停止處理我的代碼?
- 24. 爲什麼我的代碼在「con.Open()」後停止?
- 25. 爲代碼動畫暫停JavaScript執行
- 26. 在Greasemonkey腳本中檢查$會拋出'$未定義',腳本暫停:爲什麼?
- 27. 爲什麼我的程序在使用scanf後暫停?
- 28. 爲什麼我們需要在appendPixelBuffer之後暫停線程:withPresentationTime:?
- 29. 爲什麼腳本無法暫停/暫停?
- 30. 暫停android活動時執行字節碼會發生什麼?
什麼似乎是問題? – 2010-05-27 11:43:08
您是否嘗試過使用調試器來查看它卡住的位置? – Cascabel 2010-05-27 11:52:04