已积累部分函数接口

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 设计函数,三种方法赋值结构体
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
#include <string.h>

struct studentinfo
{
    char name[256];
    long number;
    unsigned int age;
    char sex[20];
};

int main(void)
{
    struct studentinfo data1 = {"xiaoming", 201900318038, 18, "man"};
    struct studentinfo data2 =
        {
            .sex = "women",
            .age = 20,
            .number = 201900318025,
            .name = {"xiaomei"}};
    struct studentinfo data3;
    data3.age = 18;
    data3.number = 165165124;
    strcpy(data3.name, "xiaobai");
    printf("data1 name is %s\n", data1.name);
    printf("data2 name is %s\n", data2.name);
    printf("data3 name is %s\n", data3.name);
}

*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 设计函数,判断大小端存储
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
union
{
   int a;
   char b;
} data;
int main()

{
   data.a = 0x12345678;
   if (0x12 == data.b)
       printf("Big-endian\n");
   else
       printf("little-endian\n");
   return 0;
}*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 判断静态局部变量和静态全局变量在函数中的改变。
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
static int y;
func1()
{
    static int x = 0;
    x++;
    printf("x=%d\n", x);
}
func2()
{
    y = 0;
    y++;
}
int main()
{
    int i = 0;
    for (i = 0; i < 2; i++)
    {
        func1();
        func2();
    }
    printf("y=%d\n", y);
    return;
}
*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 用宏定义交换两个变量数值
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
#define SWAP(x, y)     \
   {                    \
       (x) = (x) + (y); \
       (y) = (x) - (y); \
       (x) = (x) - (y); \
   }
int main()
{
   int a, b;
   printf("请输入两个数进行交换\n");
   scanf("%d%d", &a, &b);
   change(a, b);
   printf("%d,%d\n", a, b);
   return 0;
}
*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 用宏输入两个参数并返回较小的的一个
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
#define MIN(x, y) ((x) < (y)) ? (x) : (y)
int main()
{
    int a, b;
    printf("请输入两个数值进行比较\n");
    scanf("%d%d", &a, &b);
    printf("MIN number =%d\n", MIN(a, b));

    return 0;
}
*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 取10~100之间的素数
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
int main()
{
   int i, j, counter = 0;
   for (i = 11; i <= 100; i += 2)
   {
       for (j = 2; j < i; j++)
           if (i % j == 0)
               break;
       if (j >= i)
       {
           printf(" %d", i);
           counter++;
           if (counter % 5 == 0)
               printf("\n");
       }
   }
}
*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 计算一个字节有多少位为1
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
int main()
{
   unsigned char a;
   scanf("%c", &a);
   int number = 0;
   for (int i = 0; i < 8; i++)
   {
       if ((1 << i) & a)
           number++;
   }
   printf("%d\n", number);
   return 0;
}****/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 用宏定义写出交换两个变量数值的宏定义
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
#define SWAP(x, y)     \
   (x) = (x) + (y);   \
   (y) = ((x) - (y)); \
   (x) + ((x) - (y));*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 计算字符串中最大连续的字符个数
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
int func(char *str)
{

int cont = 1;
int max = 0;
int i = 0;
while (str[i] != '\0')
{
    if (str[i] == str[i + 1])
    {
        cont++;
    }
    else
    {
        max = (max > cont) ? (max) : (cont);
        cont = 1;
    }
    i++;
}
return max;
}
int main()
{
printf("max=%d\n", func("aaabbbbdddddddssccd"));
return 0;
}***/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 实现一个函数把str_src中的内容拷贝到str_dest中,并在主函数中调用。
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>

char *str_cpy(const char *str_src, char *str_dest)
{
   int i = 0;
   while (str_src[i] != '\0')
   {
       str_dest[i] = str_src[i];
       i++;
   }
   str_dest[i] = '\0';
   return str_dest;
}

