通过“回文字算法”复习C++语言
更新时间:2020年4月25日 17:34 点击:2202
一、什么是回文字
给定一个字符串,从前往后读和从后往前读,字符串序列不变。例如,河北省农村信用社的客服电话是“96369”,无论从后往前读,还是从前后往后读,各个字符出现的位置不变。
二、功能实现
(一)、给定一个字符串,判断该字符串是否是回文字。
(二)、给定一个任意字符串,判断是否可以转换为回文字,如果可以转换为回文字,给出具体的算法。
三、C++语言实现版本(JAVA语言版本后续实现)
(一)头文件 (BackText.h)
/* * BackText.h * * Created on: 2016年9月30日 * Author: gaodianhua */ #include <string> #include <cstring> #include <map> #ifndef BACKTEXT_H_ #define BACKTEXT_H_ using namespace std; class BackText { string text; map<char,int> mapBychar; int checksum; public: BackText(); BackText(char str[]); BackText(string text); virtual ~BackText(); bool isBackText(); void print() const; void countDiffCh(); void convert(char * dest); }; #endif /* BACKTEXT_H_ */
(二)类的实现
/* * BackText.cpp * * Created on: 2016年9月30日 * Author: gaodianhua */ #include "BackText.h" #include <iostream> #include <string> #include <iterator> #include <cstring> #include <cstdlib> #include <map> using namespace std; BackText::BackText() { } BackText::~BackText() { this->checksum=0; } BackText::BackText(char *str){ this->text=str; this->checksum=0; } BackText::BackText(string str){ this->text=str; this->checksum=0; } bool BackText::isBackText(){ string::iterator it1,it2; it1=text.begin(); it2=text.end()-1; for(;it1<=it2;it1++,it2--){ if(*it1!=*it2) return false; } return true; } void BackText::print() const{ cout<<this->text<<endl; } void BackText::countDiffCh(){ string::iterator it1,it2; string temp; temp.clear(); int index=0; for(it1=text.begin();it1<text.end();it1++){ if( strchr(temp.data(),*it1)==NULL ){ temp.insert(index,1,*it1); index++; } } for( it2=temp.begin();it2<temp.end();it2++){ int count=0; for(it1=text.begin();it1<text.end();it1++){ if(*it1==*it2){ count++; checksum++; } } mapBychar.insert(pair<char,int>(*it2,count)); } map<char,int>::iterator m; for(m=mapBychar.begin( );m != mapBychar.end( ); m++ ) cout <<m->first<<" "<<m->second<<endl; } void BackText::convert(char* dest){ if(isBackText()){ strcpy(dest,text.data()); return; } int cnt=0; map<char,int>::iterator m; for(m=mapBychar.begin( );m != mapBychar.end( ); m++ ){ if(m->second%2!=0){ cnt++; } } if(cnt>1){ cout<<"该字符串不能被转化为回文字"<<endl; return; } cout<<"开始转换..."<<endl; int begIndex=0; int endIndex=checksum-1; bool oddflag=0; char oddchar; for(m=mapBychar.begin( );m != mapBychar.end( ); m++ ){ if( checksum % 2 == 0 ){ for( int i=0; i< m->second/2; i++ ){ dest[begIndex++]=m->first; dest[endIndex--]=m->first; } }else{ if(m->second % 2 == 0){ for( int i=0; i< m->second/2 ; i++ ){ dest[begIndex++]=m->first; dest[endIndex--]=m->first; } }else{ oddchar=m->first; oddflag=true; continue; } } } if(oddflag){ map<char,int>::iterator it; it=mapBychar.find(oddchar); if(it==mapBychar.end()){ cout<<"do not find "<< oddchar <<endl; return; } for( int i=0; i< it->second; i++ ){ dest[begIndex++]=it->first; } } }
(三)main函数
/* * main.cpp * * Created on: 2016年9月30日 * Author: gaodianhua */ #include <iostream> #include "BackText.h" #include <cstdlib> #include <string> using namespace std; int main(){ string text; text.clear(); cout<<"请输入字符串:"; cin>>text; BackText bt=BackText(text); bt.print(); if( !bt.isBackText() ) cout<<"不是回文字符串"<<endl; bt.countDiffCh(); char dest[100]; memset(dest,0x0,sizeof(dest)); bt.convert(dest); cout<<dest<<endl; return 0; }
以上所述是小编给大家分享的通过“回文字算法”复习C++语言,希望对大家有所帮助!
上一篇: socket多人聊天程序C语言版(二)
下一篇: socket多人聊天程序C语言版(一)
相关文章
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 作者:Sabine 【导读】本文介绍了C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序 冒泡排序 using System; namespace BubbleSorter { public class Bubb...2020-06-25
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
- 这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
- 这篇文章主要是介绍了.net C# 实现任意List的全组合算法实现代码,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15