int main(){
char i,input[30],close,open;
for(i=0;i<='.';i++){
printf("enter equation");
scanf("%c",input[i]);
if(input[i]=='(')
input++;
input[i]=open;
else if(input[i]==')')
input[i]--;
input[i]=close;
else if(open[i]==close[i])
{
printf("parenthesis are balance");
}
else
printf("parenthesis are not balance");
}
getch();
return 0;
}
答案 0 :(得分:4)
如果你在代码上使用缩进,你可以更轻松地发现问题:
int main()
{
char i,input[30],close,open;
for(i=0;i<='.';i++) // why i <= '.'? maybe you mean int i and input[i] != '.'...
{
printf("enter equation");
scanf("%c",input[i]); // you need &input[i]. In fact, I think what you need is scanf("%s", input); but outside of this for loop...
if(input[i]=='(')
input++; // Do you mean input[i]++?
input[i]=open; // this isn't inside the if condition. Use brackets if you want it to be
else if(input[i]==')') // won't compile because there's no matching if
input[i]--;
input[i]=close; // not inside the else. Also, what is close and open? You don't initialize them
else if(open[i]==close[i]) // open and close are not arrays. You can't use them like this
{
printf("paranthesis are balance");
}
else
printf("paranthesis are not balance");
}
getch();
return 0;
}
有很多错误。我建议阅读一个教程。例如This one。你可以通过“C教程”谷歌了解更多信息
答案 1 :(得分:0)
我使用的技术是通过输入,保留括号的计数器。对于每个'(',递增计数器。对于每个')',递减计数器并检查它是否为负(发现像“())(()”,它具有相同数量的左右parens,但是不平衡)。如果计数器在任何点都变为负值,则表示不平衡,否则如果计数器最后为零则它们是平衡的。
坦率地说,我完全不明白你要用上面的代码完成什么,而且我在阅读学生C代码方面有很多经验,所以我能给你的只是算法建议。