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("paranthesis are balance");
}
else
printf("paranthesis are not balance");
}
getch();
return 0;
}
views:
90answers:
3If you'd use indentation on your code you could spot the problems a lot easier:
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;
}
There's a lot wrong. I suggest reading a tutorial. This one, for example. You can google for more with "C tutorial"
The technique I'd use is to go through the input, keeping a counter of parentheses. For each '(', increment the counter. For each ')', decrement the counter and check if it's negative (to spot things like "())(()", which has the same number of left and right parens, but aren't balanced). If the counter goes negative at any point, the parens are unbalanced, otherwise they're balanced if the counter is zero at the end.
Frankly, I completely fail to understand what you're trying to accomplish with the above code, and I've got a lot of experience at reading student C code, so all I can give you is algorithmic advice.
then plz give me simple code of my problem and that is i have equation like this ((9+5)/55) and array of lenght 30 , if the parenthesis are balanced then solve the operation and and get the output ... thats it. i need it