爱站网长尾关键词挖掘工具福利片,新开传奇网站迷失版,临沂大企业网站,建站快车的应用场景学习函数主要学习的就是函数的声明、定义和调用#xff0c;下面请看两个例子#xff0c;来帮助我们学习函数#xff1a; 题目一#xff1a; 编写一个函数iswithin()#xff0c;它接受两个参数#xff0c;一个是字符#xff0c;另一个是字符串指针。其功能是如果字符在字… 学习函数主要学习的就是函数的声明、定义和调用下面请看两个例子来帮助我们学习函数 题目一 编写一个函数iswithin()它接受两个参数一个是字符另一个是字符串指针。其功能是如果字符在字符串中。就返回1 真如果字符不在字符串中就返回0假。在一个使用循环语句为这个函数提供舒服的完整程序中进行测试。 代码如下 [cpp] view plaincopy #include stdio.h int iswithin(char p,char *q) { while(*q) { if(p *q) return 1; else q; } return 0; } int main(int argc, char *argv[]) { int m; char p,*q; p *argv[1]; q argv[2]; m iswithin(p,q); if(m 1) printf(\%c\ is in the string!\n,p); else printf(\%c\ is not in the string!\n,p); return 0; } 执行结果如下 [cpp] view plaincopy fsubuntu:~/qiang/hanshu$ ./hanshu2 h hello h is in the string! fsubuntu:~/qiang/hanshu$ ./hanshu2 h world h is not in the string! fsubuntu:~/qiang/hanshu$ 注意函数传参的方式。 题目二、 以下函数的功能是用递归的方法计算 x 的 n 阶勒让德多相式的值。已有调用语句p(n,x)编写函数实现功能。 代码如下 [cpp] view plaincopy #include stdio.h int p(int n,int x) { int m; if(n 0) return 0; else if(n 1) return x; else { m ((2*n - 1)*x*p(n - 1,x) - (n - 1)*p(n - 2,x))/n; return m; } } int main(int argc, const char *argv[]) { int x, n; int q; printf(Please input x and n:\n); scanf(%d%d,x,n); q p(n,x); printf(p %d\n,q); return 0; } 执行结果如下 [cpp] view plaincopy fsubuntu:~/qiang/hanshu$ ./hanshu1 Please input x and n: 2 1 p 2 fsubuntu:~/qiang/hanshu$ ./hanshu1 Please input x and n: 2 5 p 194 fsubuntu:~/qiang/hanshu$