算法库应用-顺序串(串比较)

学习贺利坚老师博客

数据结构例程——串的顺序存储应用_使用顺序串存储身份证号-CSDN博客

本人详细解析博客

串的顺序存储结构应用_(1)假设串采用顺序串存储,设计一个算法程序,按顺序比较两个串s和t的大小。请-CSDN博客

版本日志

V1.0: 利用顺序串, 进行简单的判断比较, 也算是简单应用

功能函数

传入两个顺序串, 然后先比较共同长度部分, 一般如果判断出就可以, 如果共同长度仍然相同,则比较谁更长就可以

/**************************************************
函数名: String_comparison
功  能: 比较两个顺序串,那个更大
参  数: (1)Sequential_string compare_one: 第一个串
        (2)Sequential_string compare_two :第二个串
返回值: int comparative_result: 0--两个串相等,1--第一个串大,-1--第二个串大
**************************************************/
int String_comparison(Sequential_string compare_one,Sequential_string compare_two)
{
    int counter;
    int Common_length;
    int comparative_result = 0;
    //得到共同长度
    if(compare_one.length < compare_two.length)
    {
        Common_length = compare_one.length;
    }
    else
    {
        Common_length = compare_two.length;
    }
    //在共同长度内逐字符比较
    for(counter = 0; counter < Common_length; counter++)
    {
        if(compare_one.Sequential_string_data[counter] > compare_two.Sequential_string_data[counter])
        {
            comparative_result = 1;
        }
        else
        if(compare_one.Sequential_string_data[counter] < compare_two.Sequential_string_data[counter])
        {
            comparative_result = -1;
        }
    }
    if(comparative_result == 0)
    {
        if(compare_one.length == compare_two.length)
        {
            comparative_result = 0;
        }
        else
        if(compare_one.length < compare_two.length)
        {
            comparative_result = -1;
        }
        else
        if(compare_one.length > compare_two.length && comparative_result == 0)
        {
            comparative_result = 1;
        }
    }
    return comparative_result;
}

调用的库文件

头文件

Sequential_string.h
#ifndef _SEQUENTIAL_STRING_H_INCLUDE
#define _SEQUENTIAL_STRING_H_INCLUDE

#include <stdio.h>
#define MaxSize 100 //最多字符个数

//顺序串数据结构
typedef struct
{
    char Sequential_string_data[MaxSize];//数组串数据
    int length;                          //实际串长
}Sequential_string;

//(1)将一个字符串数组赋值给顺序串
void Assignment_Sequential_string(Sequential_string &New_String, char Assign_array[]);
//(2) 复制一个串,到另一个串
void Copy_Sequential_String(Sequential_string &accept_string, Sequential_string copy_string);
//(3)判断两个串是否相等
bool Equal_Sequential_String(Sequential_string judge_string1, Sequential_string judge_string2);
//(4)求顺序串串长
int Length_Sequential_String(Sequential_string measure_string);
//(5)串连接
Sequential_string Connect_Sequential_String(Sequential_string link1, Sequential_string link2);
//(6)求子串(从begin_loation开始的number个字符)
Sequential_string Get_Sequential_Substring(Sequential_string substring, int begin_loation, int number);
//(7)插入串(从从begin_loation开始插入字符串,然后组合成新的串)
Sequential_string Insert_Sequential_String(Sequential_string old_string, int begin_loation,Sequential_string insert_string);
//(8)删除串(从begin 开始的number个字符)
Sequential_string Delete_Sequential_String(Sequential_string old_string, int begin_loation,int number);
//(9)串替换(从begin 开始的number个字符)
Sequential_string Replace_Sequential_String(Sequential_string old_string, int begin_loation,int number,Sequential_string new_string);
//(10)输出展示串
void Display_Sequential_String(Sequential_string show_String);
#endif

库函数文件

Sequential_string.cpp
#include "Sequential_string.h"

