当前位置:编程学习 > C/C++ >>

C语言 括号匹配问题 帮忙看看哪里有问题 谢谢

#include "Stack_Sq.cpp"

int BracketMatch(char *str);
int March(char ch,char str[]);

int main()
{ char str[100];

 printf("Please input a string:");  gets(str);
 printf("%s\n", BracketMatch(str) ? "括号匹配" : "括号不匹配");
 getchar();  getchar();
 return 1;
}

int March(char ch,char str[])
{   //判断两个括号是否匹配

    if ('ch'-'str[i]'==1||'ch'-'str[i]'==-1||'ch'-'str[i]'==2||'ch'-'str[i]'==-2)
      return 1;
    else return 0;
}

void BracketMatch(char *str)
{
    Stack S;int i; char ch;
    InitStack(&S);
    for(i=0;str[i]!='\0';i++)
    {
        switch(str[i]){
            case'(':
            case'[':
            case'{':
                Push(&S,str[i]);
                break;
            case')':
            case']':
            case'}':
                if(IsEmpty(&S))
                  {printf("\n右括号多余!");return;}
                else
                  {
                      GetTop(&S,&ch);
                      if(Match(ch,str[i]))
                        Pop(&S,&ch);
                      else
                        {printf("\n对应的左右括号不同类!");return;}
                  }
            }
       
    }
    if(IsEmpty(&S))  printf("\n括号匹配!");
    else  printf("\n左括号多余!");
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <stdio.h>
#include <stdlib.h>

#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0

#define Stack_Size 50

typedef char StackElementType;

typedef struct            // 顺序栈
{ StackElementType elem[Stack_Size]; // 用来存放栈中元素的一维数组
 int top;              // 用来存放栈顶元素的下标,top为-1表示空栈
}SeqStack;

void InitStack(SeqStack *S);
int IsEmpty(SeqStack *S);
int IsFull(SeqStack *S);
int Push(SeqStack *S, StackElementType x);
int Pop(SeqStack *S, StackElementType *x);
int GetTop(SeqStack *S,StackElementType *x);
int ClearStack(SeqStack *S);

void InitStack(SeqStack *S)
{ // 将S初始化为一个空栈
   S->top = -1;
}

int IsEmpty(SeqStack *S)
{   // 判断栈S是否为空栈,是返回TRUE,否则返回FALSE
 return(S->top==-1 ? TRUE : FALSE);
}

int IsFull(SeqStack *S)
{   // 判断栈S是否已满,是返回TRUE,否则返回FALSE
 return(S->top==Stack_Size-1 ? TRUE : FALSE);
}

int Push(SeqStack *S, StackElementType x)
{   // 将数据元素x进栈S
 if (S->top==Stack_Size-1) return(FALSE);  // 栈已满
 S->top++;
 S->elem[S->top] = x;
 return(TRUE);
}

int Pop(SeqStack *S, StackElementType *x)
{   // 将栈S的栈顶元素出栈,放到x中
 if (S->top==-1) return(FALSE);  // 栈为空
 *x = S->elem[S->top];
 S->top--;                       // 修改栈顶指针
 return(TRUE);
}

int GetTop(SeqStack *S,StackElementType *x)
{   // 取栈S的栈顶元素,放到x中
 if (S->top==-1) return(FALSE);  // 栈为空
 *x = S->elem[S->top];
 return(TRUE);
}

int ClearStack(SeqStack *S)
{   //将栈S制成空栈
    S->top = -1;
    return(TRUE);
}

追问:好人啊~~谢谢谢谢~~

答案:5分??
未免也太少了吧?

你这代码问题也不少:
1,函数声明与函数的定义不一致,声明的函数是int型返回值,而定义的却是void型返回值。
2,结构体名称写错,使用的结构体的名称不是定义的结构体的名称。
3,调用函数时,函数名写错,不是你定义的那个函数的名称。
4,函数的参数类型与你调用函数时传入的参数类型不一致,函数支持的参数是字符串类型,你传的却是单字符类型。
5,把变量名当成字符串用,变量名两边加上单引号就不是那个变量了,而是一个字符常量。
6,用#include去包含cpp文件,虽然是可以,但不推荐这样用,应该新建一个h文件,把函数的声明以及#define宏定义写进去,之后,#include "你的头文件.h"即可。
7,代码编写风格不怎么好,虽然不是大问题,但影响代码的可读性,建议格式化代码。



花费了这么多时间为你修改/调试代码,不采纳答案那还真对不起我了。
以下内容,请看完,建议复制粘贴到代码编辑器里,重新格式化代码,方便阅读。


假设头文件为: Stack_Sq.h
那么,它的内容应该为:
//宏定义 
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0

#define Stack_Size 50

typedef char StackElementType;

//定义结构体 
typedef struct            // 顺序栈

  StackElementType   elem[Stack_Size]; // 用来存放栈中元素的一维数组
  int top;              // 用来存放栈顶元素的下标,top为-1表示空栈
}
SeqStack;

