首页 > 其他分享 >code

code

时间:2024-04-20 16:11:41浏览次数:14  
标签:char code name int 2024 str com

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

  • file name: main.c
  • author : [email protected]
  • date : 2024/04/07
  • function : 设计函数,三种方法赋值结构体
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 设计函数,判断大小端存储
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 判断静态局部变量和静态全局变量在函数中的改变。
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 用宏定义交换两个变量数值
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 用宏输入两个参数并返回较小的的一个
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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;

}
*/

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


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 : [email protected]
  • date : 2024/04/07
  • function : 计算一个字节有多少位为1
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 用宏定义写出交换两个变量数值的宏定义
  • note : None
  • CopyRight (c) 2023-2024 [email protected] All Right Reseverd

include <stdio.h>

define SWAP(x, y) \

(x) = (x) + (y);   \
(y) = ((x) - (y)); \
(x) + ((x) - (y));*/

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

  • file name: main.c
  • author : [email protected]
  • date : 2024/04/07
  • function : 计算字符串中最大连续的字符个数
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 实现一个函数把str_src中的内容拷贝到str_dest中,并在主函数中调用。
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/07
  • function : 设计一个函数,实现比较两个字符串是否相等,相等返回1,不相等返回0
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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;
}***/

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


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 : [email protected]
  • date : 2024/04/07
  • function : 从1到n,每数到m的倍数输出
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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 : [email protected]
  • date : 2024/04/17
  • function : 该案例是掌握进行模块化编程思想,以及封装函数接口流程
  • note : None
  • CopyRight (c) 2023-2024 [email protected] All Right Reseverd

include "mystring.h"

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

  • name : CalSubStrMaxCnt
  • function : 计算一个字符串中最大的重复子串的字符的数量
  • argument :
  •  		@str:需要查找的字符串的地址
    
  • retval : 函数调用成功则返回最大子串的字符数量
  • author : [email protected]
  • 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 : [email protected]

  • 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 : [email protected]

  • 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 : [email protected]

  • 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 : [email protected]

  • 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 : [email protected]
  • 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 : [email protected]
  • 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 : [email protected]
  • date : 2024/04/07
  • function : 输入一个证书输出其二进制位中1的个数
  • note : None
  • CopyRight (c) 2023-2024 [email protected] 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);
}
***/

标签:char,code,name,int,2024,str,com
From: https://www.cnblogs.com/zeratul/p/18147813

相关文章

  • LeetCode三则
    63.不同路径II一个机器人位于一个mxn网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?网格中的障碍物和空......
  • leetcode回文数
    给你一个整数x,如果x是一个回文整数,返回true;否则,返回false。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121是回文,而123不是。示例1:输入:x=121输出:true示例2:输入:x=-121输出:false解释:从左向右读,为-121。从右向左读,为121-。因此......
  • Anaconda Navigator启动卡在loading applications界面(同时莫名启动了VSCODE)
    0.问题参考:anacondanavigator启动时一直卡在loadingapplications页面我的问题和网上普遍的不太一样,首先我能找到conda_api.py,但是不是在D:\anaconda\Lib\site-packages\anaconda_navigator\api文件夹下,而是在...\anaconda3\pkgs\anaconda-project-0.11.1-py311haa9553......
  • vsCode无法连接服务器问题解决及思考
    背景早上刚打开电脑,准备开始一天的工作。但是发现VSCode无法连接上我的虚拟机了,导致无法工作了,这让我十分头疼。最终花了将近一天的时间将问题解决,但是其中的过程走了不少弯路,浪费了不少时间,也进行了反思。我们作为开发人员,应该要用软件思维去理解这款产品,帮助我们去思考问题。......
  • Codeforces 1824C LuoTianyi and XOR-Tree
    考虑到肯定如果能在这个节点让子树的值尽量相同肯定更好,这样子不会与上面的操作相冲突。于是有个\(\text{DP}\)的思路。记\(f_{u,i}\)为\(u\)子树内叶子节点的值都变为\(i\)的最小代价。这个有一个很好的性质,就是\(\maxf_{u,i}-\minf_{u,i}=1\)。这是因为考......
  • leedcode-二进制手表
    自己写的,调用了combinations函数:fromitertoolsimportcombinationsfromtypingimportListclassSolution:defreadBinaryWatch(self,turnedOn:int)->List[str]:#可以表示小时的LED灯,对应的值分别是1,2,4,8hour_list=[1,2,4,8]#......
  • Codeforces Round 932 (Div. 2)题解(c、d)
    CodeforcesRound932(Div.2)C.MessengerinMAC题目大意给定一些\(a_i\)\(和b_i\),选出尽量多的\(a_i和b_i\),使得\(\sum_{i=1}^ka_{p_i}+\sum_{i=1}^{k-1}\left|b_{p_i}-b_{p_{i+1}}\right|\)小于给定的\(l\)。题目解析由于题目没有要求\(\{p\}\)是升序排列的序列,因此......
  • Codes 重新定义 SaaS 模式的研发项目管理平台开源版 4.5.3 发布
    一:简介   Codes重新定义SaaS模式= 云端认证+程序及数据本地安装+不限功能+30人免费    Codes 是一个 高效、简洁、轻量的一站式研发项目管理平台。包含需求管理,任务管理,测试管理,缺陷管理,自动化测试,cicd 等功能;Codes帮助企业加速融合研发、测试、运......
  • [code notes] ecpg precompiler 1
    Thisnotewillintroducetheworkflowofparse.ploftheecpgprecompiler.Runtheprecompiler:perlparse.pl.../../../backend/parser/gram.yworkflowloadecpg.addonsintoanmemoryhashtable.Thekeyiscomposedofstringliteralsfromaproduction......
  • vscode使用PasteImage插入图片
    vscode使用PasteImage插入图片需求在vscode中写Markdown文件,经常需要插入图片,使用插件PasteImage进行简单配置后,就可以方便插入图片并自动存放到相应路径的文件夹中安装及配置安装从扩展商店搜索PasteImage并安装即可配置vscode设置中搜索PasteImage,找到PasteImage:......