/**************************************************
(1)函数名: Assignment_Sequential_string
功  能: 将一个字符串数组赋值给顺序串
参  数: (1)Sequential_string &New_String:创建的新串
        (2)char Assign_array[]: 原始字符串数组
注  意:  顺序数组,结尾加入'\0'
返回值: 无
**************************************************/
void Assignment_Sequential_string(Sequential_string &New_String, char Assign_array[])
{
    int counter;
    for(counter = 0; Assign_array[counter] != '\0'; counter++)
    {
        New_String.Sequential_string_data[counter] = Assign_array[counter];
    }
    New_String.Sequential_string_data[counter] = '\0';
    New_String.length = counter;    //更新串最大位序
}

/**************************************************
(2)函数名: Copy_Sequential_String
功  能: 复制一个串,到另一个串
参  数: (1)Sequential_string &accept_string: 复制成的串
        (2)Sequential_string copy_string:要复制的串
注  意:  复制成的串,传回的是地址,所以不用传回参数
返回值: 无
**************************************************/
void Copy_Sequential_String(Sequential_string &accept_string, Sequential_string copy_string)
{
    int counter;
    for(counter = 0; counter < copy_string.length;counter++)
    {
        accept_string.Sequential_string_data[counter] = copy_string.Sequential_string_data[counter];
    }
    accept_string.Sequential_string_data[counter] = '\0';
    accept_string.length = copy_string.length;
}
/**************************************************
(3)函数名: Equal_Sequential_String
功  能: 判断两个串是否相等
参  数: (1)Sequential_string judge_string1:第一个串
        (2)Sequential_string judge_string2:第二个串
返回值: bool?是否相等,true:false
**************************************************/
bool Equal_Sequential_String(Sequential_string judge_string1, Sequential_string judge_string2)
{
    bool same = true;
    int counter;
    if(judge_string1.length != judge_string2.length)
    {
        same = false;
    }
    else
    {
        for(counter = 0; counter < judge_string1.length;counter++)
        {
            if(judge_string1.Sequential_string_data[counter] != judge_string2.Sequential_string_data[counter])
            {
                same = false;
                break;
            }
        }
    }
    return same;

}

/**************************************************
(4)函数名: Length_Sequential_String
功  能: 求顺序串串长
参  数: Sequential_string measure_string:要进行测量的串
返回值: int:顺序串长度信息
**************************************************/
int Length_Sequential_String(Sequential_string measure_string)
{
    return measure_string.length;
}

/**************************************************
(5)函数名: Connect_Sequential_String
功  能: 把两个串连接成一个串
参  数: Sequential_string link1, Sequential_string link2:两个要链接的串
返回值: 返回Sequential_string Connection_string: 链接成的串
**************************************************/
Sequential_string Connect_Sequential_String(Sequential_string link1, Sequential_string link2)
{
    Sequential_string Connection_string;
    int counter;
    Connection_string.length = link1.length + link2.length;
    //将第一个串加入链接的串
    for(counter = 0; counter < link1.length; counter++)
    {
        Connection_string.Sequential_string_data[counter] = link1.Sequential_string_data[counter];
    }
    //将第二个串加入链接的串
    for(counter = 0; counter < link2.length; counter++)
    {
        Connection_string.Sequential_string_data[link1.length+counter] = link2.Sequential_string_data[counter];
    }
    Connection_string.Sequential_string_data[link1.length+counter] = '\0';
    return Connection_string;
}

/**************************************************
(6)函数名: Get_Sequential_Substring
功  能: 求子串(从begin_loation开始的number个字符)
参  数: (1)Sequential_string mother_String:母串
        (2)int begin_loation:开始分割子串的位置
        (3)int number:子串的数量
返回值: Sequential_string son_String:得到的子串
**************************************************/
Sequential_string Get_Sequential_Substring(Sequential_string mother_String, int begin_loation, int number)
{
    Sequential_string son_String;
    int counter;
    son_String.length = 0;
    if(begin_loation <= 0 || begin_loation > mother_String.length || number < 0 || begin_loation+number-1>mother_String.length)
    {
        //错误:分割的子字符串的位置错误。
        printf("\nError<6>:The position of the divided substring is wrong.\n");
        return son_String; //    参数不正确返回空串
    }
    for(counter = begin_loation-1; counter < begin_loation+number-1; counter++)
    {
        son_String.Sequential_string_data[counter-begin_loation+1] = mother_String.Sequential_string_data[counter];
    }
    son_String.Sequential_string_data[counter-begin_loation+1] = '\0';
    son_String.length = number;
    return son_String;
}

