第八天 0716

1,使用函数封装实现对一个数组的冒泡排序

int maopao(int a[], int n)
{
  int i, j;
  for(i = 0; i < n-1; i++){
    for(j = 0; j < n-1-i; j++){
      if(a[j] > a[j+1]){
        a[j] ^= a[j+1];
        a[j+1] ^= a[j];
        a[j] ^= a[j+1];
      }
    }
  }
}
int ptr_arr(int a[], int n)
{
  int i = 0;
  for(i; i < n; i++)
  printf("%d ", a[i]);
  printf("\n");
}

 

2,使用函数实现strlen的功能

int mystrlen(char *s)
{
  int i = 0;
  while(s[i])
    i++;
  return i;
}
int main()
{
  char a[100] = {0};
  gets(a);
  printf("%d \n", mystrlen(a));
}

 

3使用函数实现strcpy的功能

void mystrcpy(char *s, char *s1)
{
  while(*s1){
    *(s++) = *(s1++);
  }
  *s = '\0';
}
int main()
{
  char a[100] = {0}, b[100] = {0};
  gets(b);
  mystrcpy(a, b);
  puts(a);
}

 

char *mystrcpy(char *s, const char *s2)
{
  char *tem = s;
  while(*s2){
    *(s++) = *(s2++);
  }
  *s = '\0';
  return tem;
}
int main()
{
  char a[100] = {"hello"};
  char b[100] = {"world"};
  char *p = mystrcpy(a,b);
  puts(a);
  puts(p);
  printf("%p %p\n", a, p);
}

 

4,设计一个函数求阶乘的和

 

int jc(int n)
{
  int i = 1, j = 1, sum = 1, tem = 0;
  for(i; i <= n; i++){
    for(j = 1; j <= i; j++)
      sum *= j;
    tem += sum;
    sum = 1;
  }
  return tem;
}
int main()
{
  int n;
  scanf("%d", &n);
  int m = jc(n);
  printf("%d \n", m);
}

上一篇:我爱Linux


下一篇:20、去除重复字母