国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

通過“回文字算法”復習C++語言

2020-05-23 14:00:03
字體:
來源:轉載
供稿:網友

 一、什么是回文字

給定一個字符串,從前往后讀和從后往前讀,字符串序列不變。例如,河北省農村信用社的客服電話是“96369”,無論從后往前讀,還是從前后往后讀,各個字符出現的位置不變。

二、功能實現

(一)、給定一個字符串,判斷該字符串是否是回文字。

(二)、給定一個任意字符串,判斷是否可以轉換為回文字,如果可以轉換為回文字,給出具體的算法。

三、C++語言實現版本(JAVA語言版本后續(xù)實現)

(一)頭文件 (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++語言,希望對大家有所幫助!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 襄垣县| 沙洋县| 高唐县| 名山县| 永福县| 南靖县| 孝义市| 江安县| 德兴市| 共和县| 甘谷县| 铁岭县| 宜阳县| 新兴县| 开原市| 砚山县| 米脂县| 喀喇| 班玛县| 扶沟县| 山东省| 廊坊市| 左贡县| 广平县| 高雄县| 兰溪市| 漳平市| 苍梧县| 六盘水市| 夏津县| 镇平县| 百色市| 卫辉市| 普陀区| 柳河县| 苗栗市| 华容县| 呼图壁县| 永城市| 张家界市| 祁门县|