/**************************************************
(7)函数名: Insert_Sequential_String
功  能: 插入串(从从begin_loation开始插入字符串,然后组合成新的串)
参  数: (1)Sequential_string old_string:在原始串的基础上插入
        (2)int begin_loation: 插入的位置
        (3)Sequential_string insert_string:插入的新串
思  路:  在原有串的基础上,割开一个口子,放上新串,然后组合成新串
返回值: Sequential_string form_string:组合成的新串
**************************************************/
Sequential_string Insert_Sequential_String(Sequential_string old_string, int begin_loation,Sequential_string insert_string)
{
    int counter;
    Sequential_string form_string;
    form_string.length = 0;
    //参数不正确, 返回空串
    if(begin_loation <= 0 || begin_loation > old_string.length+1)
    {
        //错误:插入位置错误
        printf("\nError<7>: wrong insertion position.\n");
        return form_string;
    }
    for(counter = 0; counter < begin_loation-1;counter++)
    {
        form_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];
    }

    for(counter = 0; counter < insert_string.length;counter++)
    {
        form_string.Sequential_string_data[begin_loation-1+counter] = insert_string.Sequential_string_data[counter];
    }

    for(counter = begin_loation-1; counter<old_string.length;counter++)
    {
        form_string.Sequential_string_data[insert_string.length+counter] = old_string.Sequential_string_data[counter];
    }
    form_string.Sequential_string_data[insert_string.length+counter] = '\0';
    form_string.length = old_string.length + insert_string.length;
    return form_string;

}
/**************************************************
(8)函数名: Delete_Sequential_String
功  能: 删除串(从begin 开始的number个字符)
参  数: (1)Sequential_string old_string:在原有串的基础上删除
        (2)int begin_loation: 开始删除的位置(从逻辑1开始)
        (3)int number:删除的数量
注  意:  要判断删除的位置和数量是否正确
返回值:Sequential_string new_string:删除完后的新串
**************************************************/
Sequential_string Delete_Sequential_String(Sequential_string old_string, int begin_loation,int number)
{
    int counter;//定义计数器
    Sequential_string new_string;
    new_string.length = 0;
    //合法性判断(begin_loation理应从1开始到leng长度)
    if(begin_loation <= 0 || begin_loation > old_string.length || (begin_loation+number-1) > old_string.length)
    {
        //错误:删除的位置或数量错误。
        printf("Error<8>: Wrong location or quantity of deletion.");
        return new_string;//返回空串
    }
    //择出删除位置之前的串
    for(counter = 0; counter < begin_loation-1;counter++)
    {
        new_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];
    }
    //择出删除位置之后的串
    for(counter = begin_loation+number-1; counter < old_string.length; counter++)
    {
        new_string.Sequential_string_data[counter-number] = old_string.Sequential_string_data[counter];
    }
    new_string.Sequential_string_data[counter-number] = '\0';
    new_string.length = old_string.length - number;
    return new_string;
}

/**************************************************
(9)函数名: Replace_Sequential_String
功  能: 串替换(从begin 开始的number个字符)
参  数: (1)Sequential_string old_string:原始串
        (2)int begin_loation:开始替换的位置
        (3)int number:替换的字符个数
        (4)Sequential_string replace_string:要替换成的字符串
思  路: 锁定old_string从begin_loation开始的number个字符,
        然后开始剪切建立新串,
        ①把begin_loation之前的字符加入新串,
        ②要替换成的串加入,
        ③锁定后的字符加入
        ④组合成新串,返回传出
注  意:  最后加'\0'
返回值: Sequential_string new_string:替换后,产生的新串
**************************************************/
Sequential_string Replace_Sequential_String(Sequential_string old_string, int begin_loation,int number,Sequential_string replace_string)
{
    int counter;
    Sequential_string new_string;
    new_string.length = 0;
    //合法性判断
    if(begin_loation <= 0 || begin_loation > old_string.length || begin_loation+number-1>old_string.length)
    {
        //错误:要替换位置出现错误
        printf("\nError<9>: There is an error in the position to be replaced.\n");
        return new_string;//返回空串
    }
    //开始复制剪切
    for(counter = 0; counter < begin_loation-1; counter++)
    {
        new_string.Sequential_string_data[counter] = old_string.Sequential_string_data[counter];
    }
    //加入要替换的串
    for(counter = 0; counter < replace_string.length; counter++)
    {
        new_string.Sequential_string_data[begin_loation-1+counter] = replace_string.Sequential_string_data[counter];
    }
    //被替换位置,后面剩余的串
    for(counter = begin_loation+number-1; counter < old_string.length; counter++)
    {
        new_string.Sequential_string_data[counter-number+replace_string.length] = old_string.Sequential_string_data[counter];
    }
    new_string.Sequential_string_data[counter-number+replace_string.length] = '\0';
    new_string.length = old_string.length - number + replace_string.length;
    return new_string;
}



