#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. 为什么在AppFabric中恢复暂停的工作流会给出错误?
- 2. 这段代码为什么会出错?
- 3. 为什么代码会抛出java.sql.SQLException:ORA-01438?
- 4. 为什么此映射代码会给出空指针异常?
- 5. 为什么此CSV到Python字典代码会给出IndexError?
- 6. AlertView暂停代码
- 7. 线程在睡眠中暂停后会发生什么?
- 8. 运行此代码后,为什么会出现Ajax错误?
- 9. 执行这部分C#代码后会有什么行为?
- 10. 为什么LotusScript在循环中暂停?
- 11. C++:为什么这段代码会出错?多线性回归
- 12. 为什么PauseWorkoutSession健康存储不会暂停会话?
- 13. 为什么servlet在并发请求中暂停一会儿
- 14. 为什么Chrome会在jQuery中的某些行上暂停?
- 15. C++为什么会抛出?
- 16. 为什么我的代码在“con.Open()”后停止?
- 17. 为代码动画暂停JavaScript执行
- 18. 为什么我的程序在使用scanf后暂停?
- 19. 为什么我们需要在appendPixelBuffer之后暂停线程:withPresentationTime:?
- 20. 警报暂停代码
- 21. 为什么在迭代器上调用remove()会给出ConcurrentModificationException?
- 22. c中以下代码的输出是什么?为什么?
- 23. 为什么c值在给定的代码中重复?
- 24. 为什么https下载暂停PAUSED_WAITING_TO_RETRY?
- 25. 为什么我的tensorflow暂停?
- 26. Flex-AIR:主线(暂停).....为什么?
- 27. 为什么cout和return在以下C++代码中给出了不同的值?
- 28. 为什么在错误发生后IDE会粘住旧代码?
- 29. 为什么python会在`pass`后继续评估代码?
- 30. 为什么不会出现后退按钮出现此代码(UINavigationController问题)
什么似乎是问题? – 2010-05-27 11:43:08
您是否尝试过使用调试器来查看它卡住的位置? – Cascabel 2010-05-27 11:52:04