回文判断

 //回文是指正读反读均相同的字符序列,如abba abdda(将一半字符入栈
#ifndef PCH_H
#define PCH_H
#include<stdio.h>
#include<cstdlib>
#include<malloc.h>
#include<math.h>
#include<iostream>
#include<string>
constexpr auto MAXSIZE = 100;
constexpr auto ERROR = 0;
constexpr auto OK = 1;
typedef int Status;
typedef struct
{
    char *base;
    char *top;
    int stacksize;
}SqStack;
Status InitStack(SqStack &S);
Status Push(SqStack &S,char e);
Status Pop(SqStack &S, char &e);
Status EmptyStack(SqStack S);
void TraverStack(SqStack S);
void Palindrome(int n);//必须给出字符串长度
#endif //PCH_H
---------------------------------------------------------
// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的

#include "pch.h"

// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。

Status InitStack(SqStack & S)
{
    S.base = (char *)malloc(sizeof(char));
    if (!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = MAXSIZE;//栈最大容量
    return OK;
}
Status EmptyStack(SqStack S)
{
    if (S.base == S.top) return OK;
    return ERROR;
}
void TraverStack(SqStack S)
{
    if (EmptyStack(S)) std::cout<<"null stack!!";
    else
    {
        while (S.base!=S.top)
        {
            std::cout << *--S.top;
        }
    }
}
void Palindrome(int n)
{
    SqStack S;
    char e;
    char a;
    bool flag=true;
    InitStack(S);
    std::cout << "put in "<<n<<" char:";
    for(int i=0;i<n;i++)
    {
        std::cin >> e;
        if(i<n/2)
        Push(S, e);
        if (i >= n / 2 && n % 2 == 0)
        {
            Pop(S, a);
                if (a != e)
                {
                    flag = false;
                    break;
                }
        }
        else if (i > n / 2 && n % 2 != 0)
        {
            Pop(S, a);
            if (a != e)
            {
                flag = false;
                break;
            }
        }
    }
    if (flag) std::cout << "IS Palindrome";
    else std::cout << "NOT Palindrome";
}
Status Push(SqStack & S, char e)
{
    if (S.top-S.base==MAXSIZE) return ERROR;//栈满
    *S.top++= e;
    return OK;
}

Status Pop(SqStack & S, char & e)
{
    if (EmptyStack(S))return ERROR;
    e = *--S.top;
    return OK;
}
-----------------------------------------------------------
// 030202.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>

int main()
{
    int n = 4;
    Palindrome(n);
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值