/**************************************************
(10)函数名: Display_Sequential_String
功  能: 输出展示串
参  数: Sequential_string show_String:要输出展示的串
注  意: 字符串后续可以换成自定义类型
返回值: 无
**************************************************/
void Display_Sequential_String(Sequential_string show_String)
{
    int counter;
    if(show_String.length > 0)
    {
        for(counter = 0; counter < show_String.length; counter++)
        {
            printf("%c", show_String.Sequential_string_data[counter]);
        }
        printf("\n");
    }
}

主函数main.cpp(包含功能函数)

main.cpp
#include <stdio.h>
#include "Sequential_string.h"

/**************************************************
函数名: String_comparison
功  能: 比较两个顺序串,那个更大
参  数: (1)Sequential_string compare_one: 第一个串
        (2)Sequential_string compare_two :第二个串
返回值: int comparative_result: 0--两个串相等,1--第一个串大,-1--第二个串大
**************************************************/
int String_comparison(Sequential_string compare_one,Sequential_string compare_two)
{
    int counter;
    int Common_length;
    int comparative_result = 0;
    //得到共同长度
    if(compare_one.length < compare_two.length)
    {
        Common_length = compare_one.length;
    }
    else
    {
        Common_length = compare_two.length;
    }
    //在共同长度内逐字符比较
    for(counter = 0; counter < Common_length; counter++)
    {
        if(compare_one.Sequential_string_data[counter] > compare_two.Sequential_string_data[counter])
        {
            comparative_result = 1;
        }
        else
        if(compare_one.Sequential_string_data[counter] < compare_two.Sequential_string_data[counter])
        {
            comparative_result = -1;
        }
    }
    if(comparative_result == 0)
    {
        if(compare_one.length == compare_two.length)
        {
            comparative_result = 0;
        }
        else
        if(compare_one.length < compare_two.length)
        {
            comparative_result = -1;
        }
        else
        if(compare_one.length > compare_two.length && comparative_result == 0)
        {
            comparative_result = 1;
        }
    }
    return comparative_result;
}

int main()
{
    Sequential_string test1,test2;
    char test1_Array[] = {'a','b','c','d','e','f','g','\0'};
    char test2_Array[] = {'a','b','\0'};
    char test3_Array[] = {'a','b','c','d','\0'};
    char test4_Array[] = {'a','b','c','d','\0'};
    Assignment_Sequential_string(test1,test1_Array);
    Assignment_Sequential_string(test2,test2_Array);
    printf("\ntest1:\n");
    Display_Sequential_String(test1);
    printf("\ntest2:\n");
    Display_Sequential_String(test2);
    printf("test1与test2比较, %d\n",String_comparison(test1,test2));

    printf("\n第二次赋值:\n");
    Assignment_Sequential_string(test1,test3_Array);
    Assignment_Sequential_string(test2,test4_Array);
    printf("\ntest1:\n");
    Display_Sequential_String(test1);
    printf("\ntest2:\n");
    Display_Sequential_String(test2);
    printf("test1与test2比较, %d\n",String_comparison(test1,test2));

    printf("\n第三次赋值:\n");
    Assignment_Sequential_string(test1,test2_Array);
    Assignment_Sequential_string(test2,test3_Array);
    printf("\ntest1:\n");
    Display_Sequential_String(test1);
    printf("\ntest2:\n");
    Display_Sequential_String(test2);
    printf("test1与test2比较, %d\n",String_comparison(test1,test2));
    return 0;
}



