C语言编写括号的配对问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 23:32:43
要求编写判断一个表达式中括号是否配对的问题。
我想用
c=getchar();
if(c=='(')....
来判断字符c是不是等于括号。
一直不成功。
就自己写了句
cout<<'}'==c?1:0;
编译出现错误。
error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'char' (or there is no acceptable conversion)
好像是说不可以用==判断字符c和‘(’的关系。
那么要怎么写呢。大家帮忙。
自己做出来了。
#include "iostream.h"
#include "stdio.h"
void main()
{
int i=0,n=0;
char x[100],c;
printf("输入一个表达式:");
c=getchar();
while(c!='\n')
{
n=1;
if(c=='{'||c=='['||c=='(')
x[i++]=c;
else
{
if((c=='}'&&x[i-1]=='{')||(c==']'&&x[i-1]=='[')||(c==')'&&x[i-1]=='('))
x[--i]='\0';
else {x[0]=1;break;}
}
c=getchar();
}
if(n==0) cout<<"NULL"

c=getch();
if(c=='(')

这样试试.

c=getchar();
if(c=='(')
你这么写也对啊.
我这边正常,是不是你写的代码有问题?

我的可以用括号呢 可能你的括号不是英文括号是中文括号吧