要求
寫一算法额嘿,識(shí)別一次讀入的一個(gè)以@為結(jié)束符的字符序列是否為形如‘序列1&序列2’模式的字符序列荧呐。
其中序列1和序列2中都不包含字符‘&’且序列2時(shí)序列1的逆序列夺颤。
例如后豫,‘a(chǎn)+b&b+a’是屬于該模式的字符序列,而‘1+3&3-1’則不是怀愧。判斷輸入的字符串中‘&’前和‘&’后的部分是否為逆串侨颈,是則返回1,否則返回0芯义。
思路
=>STEP1:將字符串輸入到str1中哈垢,判斷字符是否有效。
判斷無效時(shí)服從三點(diǎn)要求:1.字符串內(nèi)有‘@’扛拨。2.字符串內(nèi)有‘&’耘分。3.‘@’在‘&’后面。
/*-------輸入字符串------*/
while(1){
cin>>s;
/*-------判斷合理性------*/
while(s[i]!=NULL){
if(s[i]=='&'){
yes_and=1;
before=i;}
else if(s[j]=='@'){
yes_end=1;
after=j;}
i++;
j++;
}
if(yes_and==1 && yes_end==1&& after>before){
yes=1;break;}else{
cout<<"錯(cuò)誤绑警。請(qǐng)輸入字符串求泰,以@鍵結(jié)束。"<<endl;
i=0;
j=0;}
}
i=0;
=>STEP2:將str1中字符串分割為單個(gè)字符计盒。將每個(gè)字符壓入棧a內(nèi)渴频。
/*-------輸入字符串------*/
cin>>s;
/*-------判斷字符串長度------*/
while(s[i]!='@'){
i++;
lenth=i;
}
/*-------創(chuàng)建棧a與棧b-----*/
arrStack<string> a(i);
arrStack<string> b(i);
a.clear();
b.clear();
i=0;
/*-------將字符串壓入棧a------*/
while(i!=lenth){
a.push(s[i]);
i++;
}
=>STEP3:使棧a中字符‘&’以上的內(nèi)容出棧并進(jìn)入b中。
此時(shí)b中元素為字符‘&’后元素的倒敘北启,即只需要判斷b中元素是否等于a中元素卜朗,即可判讀該字符串是否為逆串。
while(aMember[0]!='&'){
a.pop(aMember);
b.push(aMember[0]);
}
b.pop(bMember);
=>STEP4:判斷棧a元素是否等于棧b元素聊替。
/*-------判斷棧a與棧b是否相同------*/
while (alenth!=0&&blenth!=0){
if (aMember!=bMember){
testtrue=-1;break;}
else if(alenth!=blenth){
testtrue=-1;break;}
a.pop(aMember);
b.pop(bMember);
a.getlenth(alenth);
b.getlenth(blenth);
}
if(testtrue==-1){
out=1;
cout<<out<<"不相等"<<endl;}
else{
out=0;
cout<<out<<"相等"<<endl;}
}
程序與運(yùn)算結(jié)果
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
template <class T>
class arrStack{
private:
int mSize;
int top;
T *st;
public:
arrStack(int size){
mSize=size;
top=-1;
st=new T[mSize];
}
~arrStack(){
delete[]st;
}
void clear(){
top=-1;
}
void push(char &item){
if(top == mSize - 1){
cout << "stack is full, cannot push element!" << endl;
return;
}
else{
st[++top]=item;
}
}
void pop(string &m){
if(top == -1){
cout << "empty" << endl;
}
else{
m=st[top--];
}
}
void getop(string &t){
if(top ==-1){
cout<<"empty!"<<endl;
}
else{
t=st[top];
cout<<"獲得棧頂元素:"<<t<<endl;
}
}
void getlenth(int &m){
m=top+1;
}
};
void clear(){
int i=0;
int j=0;
int before=0;
int after=0;
int lenth;//lenth
int testtrue=0;
int alenth=2;
int blenth=2;
int n=0;
int yes_and=0;
int yes_end=0;
int yes;
int out;
string s;
string aMember;
string bMember;
}
void start(){
int lenth;//lenth
int i=0;
int j=0;
int before=0;
int after=0;
int testtrue=0;
int alenth=2;
int blenth=2;
int n=0;
int yes_and=0;
int yes_end=0;
int yes;
int out;
string s;
string aMember;
string bMember;
/*-------輸入字符串------*/
while(1){
cin>>s;
while(s[i]!=NULL){
if(s[i]=='&'){
yes_and=1;
before=i;}
else if(s[j]=='@'){
yes_end=1;
after=j;}
i++;
j++;
}
if(yes_and==1 && yes_end==1&& after>before){
yes=1;break;}else{
cout<<"錯(cuò)誤。請(qǐng)輸入字符串培廓,以@鍵結(jié)束耍铜。"<<endl;
i=0;
j=0;}
}
i=0;
/*-------判斷字符串長度------*/
while(s[i]!='@'){
i++;
lenth=i;
}
/*-------創(chuàng)建棧a與棧b-----*/
arrStack<string> a(i);
arrStack<string> b(i);
a.clear();
b.clear();
i=0;
/*-------將字符串壓入棧a------*/
while(i!=lenth){
a.push(s[i]);
i++;
}
/*-------將a中元素壓入棧b憔儿,以符號(hào)&結(jié)束------*/
while(aMember[0]!='&'){
a.pop(aMember);
b.push(aMember[0]);
}
b.pop(bMember);
/*-------判斷棧a與棧b是否相同------*/
while (alenth!=0&&blenth!=0){
if (aMember!=bMember){
testtrue=-1;break;}
else if(alenth!=blenth){
testtrue=-1;break;}
a.pop(aMember);
b.pop(bMember);
a.getlenth(alenth);
b.getlenth(blenth);
}
if(testtrue==-1){
out=1;
cout<<out<<"不相等"<<endl;}
else{
out=0;
cout<<out<<"相等"<<endl;}
}
int main(){
int n=0;
cout<<"歡迎使用残家。"<<endl;
while(1){
clear();
cout<<"請(qǐng)輸入字符串,以@鍵結(jié)束暂殖。"<<endl;
start();
}
system("pause");
return 0;
}
總結(jié)
程序運(yùn)行結(jié)果比較好。
最重要的問題時(shí)覺得自己寫的程序有點(diǎn)邋遢当纱,不夠簡潔呛每。
比如說判斷輸入字符是否合理時(shí),我定義了after坡氯、before晨横、i、j箫柳、yes_end手形,yes_and等等變量∶趸校或許會(huì)有更簡潔的方法库糠,以后我會(huì)學(xué)習(xí)優(yōu)化。