鱼C论坛

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

[已解决]C语言 数据结构括号匹配 主函数错误 求助!!

[复制链接]
发表于 2020-10-31 10:41:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 beannaeb 于 2020-10-31 18:36 编辑
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef char ElemType;

typedef struct
{        ElemType data[MaxSize];
        int top; 
 }SqStack;
 
//初始化栈运算算法
void InitStack(SqStack &st)
{        
        st.top=-1; 
 } 
 
//销毁栈运算算法 
void DestroyStack(SqStack st)
{
 }
 
//进栈运算算法
int Push(SqStack &st,ElemType x)
{        if(st.top==MaxSize-1)                        //栈满上溢出返回0 
                return 0;
        else
        {        st.top++;
                st.data[st.top]=x;
                return 1;                                        //成功进栈返回1 
        }
 } 

//出栈运算算法
int Pop(SqStack &st,ElemType &x)         //x为引用型参数
{        if(st.top==-1)                                        //栈空返回0 
                return 0;
        else
        {        x=st.data[st.top];
                st.top--;                                        //成功出栈返回1 
                return 1;
        }
 } 
 
//取栈顶元素运算算法
int GetTop(SqStack st,ElemType &x)        //x为引用型参数
{        if(st.top==-1)                                        //栈空返回0 
                return 0;
        else
        {        x=st.data[st.top];
                return 1;                                        //成功取栈项返回1 
        }
 } 
 
//判断栈空运算算法
int StackEmpty(SqStack st)
{        if(st.top==-1)return 1;                        //栈空返回1 
        else return 0;                                        //栈不空返回0 
 } 

int Match(char exp[],int n)
{
        SqStack st;
        InitStack(st);
        int flag=1,i=0;
        char ch;
        while(i<n&&flag==1)
        {
                switch(exp[i])
                {
                        case'(':case'[':case'{':
                                Push(st,exp[i]);break;
                                case')':
                                        if(!Pop(st,ch)||ch!='(')
                                        flag=0;
                                        break;
                                        case']':
                                                if(!Pop(st,ch)||ch!='[')
                                                flag=0;
                                                break;
                                                case'}':
                                                        if(!Pop(st,ch)||ch!='{')
                                                        flag=0;
                                                        break;
                }
                i++;
        }
        if(StackEmpty(st)&&flag==1)
        {
                DestroyStack(st);
                return 1;
        }
        else
        {
                DestroyStack(st);
                return 0;
        }
}
int main()
{        
        SqStack st;
        int n;
        int flag;
        char *exp;
        printf("判断结果如下\n"); 
        flag=Match(exp,n);
         printf("[(])%s\n",(flag==1?"是匹配的表达式":"不是匹配的表达式"));
        printf("[()]%s\n",(flag==1?"是匹配的表达式":"不是匹配的表达式"));
        printf("[()])%s\n",(flag ==1?"是匹配的表达式":"不是匹配的表达式"));
        printf("([()]%s\n",(flag==1?"是匹配的表达式":"不是匹配的表达式"));
}
最佳答案
2020-10-31 20:46:18
本帖最后由 巴巴鲁 于 2020-10-31 21:06 编辑

很离谱,主函数里n和exp没有初始化或赋值,就当参数传进去了
关于括号匹配,我自己刚刚写了一个代码,仅供参考:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

typedef struct
{
        char data[MAXSIZE];
        int top;
}SqStack;

// 入队运算
int PushStack(SqStack *head, char ch)
{
        if (head->top >= MAXSIZE - 1) // 栈满溢出
        {
                return 0;
        }
        else
        {
                head->top++;
                head->data[head->top] = ch;
                return 1;
        }
}

// 出队运算
int PopStack(SqStack *head)
{
        if (head->top == -1)
        {
                printf("栈为空,无法出队\n");
                return 0;
        }
        else
        {
                head->top--;
                return 1;
        }
}


// 取栈顶元素
char  Gettop(SqStack *head)
{
        char ch;
        ch = head->data[head->top];
        return ch;
}

// 销毁栈
void DestroyStack(SqStack *head)
{
        head->top = -1;
}

// 判断栈是否为空
int EmptyStack(SqStack *head)
{
        if (head->top == -1)
        {
                return 0;
        }
        return 1;
}

void MatchStack(SqStack *head)
{
        int flag = 1;
        int i = 0;
        char ch;
        getchar();
        char exp[MAXSIZE];
        printf("请输入待判定的括号(以回车字符结束):");
        while ((ch = getchar()) != '\n'  && flag == 1)
        {
                switch (ch)
                {
                        // 若是左括号,压入栈中
                        case '(':
                        case '[':
                        case '{':
                                PushStack(head, ch);
                                break;
                        case ')':
                                if (EmptyStack(head) && Gettop(head) == '(')
                                {
                                        PopStack(head);
                                }
                                else
                                {
                                        flag = 0;
                                }
                                break;
                        case ']':
                                if (EmptyStack(head) && Gettop(head) == '[')
                                {
                                        PopStack(head);
                                }
                                else
                                {
                                        flag = 0;
                                }
                                break;
                        case '}':
                                if (EmptyStack(head) && Gettop(head) == '{')
                                {
                                        PopStack(head);
                                }
                                else
                                {
                                        flag = 0;
                                }
                                break;
                }
        }

        if (EmptyStack(head))
        {
                printf("不是匹配的表达式\n\n");
        }
        else
        {
                printf("是匹配的表达式\n\n");
        }
        DestroyStack(head);

}