//以下声明函数原型 
void InitStack(SeqStack *S);
int IsEmpty(SeqStack *S);
int IsFull(SeqStack *S);
int Push(SeqStack *S, StackElementType x);
int Pop(SeqStack *S, StackElementType *x);
int GetTop(SeqStack *S,StackElementType *x);
int ClearStack(SeqStack *S);








头文件写好了,之后在Stack_Sq.cpp中写上以下内容: 


#include "Stack_Sq.h" //包含头文件,使用头文件中的宏定义以及结构体的定义 

void InitStack(SeqStack *S)
{ // 将S初始化为一个空栈
   S->top = -1;
}

int IsEmpty(SeqStack *S)
{   // 判断栈S是否为空栈,是返回TRUE,否则返回FALSE 
  return(S->top==-1 ? TRUE : FALSE);
}

int IsFull(SeqStack *S)
{   // 判断栈S是否已满,是返回TRUE,否则返回FALSE 
   return(S->top==Stack_Size-1 ? TRUE : FALSE);
}

int Push(SeqStack *S, StackElementType x)
{   // 将数据元素x进栈S
 if (S->top==Stack_Size-1)  return(FALSE);  // 栈已满
 S->top++;
 S->elem[S->top] = x;
 return(TRUE);
}

int Pop(SeqStack *S, StackElementType *x)
{   // 将栈S的栈顶元素出栈,放到x中
 if (S->top==-1) return(FALSE);  // 栈为空
 *x = S->elem[S->top];
 S->top--;                       // 修改栈顶指针
 return(TRUE);
}

int GetTop(SeqStack *S,StackElementType *x)
{   // 取栈S的栈顶元素,放到x中
 if (S->top==-1) return(FALSE);  // 栈为空
 *x = S->elem[S->top];
 return(TRUE);
}

int ClearStack(SeqStack *S)
{   //将栈S制成空栈 
    S->top = -1; 
    return(TRUE);
}


最后,在你的main.cpp中写上以下内容:
 
#include <stdio.h>
#include <stdlib.h>
#include "Stack_Sq.h" //包含头文件,使用头文件中的宏定义以及结构体的定义 

int BracketMatch(char *str);
//错误:BracketMatch函数的声明和调用都是int型的返回值,而定义却是void型的 
int March(char ch,char str[]);

int main()

  char str[100];
  printf("Please input a string:");  gets(str);
  //BracketMatch()函数需要有返回值,不能用void,不然怎么根据返回值来判断括号是否匹配??? 
  //当BracketMatch()函数返回值为1时,也就是是为“真”,显示的字符串为:"括号匹配"
  // 当BracketMatch()函数返回值为0时,也就是是为“假”,显示的字符串为:"括号不匹配"
  printf("%s\n", BracketMatch(str) ? "括号匹配" : "括号不匹配");
  getchar();  
  getchar();
  return 1;
}

//int March(char ch,char str[])
//{   //判断两个括号是否匹配 
   //错误:if ('ch'-'str[i]'==1||'ch'-'str[i]'==-1||'ch'-'str[i]'==2||'ch'-'str[i]'==-2)
   //你这变量名加单引号就不是变量名了,而是一个字符常量的内容,一个字符用单引号,字符串就用双引号
   // 改正后为:
   //if (ch - str[i] ==1 || ch - str[i] == -1 || ch - str[i] == 2 || ch - str[i] == -2)
   //   return 1;
   //还有,你这个变量i从哪里来的?在哪里定义的?
   //else return 0; 
//}

//以下是重新修改后的March函数: 
int March(char ch1,char ch2)
{   //判断两个括号是否匹配  
   if (ch1 - ch2 == 1 || ch1 - ch2 == -1 || ch1 - ch2 == 2 || ch1 - ch2 == -2)
      return 1; 
   else return 0; 
}


//void BracketMatch(char *str) 声明的是int型,你这定义却是void型的,不统一!
//改正后为:
int BracketMatch(char *str)  
{
    //错误:Stack S;
    //Stack类型你定义了吗? 唯一定义的是SeqStack结构体。
    //改正后为:
    SeqStack S; 
    int i; 
    char ch;
    InitStack(&S);
    for(i=0;str[i]!='\0';i++)
    {
        switch(str[i])
        {
            case'(':
            case'[':
            case'{':
                Push(&S,str[i]);
                break;
            case')':
            case']':
            case'}':
                if(IsEmpty(&S))
                  { 
                      printf("\n右括号多余!");
                      return 0;
                 }
                else
                {
                      GetTop(&S,&ch);
                      //错误:if(Match(ch,str[i]))
                     //有Match()函数吗?没定义,你定义的是 March函数,名字写错了
                     //改正后为:
                      if(March(ch, str[i])) 
                      //但是,参数有问题,str[i]指的是字符串变量str中第i个元素,而这个元素只保存单个字符,不能保存字符串 
                         Pop(&S,&ch);
                      else
                      {
                         printf("\n对应的左右括号不同类!");
                         return 0;//返回0,表示“假” 
                   

上一个:<<C语言程序设计>>的总结?
下一个:考计算机三级网络技术要学会C语言吗 ?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,