int main()
{
   char buf[128] = {0};
   str_cpy("xiao ming", buf);
   printf("buf =%s\n", buf);
}*/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 设计一个函数,实现比较两个字符串是否相等,相等返回1,不相等返回0
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
int str_compare(const char *str1, const char *str2)
{

   while (*str1 == *str2)
   {
       if (*str1 == *str2 != '\0')

           return 1;
   }
   return 0;
}
int main()
{
   char a[5] = {"abcde"};
   char b[5] = {"abcde"};
   printf("%d\n", str_compare(a, b));
   return 0;
}***/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 用递归实现n!
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include <stdio.h>
int func(int n)
{
   if (1 == n)
       return 1;
   else
       return n * func(n - 1);
}
int main()
{
   int x;
   scanf("%d", &x);
   printf("n!=%d\n", func(x));
   return 0;
}***/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 从1到n,每数到m的倍数输出
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

#include<stdio.h>
void func(int m,int n)

{
    for (int i=1;i<=n;i++)
    {
        if (i%m!=0)
        {
            continue;
        }
        else
        {
            printf("%d\n",i);
        }
        i++;
    }
}

int main()
{
  int a=2,b=10;
func(a,b);
  return 0;
  }
*/

/*******************************************************************
*

  • file name: mystring.c
  • author : cecelmx@126.com
  • date : 2024/04/17
  • function : 该案例是掌握进行模块化编程思想,以及封装函数接口流程
  • note : None
  • CopyRight (c) 2023-2024 cecelmx@126.com All Right Reseverd
  • *****************************************************************/
#include "mystring.h"

/*******************************************************************
*
*	name	 :	CalSubStrMaxCnt
*	function :  计算一个字符串中最大的重复子串的字符的数量
*	argument :
*				@str:需要查找的字符串的地址
*
*	retval	 :  函数调用成功则返回最大子串的字符数量
*	author	 :  cecelmx@126.com
*	date	 :  2024/04/17
* 	note	 :  None
*
* **************************************************************
int CalSubStrMaxCnt(const char *str)
{
   if (NULL == str)
   {
       printf("argument is invaild\n");
       return -1;
   }

   int cnt = 1; // 计数器用于记录相同字符的数量,都不连续则返回值为1
   int max = 0;

   // 循环判断字符,当遇到'\0'则表达到达字符串末尾,此时可以终止循环
   while (*str != '\0')
   {

       // 判断当前的字符和下一个字符的ASCII是否相同
       if (*str == *(str + 1))
       {
           cnt++;
       }
       else
       {
           max = (max < cnt) ? cnt : max;
           cnt = 1;
       }

       str++;
   }

   // 当终止循环时,则把最大值返回
   return max;
}

/
/
****************************************************************
*

  • name : MyStrcpy
  • function : 实现字符串的拷贝(不调用库函数的情况下),是笔试题
  • argument :
  •  		@str_src :需要拷贝字符串的地址
    
  •  		@str_dest:待拷贝的目标空间的地址
    
  • retval : 函数调用成功则返回目标空间的地址
  • author : cecelmx@126.com
  • date : 2024/04/17
  • note : None
  • *****************************************************************/
char *MyStrcpy(const char *str_src, char *str_dest)
{
   // 循环的把str_src字符串的每个字符进行赋值,赋值到str_dest地址下
   while (*str_src != '\0')
   {
       // 如果没有到达字符串末尾,则把字符串的字符按照顺序依次赋值
       *str_dest = *str_src++;

       str_dest++;
   }

   // 包含了'\0'
   *str_dest = '\0';

   return str_dest;
}

/*******************************************************************
*

  • name : MyStrcmp
  • function : 实现字符串的比较(不调用库函数的情况下),是笔试题
  • argument :
  •  		@str1 :需要比较的第一个字符串的地址
    
  •  		@str2 :需要比较的第二个字符串的地址
    
  • retval : 两个字符串相同则返回0,如果不相同则返回1
  • author : cecelmx@126.com
  • date : 2024/04/17
  • note : None
  • *****************************************************************/
int MyStrcmp(const char *str1, const char *str2)
{

   // 循环的比较两个字符串的字符ASCII码是否相等
   while (*str1++ == *str2++)
   {
       if (*str1 == '\0' && *str2 == '\0')
       {
           return 1;
       }
   }

   return 0;
}

