9.28
作者:互联网
今天编写了用栈实现回文的代码.
代码如下:
#include<iostream>
using namespace std;
const int Maxsize = 100;
class stact {
public:
char data[Maxsize];
int top = 0;
void push(char b) {
data[++top] = b;
}
char pop() {
return data[top--];
}
};
int main() {
stact S_n1;
int n = 0;
char ch_str[100];
char ch_s[100];
char ch_n;
cin >> n;
getchar();
for (int i = 0; i < n; i++) {
ch_n = getchar();
if (ch_n == ' ') {
cout << "入栈不成功";
return 0;
}
else {
S_n1.push(ch_n);
ch_s[i] = ch_n;
}
}
for (int i = 0; i < n; i++) {
ch_str[i] = S_n1.pop();
}
int count = 0;
for (int i = 0; i < n; i++) {
if (ch_str[i] == ch_s[i]) count ++;
}
if (count == n) {
cout << "此字符串是回文串";
}
else cout << "此字符串不是回文串";
}
开始时,我想用flag来判断是否为回文,但是后来发现先对会很麻烦,也出现了bug,最后我决定用计数器来判断是否重复。
标签:ch,cout,int,top,9.28,char,++ 来源: https://www.cnblogs.com/crimsonyu/p/15350645.html