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

news/2024/7/8 3:46:52 标签: 算法, 数据结构, c++

学习贺利坚老师博客

数据结构例程——串的顺序存储应用_使用顺序串存储身份证号-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.niftyadmin.cn/n/5536324.html

相关文章

离线安装arm架构Firefox

离线安装Firefox浏览器及其插件在ARM架构的设备上&#xff08;如树莓派、部分Android设备或其他采用ARM处理器的Linux系统&#xff09;可能需要一些特殊步骤&#xff0c;因为默认情况下&#xff0c;大多数浏览器和插件都是为x86架构设计的。对于ARM架构&#xff0c;你需要找到特…

spring6框架解析(by尚硅谷)

文章目录 spring61. 一些基本的概念、优势2. 入门案例实现maven聚合工程创建步骤分析实现过程 3. IoC&#xff08;Inversion of Control&#xff09;基于xml的bean环境搭建获取bean获取接口创建实现类依赖注入 setter注入 和 构造器注入原生方式的setter注入原生方式的构造器注…

vue2 webpack使用optimization.splitChunks分包,实现按需引入,进行首屏加载优化

optimization.splitChunks的具体功能和配置信息可以去网上自行查阅。 这边简单讲一下他的使用场景、作用、如何使用&#xff1a; 1、没用使用splitChunks进行分包之前&#xff0c;所有模块都揉在一个文件里&#xff0c;那么当这个文件足够大、网速又一般的时候&#xff0c;首…

大数据开发中的数据生命周期管理

上班越久&#xff0c;发现有些数据一直放在那里&#xff0c;根本没有流动&#xff0c;完全没有发挥价值&#xff0c;数据是有生命周期的&#xff0c;而且生命周期管理得好&#xff0c;工作就会更轻松。 目录 引言数据创建示例代码 数据存储示例代码 数据使用示例代码 数据维护示…

SQL Server特性

一、创建表 在sql server中使用create table来创建新表。 create table Customers( id int primary key identity(1,1), name varchar(5) ) 该表名为Customers其中包含了2个字段&#xff0c;分别为id&#xff08;主键&#xff09;以及name。 1、数据类型 整数类型&#xff…

SQL注入工具Sqlmap

一、什么是Sqlmap Sqlmap是一款开源的渗透测试工具&#xff0c;主要用于自动化检测和利用SQL注入漏洞&#xff0c;并能实现数据库服务器的接管。Sqlmap提供了多种功能和选项&#xff0c;包括数据库指纹识别、数据读取、文件系统访问以及通过带外数据连接执行系统命令等。它支持…

mongodb-数据备份和恢复

mongodb-database-tools mongodb-database-tools是MongoDB官方提供的一组命令行工具&#xff0c;用于执行各种与MongoDB数据库相关的操作&#xff0c;包括备份、恢复、导入、导出、查询和转换数据等。这些工具可帮助开发人员和管理员轻松地管理MongoDB数据库。 以下是一些常用…

【总线】AXI4第六课时:寻址选项深入解析

大家好,欢迎来到今天的总线学习时间!如果你对电子设计、特别是FPGA和SoC设计感兴趣&#xff0c;那你绝对不能错过我们今天的主角——AXI4总线。作为ARM公司AMBA总线家族中的佼佼者&#xff0c;AXI4以其高性能和高度可扩展性&#xff0c;成为了现代电子系统中不可或缺的通信桥梁…