测试结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/773062.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

JavaScript中闭包的理解

闭包&#xff08;Closure&#xff09;概念&#xff1a;一个函数对周围状态的引用捆绑在一起&#xff0c;内层函数中访问到其外层函数的作用域。简单来说;闭包内层函数引用外层函数的变量&#xff0c;如下图&#xff1a; 外层在使用一个函数包裹住闭包是对变量的保护&#xff0c…

Linux--V4L2摄像头驱动框架及UVC浅析

一、前言 对于一个usb摄像头&#xff0c;它的内核驱动源码位于/drivers/media/usb/uvc/ 核心层&#xff1a;V4L2_dev.c文件 硬件相关层&#xff1a; uvc_driver.c文件 本篇记录基于对6.8.8.8内核下vivid-core.c文件&#xff08;虚拟视频驱动程序&#xff09;的分析&#xff…

【数据库】仓库管理数据库(练习样例)

某连锁超市需要设计实现一个仓库管理系统&#xff0c;要求每个仓库可以有多名仓库管理员&#xff0c;每个仓库管理员只负责管理一个仓库&#xff0c;同时每个仓库都配备了一名仓库主管&#xff1b;不同的仓库存放的是不同类型的货品&#xff0c;每种货品只存放在固定的仓库中&a…

Os-hackNos

下载地址 https://download.vulnhub.com/hacknos/Os-hackNos-1.ova 环境配置如果出现&#xff0c;扫描不到IP的情况&#xff0c;可以尝试vulnhub靶机检测不到IP地址解决办法_vulnhub靶机扫描不到ip-CSDN博客 信息收集 确定靶机地址&#xff1a; 探测到存活主机192.168.111.…

modelscope可控细节的长文档摘要

modelscope可控细节的长文档摘要尝试 本文的想法来自今年OpenAI cookbook的一篇实践&#xff1a;summarizing_long_documents&#xff0c;目标是演示如何以可控的细节程度总结大型文档。 如果我们想让大语言模型总结一份长文档&#xff08;例如 10k 或更多tokens&#xff09;&…

【MySQL】 NDB 集群概述

MySQL NDB&#xff08;Network Database&#xff09;是MySQL的一个存储引擎&#xff0c;也称为NDB Cluster存储引擎。它主要用于构建高可用性、高可扩展性和高性能的分布式数据库集群。NDB Cluster是MySQL的一个特殊版本&#xff0c;专门设计用于处理大规模的分布式数据存储和处…

【MySQL】MySQL 9.0悄悄的来了

MySQL 9.0.0 中的变化 MySQL 9.0 中的新功能 JavaScript 存储程序 MySQL 企业版现在支持用 JavaScript 编写的存储程序&#xff0c;例如使用 CREATE FUNCTION下面显示的语句和 JavaScript 代码创建的这个简单示例&#xff1a; CREATE FUNCTION gcd(a INT, b INT) RETURNS …

SpringBoot-第一天学习

SpringBoot介绍-约定大于配置 SpringBoot是在Spring4.0基础上开发的&#xff0c;不是替代Spring的解决方案&#xff0c;而是和Spring框架结合并进一步简化Spring搭建和开发过程的。 如何简化&#xff1f;就是通过提供默认配置等方式让我们更容易&#xff0c;集成了大量常用的…

泛微开发修炼之旅--29用计划任务定时发送邮件提醒

文章链接&#xff1a;29用计划任务定时发送邮件提醒

嵌入式Linux系统编程 — 6.7 实时信号

目录 1 什么是实时信号 2 sigqueue函数 3 sigpending()函数 1 什么是实时信号 等待信号集只是一个掩码&#xff0c;它并不追踪信号的发生次数。这意味着&#xff0c;如果相同的信号在被阻塞的状态下多次产生&#xff0c;它只会在信号集中被记录一次&#xff0c;并且在信号集…

