第五周项目3

来源:互联网 发布:影楼行业全国数据 编辑:程序博客网 时间:2024/06/08 23:03
/* 烟台大学计算机学院  文件名称:ycddd.cpp  作者:于琛  完成日期:2017年10月6日  问题描述:判断表达式中的各种左括号是否与右括号匹配输入描述:表达式输出描述:是否配对正确。用到了stlist.h算法库 */ #include <stdio.h>#include "stlist.h"int main(){    char c;//出栈用到    char st[50];    int d=1, i;//d用来记录是否配对  SqStack *s;    InitStack(s);    printf("请输入表达式:");    scanf("%s", st);    for(i=0; st[i]!='\0'&&d; i++)//读表达式符号    {        switch(st[i])        {        case'(':        case'[':        case'{':            Push(s, st[i]);//符号为左括号入栈            break;        case')'://为右括号出栈比较            Pop(s, c);            if(c!='(') d=0;            break;        case']':            Pop(s, c);            if(c!='[') d=0;            break;        case'}':            Pop(s,c);            if(c!='{') d=0;            break;        }    }    if(StackEmpty(s)&&d==1)        printf("配对正确!!\n");    else        printf("配对错误!!\n");    return 0;}



运行结果:



学习心得:


学会了如何判断表达式的括号配对。但是此程序有bug  例如:())));{}}}}};[]]]]。此程序都判断的不正确。