65,211
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class stack
{
public:
stack(){m_top = 0;}
char top(){ return m_top ? data[m_top-1] : 0;}
void pop() { m_top ? --m_top : 0;}
void push(char c){data[m_top++] = c;}
private:
int m_top;
char data[64];
};
class input
{
public:
static void getline(char b[50]);
static void deblank(char b[50]);
};
void input::getline(char b[50])
{
cout <<"请输入一行字符串:" <<endl;
gets(b);
}
void input::deblank(char b[50])
{
char* curr = b;
while (*curr=*b++)
if (*curr != ' ') ++curr;
}
int main()
{
char a[50];
int j;
int top;
int last;
int OK = 1;
stack A;
input::getline(a);
input::deblank(a);
for (j = 0; j < strlen(a); ++j)
A.push(a[j]);
for (j = 0; j < strlen(a)/2; ++j)
{
if (A.top() != a[j])
{
OK = 0;
break;
}
A.pop();
}
if(OK) cout <<"该字符串是回文!!" <<endl;
else cout <<"该字符不是回文!!" <<endl;
} #include <iostream>
#include <string>
#include <cstdlib>
#include <stack>
using namespace std;
int check(string str)
{
stack<char> s;
int i;
for (i = 0; i < str.length(); ++i)
s.push(str[i]);
for (i = 0; i < str.length()/2; ++i)
{
char t = s.top();
s.pop();
if (t != str[i]) return 0;
}
return 1;
}
int main()
{
string a = "12321";
string b = "1231";
cout << check(a) << endl;
cout << check(b) << endl;
return 0;
}