C语言:判断字符串是否为回文,-函数fun将单向链表结点数据域为偶数的值累加起来。-用函数指针指向要调用的函数,并进行调用。

时间:2023-03-09 17:17:41
C语言:判断字符串是否为回文,-函数fun将单向链表结点数据域为偶数的值累加起来。-用函数指针指向要调用的函数,并进行调用。

//函数fun功能:用函数指针指向要调用的函数,并进行调用。

 #include  <stdio.h>
double f1(double x)
{ return x*x; }
double f2(double x, double y)
{ return x*y; }
double fun(double a, double b)
{
/**********found**********/
double (*f)();//定义一个指针函数。
double r1, r2;
/**********found**********/
f = f1;
r1 = f(a);
/**********found**********/
f = f2;
r2 = (*f)(a, b);
return r1 + r2;
}
void main()
{ double x1=, x2=, r;
r = fun(x1, x2);
printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);
}

//建立一个带头节点的单向链表,并用随机函数为各个结点赋值,函数fun将单向链表结点数据域为偶数的值累加起来。

 #include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct aa
{ int data;
struct aa *next;
} NODE;
int fun (NODE *h)
{ int sum=;
NODE *p;
p=h->next;//首结点
/*************found**************/
while(p!=NULL)//注意
{ if(p->data%==)
sum+=p->data;
/*************found**************/
p=p->next;
}
return sum;
}
NODE *creatlink(int n)
{
NODE *h,*p,*s;
int i;
h=p=(NODE*)malloc(sizeof(NODE));
for(i=;i<n;i++)
{
s=(NODE*)malloc(sizeof(NODE));
s->data=rand()%;
s->next=p->next;
p->next=s;
p=p->next;
}
p->next=NULL;
return h;
}
void outlink(NODE *h)
{ NODE *p;
p=h->next;
printf("\n\n The LIST :\n\n HEAD");
while(p)
{ printf("->%d",p->data);
p=p->next;}
printf("\n");
}
void main()
{ NODE *head; int sum;
system("CLS");
head=creatlink();
outlink(head);
sum=fun(head);
printf("\nSUM=%d",sum);
}

//函数功能:判断字符串是否为回文,若是返回1,主函数输出YES。回文是指顺读和倒读都一样的字符串。

 #include <stdio.h>
#define N 80
int fun(char *str)
{
char *p = str;
char *q = str + strlen(str) - ;
while (*p == *q)
{
p++; q--;
if (p >= q)
{
return ;
}
}
return ;
} void main()
{
char s[N];
FILE *out;
char *test[]={"","","","abcdCBA"};
int i;
printf("Enter a string : ");
gets(s);
printf("\n\n");
puts(s);
if(fun(s))
printf("YES\n");
else
printf("NO\n");
/************************************/
out=fopen("out.dat","w");
for(i=;i<;i++)
if(fun(test[i]))
fprintf(out,"YES\n");
else
fprintf(out,"NO\n");
fclose(out);
/************************************/
}