zoukankan      html  css  js  c++  java
  • (数据结构)回文游戏(顺读与逆读字符串一样(不含空格))读入字符串; 去掉空格(原串); 压入栈; 原串字符与出栈字符依次比较; 若不等,非回文; 若直到栈空都相等,回文。

    # include<iostream>//软件工程 信 1605 -3 20163432 张运涛
    using namespace std;
    # define OK 1;//定义宏观变量
    # define ERROR 0;
    # define OVERFLOW -2
    # define MASIZE 100//定义栈的最大容量
    typedef struct{
    char *base;
    char * top;
    int stacksize;
    }SqStack;
    int InitStack(SqStack &S)//初始化栈
    {
    S.base=new char[MASIZE];
    if(!S.base)exit(OVERFLOW);//查看栈是否初始化成功
    S.top=S.base;
    S.stacksize=MASIZE;
    return OK;}
    int Push (SqStack &S,char e)//元素入栈
    {
    if(S.top-S.base==S.stacksize)return ERROR;
    *S.top++=e;
    return OK;
    }
    int Pop (SqStack &S,char &e)//栈顶元素按顺序出栈
    {

    e=*--S.top;



    cout<<"字符元素为: "<<e<<endl;

    return e;

    }
    int main()
    {
    SqStack S1;
    if(InitStack( S1))

    cout<<"栈初始化成功"<<endl;
    cout<<"请输入入栈元素的个数"<<endl;
    int n;
    cin>>n;char c[100];//定义一个存放字符的数组
    cout<<"请输入第一个字符"<<endl;
    for(int i=0;i<n;i++)
    {char e1; cin>>e1;

    c[i]=e1;
    if((Push(S1,e1))&&c[i]!='')//去除字符串中的空格

    {cout<<"第"<<i+1<<"个元素入栈成功"<<endl;

    }
    else{ cout<<"入栈失败"<<endl;}}


    int b=1;
    for(int i=0;i<n;i++)
    {char e;
    if(Pop(S1,e)!=c[i])//判断字符串是否为回文


    b=-1;


    }
    if(b==-1)
    cout<<"不是回文串"<<endl;
    else cout<<"是回文串"<<endl;

    }

     

  • 相关阅读:
    oracle 调优3
    ifconfig找不到命令的帖子 精选
    执行计划中各字段各模块描述
    oracle统计信息
    oracle中 rownum与rowid的理
    触发器
    开园第一天
    Asp.net生成htm静态文件的两种途径
    避免刷新页面,自动跳回到页面顶部的办法
    ASP.NET二级域名站点共享Session状态
  • 原文地址:https://www.cnblogs.com/zyt-bg/p/7711337.html
Copyright © 2011-2022 走看看