/*******************************************************************
*

  • name : StrRightShift
  • function : 实现把一个char组成的字符串循环右移n个。是笔试题
  • argument :
  •  		@str  :需要右移的字符串的地址
    
  •  		@n    :需要右移的位数
    
  • retval : 调用成功返回移位的字符串的首地址
  • author : cecelmx@126.com
  • date : 2024/04/17
  • note : 原来是"abcdefghi" 如果n=2,移位后应该是"hiabcdefg"
  • *****************************************************************/
char *StrRightShift(const char *str, int n)
{
   // 计算字符的实际长度
   int cnt = strlen(str);

   // printf("string cnt = %d\n",cnt);

   // 给字符串的字符申请内存
   char *p = (char *)malloc(cnt);

   // 把输入的字符串中的字符备份到堆内存
   strcpy(p, str);

   char temp = 0; // 存储要移位的字符

   // 循环进行字符的移位
   for (int i = 0; i < n; ++i)
   {
       temp = *(p + cnt - 1);
       printf("temp = %c\n", temp);

       for (int j = cnt - 2; j >= 0; j--)
       {
           *(p + j + 1) = *(p + j);
       }

       *p = temp;
   }

   // 输出移位后的字符串
   printf("string is %s\n", str);
   printf("string is %s\n", p);

   return p;
}

/********************************************************************
*

  • name : StrReverse
  • function : 实现将一个输入的字符串进行逆序 是笔试题
  • argument :
  •  		@str  :需要逆序的字符串的地址
    
  • retval : 调用成功返回逆序之后的字符串的首地址
  • author : cecelmx@126.com
  • date : 2024/04/17
  • note : 原来是"helloworld" 逆序后应该是"dlrowolleh"
  • *****************************************************************/
char *StrReverse(const char *str)
{

   char *p = (char *)malloc(128);

   int cnt = strlen(str);

   for (int i = 0; i < cnt; ++i)
   {
       *(p + i) = *((str + cnt - 1) - i);
   }

   return p;
}

/********************************************************************
*

  • name : StrReverse
  • function : 递归思想实现将一个输入的字符串的实际长度 是笔试题
  • argument :
  •  		@str  :需要逆序输出的字符串的地址
    
  • retval : 返回计算的字符数量,不包括'\0'
  • author : cecelmx@126.com
  • date : 2024/04/17
  • note : None
  • ******************************************************************/
int MyStrlen(const char *ptr)
{
   // 写好终止条件,遇到'\0'结束递归,'\0'不计算在内
   if ('\0' == *ptr)
   {
       return 0;
   }
   else
   {
       return MyStrlen(ptr + 1) + 1;
   }
}

/********************************************************************
*

  • name : StrReverse
  • function : 实现将一个输入的字符串进行逆序输出 是笔试题
  • argument :
  •   		@str  :需要逆序输出的字符串的地址
    
  • retval : None
  • author : cecelmx@126.com
  • date : 2024/04/17
  • note : 原来是"helloworld" 逆序后应该是"dlrowolleh"

void StrReversePrint(char *ptr)
{
    //写好终止条件,防止死循环出现,导致函数频繁调用出现栈溢出现象
    if ('\0' == *ptr)
    {
        return;
    }

    StrReversePrint(ptr+1);

    printf("%c",*ptr);

}**/

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/07
  • function : 输入一个证书输出其二进制位中1的个数
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd

// 2.统计二进制的1的个数

#include <stdio.h>
int count_num_of_1(int n)
{
   int count = 0;
   while (n)
   {
       if ((n % 2) == 1)
       {
           count++;
       }
       n /= 2;
   }
   return count;
}
int main()
{
   int num = 0, n;
   scanf("%d", &num);
   n = count_num_of_1(num);
   printf("%d\n", n);
}
***/
posted @ 2024-04-20 16:04  Zeratul$$$  阅读(44)  评论(0)    收藏  举报