鱼C论坛

 找回密码
 立即注册
查看: 4001|回复: 4

利用栈检验算术表达式中括号是否配对,输入错误的表达式时输出是正确的

[复制链接]
发表于 2014-4-20 19:33:54 | 显示全部楼层 |阅读模式
13鱼币
请帮忙看下哪里有问题
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define StackSize 100
typedef char DataType;
typedef struct
{        DataType data[StackSize];
        int top;
}SeqStack;

void main()
{
        SeqStack S;
        char exp[80];
        char ch;
        bool status;
        int i;
        void InitStack(SeqStack *S);                                                               
        bool StackEmpty(SeqStack S);
        void Push(SeqStack *S, DataType newelem);
        DataType Pop(SeqStack *S);
        void PrintStack(SeqStack S);
        cout<<"  输入要检验括号匹配的表达式:"<<endl;
        cin>>exp;
        InitStack(&S);
        status=true;
        i=0;
        ch=exp[i];
        i++;
        while ((ch!='\0') && status)        //回车换行符
        {
                switch (ch)
                {
                case '(':
               
                        Push(&S,ch);
                        break;
                case ')':
                        if (Pop(&S)!='(')
                                status=false;
                        break;
               
               
                }
                ch=exp[i];
                i++;
        }
        if (StackEmpty(S) && status)
                cout<<"表达式正确!"<<endl;
        else
        {
                cout<<"表达式不正确!"<<endl;
                cout<<"第"<<i<<"字符开始不正确!"<<endl;
        }
        system("pause");
}
void InitStack(SeqStack *S)
{
        S->top=-1;
}
bool StackEmpty(SeqStack S)
{
        if (S.top==-1)
                return true;
        else
                return false;
}
void Push(SeqStack *S, DataType newelem)
{
        S->top++;
        S->data[S->top]=newelem;
}
DataType Pop(SeqStack *S)
{
        S->top--;
        return S->data[S->top+1];
}

最佳答案

查看完整内容

为什么要用链栈?徒增麻烦 既然用了链栈为什么直接将指针++(->next) 用链栈得用双向吧。不然怎么POP(除非从头索引,找到next的下一个为top的位置) 还有就是没申请空间你怎么入栈 我感觉连基本的栈操作都是不对的。还没看main
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-20 19:33:55 | 显示全部楼层

为什么要用链栈?徒增麻烦
既然用了链栈为什么直接将指针++(->next)
用链栈得用双向吧。不然怎么POP(除非从头索引,找到next的下一个为top的位置)
还有就是没申请空间你怎么入栈
我感觉连基本的栈操作都是不对的。还没看main
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-4-20 21:17:42 | 显示全部楼层
这个程序编译没问题啊!

评分

参与人数 1鱼币 +3 收起 理由
qw30716 + 3 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-4-20 22:50:41 | 显示全部楼层
青玄 发表于 2014-4-20 21:17
这个程序编译没问题啊!

我发错了程序,不是这个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-4-20 22:52:36 | 显示全部楼层
青玄 发表于 2014-4-20 21:17
这个程序编译没问题啊!

#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include <iostream>
using namespace std;
typedef struct Node
{        int data;
struct Node *next;

}NODE , *PNODE;
typedef struct Stack
{
        PNODE ptop;
        PNODE pbottom;

}STACK,*PSTACK;
void InitStack(PSTACK  PS);                                                               
        bool StackEmpty(PSTACK  PS);
        void Push(PSTACK  PS, char newelem);
        char Pop(PSTACK  PS);
        void PrintStack(STACK  S);
void main()
{
        STACK  S;
        char exp[80];
        char ch;
        bool status;
        int i;
       
        cout<<"  输入要检验括号匹配的表达式:"<<endl;
        cin>>exp;
        InitStack(&S);
        status=true;
        i=0;
        ch=exp;
        i++;
        while ((ch!='\0') && status)        //回车换行符
        {
                switch (ch)
                {
                case '(':
               
                        Push(&S,ch);
                        break;
                case ')':
                        if (Pop(&S)!='(')
                                status=false;
                        break;
               
               
                }
                ch=exp;
                i++;
        }
        if (StackEmpty(&S) && status)
                cout<<"表达式正确!"<<endl;
        else
        {
                cout<<"表达式不正确!"<<endl;
                cout<<"第"<<i<<"字符开始不正确!"<<endl;
        }
        system("pause");
}
void InitStack(PSTACK  S)
{
        S->ptop=NULL;
}
bool StackEmpty(PSTACK  S)
{
        if (S->ptop==NULL)
                return true;
        else
                return false;
}
void Push(PSTACK  S, char newelem)
{
        S->ptop++;
        S->ptop->data=newelem;
}
char Pop(PSTACK  S)
{
        S->ptop--;
        return S->pbottom;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-14 02:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表