int main()
{
        SqStack *head = (SqStack *)malloc(sizeof(SqStack));
        if (head == NULL)
        {
                printf("内存调用失败,程序异常退出!\n");
                exit(0);
        }
        head->top = -1;

        int pro, n, flag;

        while (1)
        {
                printf("--------------------------\n");
                printf("1.括号是否匹配\n");
                printf("2.退出程序\n");
                printf("--------------------------\n");
                printf("请输入待执行的功能:");
                scanf_s("%d", &pro);

                if (pro > 2 || pro < 1)
                {
                        printf("输入有误,请重新输入!\n");
                        continue;
                }

                switch (pro)
                {
                        case 1:
                                MatchStack(head);
                                break;
                        case 2:
                                printf("成功退出程序,欢迎下次再来^__^\n");
                                exit(0);
                }
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-31 11:29:19 | 显示全部楼层
具体报错的内容你得发一下吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-31 12:07:03 | 显示全部楼层
小甲鱼的铁粉 发表于 2020-10-31 11:29
具体报错的内容你得发一下吧

程序可以运行但是没有进行判断
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-31 20:46:18 | 显示全部楼层    本楼为最佳答案   
本帖最后由 巴巴鲁 于 2020-10-31 21:06 编辑

很离谱,主函数里n和exp没有初始化或赋值,就当参数传进去了
关于括号匹配,我自己刚刚写了一个代码,仅供参考:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

typedef struct
{
        char data[MAXSIZE];
        int top;
}SqStack;

// 入队运算
int PushStack(SqStack *head, char ch)
{
        if (head->top >= MAXSIZE - 1) // 栈满溢出
        {
                return 0;
        }
        else
        {
                head->top++;
                head->data[head->top] = ch;
                return 1;
        }
}

// 出队运算
int PopStack(SqStack *head)
{
        if (head->top == -1)
        {
                printf("栈为空,无法出队\n");
                return 0;
        }
        else
        {
                head->top--;
                return 1;
        }
}


// 取栈顶元素
char  Gettop(SqStack *head)
{
        char ch;
        ch = head->data[head->top];
        return ch;
}

// 销毁栈
void DestroyStack(SqStack *head)
{
        head->top = -1;
}

// 判断栈是否为空
int EmptyStack(SqStack *head)
{
        if (head->top == -1)
        {
                return 0;
        }
        return 1;
}

void MatchStack(SqStack *head)
{
        int flag = 1;
        int i = 0;
        char ch;
        getchar();
        char exp[MAXSIZE];
        printf("请输入待判定的括号(以回车字符结束):");
        while ((ch = getchar()) != '\n'  && flag == 1)
        {
                switch (ch)
                {
                        // 若是左括号,压入栈中
                        case '(':
                        case '[':
                        case '{':
                                PushStack(head, ch);
                                break;
                        case ')':
                                if (EmptyStack(head) && Gettop(head) == '(')
                                {
                                        PopStack(head);
                                }
                                else
                                {
                                        flag = 0;
                                }
                                break;
                        case ']':
                                if (EmptyStack(head) && Gettop(head) == '[')
                                {
                                        PopStack(head);
                                }
                                else
                                {
                                        flag = 0;
                                }
                                break;
                        case '}':
                                if (EmptyStack(head) && Gettop(head) == '{')
                                {
                                        PopStack(head);
                                }
                                else
                                {
                                        flag = 0;
                                }
                                break;
                }
        }

        if (EmptyStack(head))
        {
                printf("不是匹配的表达式\n\n");
        }
        else
        {
                printf("是匹配的表达式\n\n");
        }
        DestroyStack(head);

}


int main()
{
        SqStack *head = (SqStack *)malloc(sizeof(SqStack));
        if (head == NULL)
        {
                printf("内存调用失败,程序异常退出!\n");
                exit(0);
        }
        head->top = -1;

        int pro, n, flag;

        while (1)
        {
                printf("--------------------------\n");
                printf("1.括号是否匹配\n");
                printf("2.退出程序\n");
                printf("--------------------------\n");
                printf("请输入待执行的功能:");
                scanf_s("%d", &pro);

                if (pro > 2 || pro < 1)
                {
                        printf("输入有误,请重新输入!\n");
                        continue;
                }

                switch (pro)
                {
                        case 1:
                                MatchStack(head);
                                break;
                        case 2:
                                printf("成功退出程序,欢迎下次再来^__^\n");
                                exit(0);
                }
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-1 09:10:01 | 显示全部楼层
巴巴鲁 发表于 2020-10-31 20:46
很离谱,主函数里n和exp没有初始化或赋值,就当参数传进去了
关于括号匹配,我自己刚刚写了一个代码,仅供 ...

好吧...谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-4 23:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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