设计 9个任务函数task0()-task8()只输出一句话:如task0()输出“task0 is called!”;设计一个调度函数schedule()输入一个字符串如"012345"然后返回一个函数指针数组和字符串的长度作为执行函数 execute()的参数进行调度任务函数。主函数只有调度函数。
#define M 1000 typedef void (*ptask)(); void task0(){ printf("task0 is called!\n"); } void task1(){ printf("task1 is called!\n"); } void task2(){ printf("task2 is called!\n"); } void task3(){ printf("task3 is called!\n"); } void task4(){ printf("task4 is called!\n"); } void task5(){ printf("task5 is called!\n"); } void task6(){ printf("task6 is called!\n"); } void task7(){ printf("task7 is called!\n"); } void task8(){ printf("task7 is called!\n"); } void execute(ptask* fun , int len){ int i= 0 ; for( i = 0 ; i < len ; i++){ ptask pfun= fun[i]; pfun(); } } void schedule(){ ptask fun[M]; int len ,i,temp; char str[M]; printf("please input a string\n"); while( scanf("%s",str),str){ len= strlen(str); for( i = 0 ; i < len ; i++){ temp = str[i]-'0'; switch(temp){ case 0:fun[i] = task0;break; case 1:fun[i] = task1;break; case 2:fun[i] = task2;break; case 3:fun[i] = task3;break; case 4:fun[i] = task4;break; case 5:fun[i] = task5;break; case 6:fun[i] = task6;break; case 7:fun[i] = task7;break; case 8:fun[i] = task8;break; } } execute(fun , len); } } int main(){ schedule(); return 0; }函数指针终极练习
最新推荐文章于 2023-01-03 19:48:58 发布