//函数fun功能:用函数指针指向要调用的函数,并进行调用。
1 #include <stdio.h> 2 double f1(double x) 3 { return x*x; } 4 double f2(double x, double y) 5 { return x*y; } 6 double fun(double a, double b) 7 { 8 /**********found**********/ 9 double (*f)();//定义一个指针函数。 10 double r1, r2; 11 /**********found**********/ 12 f = f1; 13 r1 = f(a); 14 /**********found**********/ 15 f = f2; 16 r2 = (*f)(a, b); 17 return r1 + r2; 18 } 19 void main() 20 { double x1=5, x2=3, r; 21 r = fun(x1, x2); 22 printf(" x1=%f, x2=%f, x1*x1+x1*x2=%f ",x1, x2, r); 23 }
//建立一个带头节点的单向链表,并用随机函数为各个结点赋值,函数fun将单向链表结点数据域为偶数的值累加起来。
1 #include <stdio.h> 2 #include <conio.h> 3 #include <stdlib.h> 4 typedef struct aa 5 { int data; 6 struct aa *next; 7 } NODE; 8 int fun (NODE *h) 9 { int sum=0; 10 NODE *p; 11 p=h->next;//首结点 12 /*************found**************/ 13 while(p!=NULL)//注意 14 { if(p->data%2==0) 15 sum+=p->data; 16 /*************found**************/ 17 p=p->next; 18 } 19 return sum; 20 } 21 NODE *creatlink(int n) 22 { 23 NODE *h,*p,*s; 24 int i; 25 h=p=(NODE*)malloc(sizeof(NODE)); 26 for(i=1;i<n;i++) 27 { 28 s=(NODE*)malloc(sizeof(NODE)); 29 s->data=rand()%16; 30 s->next=p->next; 31 p->next=s; 32 p=p->next; 33 } 34 p->next=NULL; 35 return h; 36 } 37 void outlink(NODE *h) 38 { NODE *p; 39 p=h->next; 40 printf(" The LIST : HEAD"); 41 while(p) 42 { printf("->%d",p->data); 43 p=p->next;} 44 printf(" "); 45 } 46 void main() 47 { NODE *head; int sum; 48 system("CLS"); 49 head=creatlink(10); 50 outlink(head); 51 sum=fun(head); 52 printf(" SUM=%d",sum); 53 }
//函数功能:判断字符串是否为回文,若是返回1,主函数输出YES。回文是指顺读和倒读都一样的字符串。
1 #include <stdio.h> 2 #define N 80 3 int fun(char *str) 4 { 5 char *p = str; 6 char *q = str + strlen(str) - 1; 7 while (*p == *q) 8 { 9 p++; q--; 10 if (p >= q) 11 { 12 return 1; 13 } 14 } 15 return 0; 16 } 17 18 void main() 19 { 20 char s[N]; 21 FILE *out; 22 char *test[]={"1234321","123421","123321","abcdCBA"}; 23 int i; 24 printf("Enter a string : "); 25 gets(s); 26 printf(" "); 27 puts(s); 28 if(fun(s)) 29 printf("YES "); 30 else 31 printf("NO "); 32 /************************************/ 33 out=fopen("out.dat","w"); 34 for(i=0;i<4;i++) 35 if(fun(test[i])) 36 fprintf(out,"YES "); 37 else 38 fprintf(out,"NO "); 39 fclose(out); 40 /************************************/ 41 }