Django文档简化版——Django快速入门——创建一个基本的投票应用程序

Django快速入门——创建一个基本的投票应用程序 准备工作1、创建虚拟环境2、安装django 1、请求和响应&#xff08;1&#xff09;创建项目&#xff08;2&#xff09;用于开发的简易服务器&#xff08;3&#xff09;创建投票应用&#xff08;4&#xff09;编写第一个视图1、编写…

FreeRTOS的任务间通信

文章目录 4 FreeRTOS任务间通信4.1 队列4.1.1 队列的使用4.1.2 队列的创建&#xff0c;删除&#xff0c;复位4.1.3 队列的发送&#xff0c;接收&#xff0c;查询 4.2 邮箱&#xff08;mailbox&#xff09;4.2.1 任务中读写邮箱4.2.2 中断中读写邮箱 4.3 队列集4.3.1 队列集的创…

linux19:程序替换

一&#xff1a;最简单的看看程序替换是什么样的&#xff08;单个进程版&#xff09; 1 #include<stdio.h>2 #include<unistd.h>3 #include<stdlib.h>4 int main()5 {6 printf("Before : I am a process , myPid:%d,myPPid:%d\n",getpid(),getpp…

【Ubuntu】详细说说Parallels DeskTop安装和使用Ubuntu系统

希望文章能给到你启发和灵感~ 如果觉得文章对你有帮助的话,点赞 + 关注+ 收藏 支持一下博主吧~ 阅读指南 开篇说明一、基础环境说明1.1 硬件环境1.2 软件环境二、Ubuntu系统的使用2.1 系统的下载2.2 系统的安装2.3 安装桌面版(可选)2.3.1 安装/更新apt2.3.2 安装桌面版2.3…

算法day02 回文 罗马数字转整数

回文 搞错了String类型的indexOf方法&#xff0c;理解成获取对应下标的值&#xff0c;实际上是在找对应值的下标。 4ms 耗时最少的方法尽量不会去调用jdk提供的方法&#xff0c;而是直接使用对应的数学逻辑关系来处理&#xff0c; 甚至用 代替equals方法。 罗马数字转整数 考…

Simulink中示波器连续运行的方法

1.在Simulink中,经常要使用到示波器,默认示波器是定时运行的,只能观察到一小部分运行的波形;实际调试过程中,经常要连续运行,因此,需要设置示波器为连续运行模式,下面将介绍示波器连续运行的方法。 打开Simulink仿真软件,找到仿真设置按钮,点击设置: 2.将其停止时间…

Oracle 解决4031错误

一、问题描述 什么是4031错误和4031错误产生的原因: 简单一个句话概括: 由于服务器一直在执行大量的硬解析,导致Oracle 的shared pool Free空间碎片过多,大的chunk不足, 当又一条复杂的sql语句要硬解析时, 缺少1个足够大的Free chunk, 通常就会报4031错误. 二、解决方法 临…

JVM原理(十五):JVM虚拟机静态分配与动态分配

1. 分派 本节讲解的分派调用过程将会揭示多态性特征的一-些最基本的体现&#xff0c;如“重载”和“重写”在Java虚拟机之中是如何实现的。 1.1. 静态分派 案例&#xff1a; 我们先来看一段代码: Human mannew Man(); 我们把上面代码中的“Human"称为变量的“静态类型…

9 redis,memcached,nginx网络组件

课程目标: 1.网络模块要处理哪些事情 2.reactor是怎么处理这些事情的 3.reactor怎么封装 4.网络模块与业务逻辑的关系 5.怎么优化reactor? io函数 函数调用 都有两个作用:io检测 是否就绪 io操作 1. int clientfd = accept(listenfd, &addr, &len); 检测 全连接队列…

Contact Form 7表单获取提交用户IP及URL等信息

有时候&#xff0c;您可能需要了解Contact Form 7表单提交后的更多的信息&#xff0c;而不仅仅是通过联系人表单字段获取用户的联系信息。例如&#xff0c;需要知道用户是哪个国家&#xff08;通过获取IP&#xff09;&#xff0c;了解用户使用的设备&#xff08;手机还是电脑&a…