斐波那契数列(函数,递归方法)
代码
#include<stdio.h>
int fbnq(int num);
int main(int argc, char const *argv[])
{
int num,i= 0;
printf("请输入你要打印多少个\n");
scanf("%d",&num);
for ( i = 1; i <= num; i++)
{
printf("%3d",fbnq(i));
}
return 0;
}
int fbnq(int num)
{
int i=num;
int count= 0;
if (num <=2 )
{
count =1;
return count;
}
else
{
count = fbnq(num -2)+fbnq(num-1);
return count;
}
num--;
}
效果
三个函数族封装(除strlen)
代码
#include<stdio.h>
#include<string.h>
int mystrlen(char *s);
int mystrcmp(char *s1,char *s2);
char *mystrcpy(char *dest,char *src);
char *mystrcat(char *dest,char *src);
int main(int argc, char const *argv[])
{
char arr1[10] = {0};
char arr2[10] = {0};
char *arr3[20] = {0};
char *arr4[20] = {0};
int len = 0, cmp = 0;
printf("请输入第一个字符串\n");
gets(arr1);
printf("请输入第二个字符串\n");
gets(arr2);
len =mystrlen(arr1);
cmp = mystrcmp(arr1,arr2);
printf("---------------长度-----------------\n");
printf("字符串的长度是:%d\n",len);
printf("---------------大小-----------------\n");
printf("字符串的大小是:%d\n",cmp);
printf("---------------拼接-----------------\n");
mystrcat(arr1,arr2);
printf("---------------拷贝-----------------\n");
mystrcpy(arr1,arr2);
return 0;
}
int mystrcmp(char *s1,char *s2)
{
int count =0;
char *p1 = s1;
char *p2 = s2;
while (*p2 ||*p1 )
{
if (*p1 >*p2)
{
count= *p1 -*p2;
break;
}
else if (*p1 <*p2)
{
count= *p1 -*p2;
break;
}
p1++;
p2++;
}
return count;
}
int mystrlen(char *s)
{
char *p = s;
int count = 0;
while (*p)
{
count++;
p++;
}
return count;
}
char * mystrcat(char *dest,char *src)
{
char *p1 = dest ;
char *p2 = src;
p1=p1+mystrlen(dest);
while (*p2)
{
*p1= *p2;
p1 ++;
p2++;
}
*p1 = *p2;
puts(dest);
return dest;
}
char * mystrcpy(char *dest,char *src)
{
char *p1 = dest;
char *p2 = src;
while (*p2)
{
*p1= *p2;
p1 ++;
p2++;
}
*p1= *p2;
puts(dest);
puts(src);
return dest;
}