1、要实现排序过程,就要有排序算法,有画图工具,有三个线程具体怎么设计?要解释数据结构和函数代码:/* * vim: filetype=c:fenc=utf-8:ts=4:et:sw=4:sts=4 */#include stddef.hmath.hstdio.hsys/mman.hsyscall.hnetinet/in.hstdlib.h#include graphics.htime.hextern void *tlsf_create_with_pool(void* mem, size_t bytes);extern void *g_heap;/* * GCC insists on _main
2、* http:/gcc.gnu.org/onlinedocs/gccint/Collect2.html /进入图形模式 / init_graphic(0x143);void _main() size_t heap_size = 32*1024*1024; void *heap_base = mmap(NULL, heap_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); g_heap = tlsf_create_with_pool(heap_base, heap_size); void swap(int A, int i, in
3、t j) int temp; temp = Ai; Ai = Aj; Aj = temp;/*创建线程 退出线程 获取线程自己的ID 等待线程退出 int task_create(void *tos, void (*func)(void *pv), void *pv);int task_exit(int code_exit);int task_getid();int task_wait(int tid, int *pcode_exit);*/划线函数 void draw1(int y,int x1,int x2) line( x1, y, x2, y,RGB(87,166,163); void
4、 draw2(int y,int x1,int x2) line( x1, y, x2, y,RGB(221,237,252); void draw3(int y,int x1,int x2) line( x1, y, x2, y,RGB(215,171,196); void draw4(int y,int x1,int x2) line( x1, y, x2, y,RGB(184,98,127);/清除划线函数 void clean(int y,int x1,int x2) line( x1, y, x2, y,RGB(0,0,0); int msleep(const uint32_t ms
5、ec) struct timespec ts=0,1000000*msec; return nanosleep(&ts, NULL);/*排序算法*/ Insertion sort implementation 插入排序void inssort(int A, int n,int l) / Insertion Sort int i; int j; int t=5; for (i=0; i0) & (Aji; / Find the least value if (AjAlowindex) lowindex = j; clean(i*t,l,l+Ai); clean(lowindex*t,l,l+A
6、lowindex);/ Put it in place swap(A, i, lowindex); msleep(900); draw2(i*t,l,l+Ai); draw2(lowindex*t,l,l+Alowindex); /冒泡排序void bubsort(int A, int n,int l) / Bubble Sort i+) / Bubble up ith record for ( j=0; jn-1-i; j+) if (Aj+1Aj) clean(j*t,l,l+Aj); clean(j*t+t,l,l+Aj+1); swap(A, j, j+1); msleep(10);
7、draw3(j*t,l,l+Aj); draw3(j*t+t,l,l+Aj+1); /*创建线程函数*/void tsk_inssort(void *pv) printf(This is task inssort with tid=%drn, task_getid(); srand(time(NULL); / int x,y; int i,m; int array100; /进入图形模式 /init_graphic(0x143); /* x=g_graphic_dev.XResolution; y=g_graphic_dev.YResolution; /生成随机数i100;i+) m=rand
8、()%100+1; if(m0) m=0-m; draw4(i*5,0,0+m);/画出随机线条的长度 arrayi=m; inssort(array,100,0); / int exit_graphic();/退出图形模式 task_exit(0);/不能直接return,必须调用task_exitvoid tsk_selsort(void *pv)This is task selsort with tid=%drn int x; x=g_graphic_dev.XResolution; /y=g_graphic_dev.YResolution; draw1(i*5,x/3,x/3+m);
9、selsort(array,100,x/3); /int exit_graphic();void tsk_bubsort(void *pv)This is task bubsort with tid=%drn draw2(i*5,(2*x/3),(2*x/3)+m); bubsort(array,100,(2*x/3); * 第一个运行在用户模式的线程所执行的函数void main(void *pv)task #%d: Im the first user task(pv=0x%08x)!rn, task_getid(), pv); init_graphic(0x143); /TODO: You
10、r code goes here unsigned char *stack_inssort=(unsigned char *)malloc(1024*1024 );/unsigned int stack_size = 1024*1024;unsigned char *stack_selsort = (unsigned char *)malloc(1024*1024 );unsigned char *stack_bubsort= (unsigned char *)malloc(1024*1024 );int tid_inssort,tid_selsort,tid_bubsort;tid_inss
11、ort = task_create(stack_inssort+1024*1024, tsk_inssort, (void *)0);tid_selsort = task_create(stack_selsort+1024*1024, tsk_selsort, (void *)0);tid_bubsort = task_create(stack_bubsort+1024*1024, tsk_bubsort, (void *)0);task_wait(tid_inssort,0);free(stack_inssort);task_wait(tid_selsort, 0);free(stack_s
12、elsort);task_wait(tid_bubsort, 0);free(stack_bubsort);int exit_graphic(); time_t t = 0; t = time(NULL);%ld,t); while(1);最后结果: void draw4(int y,int x1,int x2) void draw5(int x,int y1,int y2) line( x, y1, x, y2,RGB(215,171,196); void draw6(int x,int y1,int y2) line( x, y1, x, y2,RGB(184,98,127); / x=g_graphic_dev.XResolution; /int x; /
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1