2010-04-16 48 views
-1
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

它有什么不对?它会产生错误的结果吗? – MJB 2010-04-16 16:03:20

+2

输入缓冲区相当小。您可能希望使其变大或添加大小检查以防止缓冲区溢出。 – futureelite7 2010-04-16 16:04:39

+1

这会编译吗?如果您需要在“if”块中包含多行代码,则需要使用{}将它们包围。 – Vicky 2010-04-16 16:12:53

回答

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教程”获取更多信息。

0

我使用的技巧是通过输入,保留括号的计数器。对于每个'(',递增计数器,对于每个')',递减计数器并检查其是否定的(以发现具有相同数量的左侧和右侧支配的“())(()”的东西,但是不平衡)。如果计数器在任何时候都变为负值,那么这些参数是不平衡的,否则,如果计数器在最后为零,它们会保持平衡。老实说,我完全不明白你想用上面的代码来完成什么,而且我在阅读学生C代码方面有很多经验,所以我可以给你的只是算法建议。

相关问题