Okay it might be easiest if we start with pinning down what it takes for the expression to be invalid.
Here are a few examples:
The first one is invalid because a bracket should not come directly after a numberCode:1 ( 2 ) + 3 )
The second one is invalid because a plus sign must not be the first thing in the expression.
The Third one is invalid because an expression must not start with a close bracket, or more accurately, the close bracket does not have a corresponding open bracket before it.
See how many more you can come up with.
Then once you think you've got all the rules about when something is invalid, then you've got a good idea on what it takes for an expression to be valid, and after that you might be able to work out pseudocode for solving this and then onto the real code.
One thing though, do you know all of the possible things that can be in an expression? I mean it can have plus, minus, multiply and divide, but can it also have thing like ^ which means to-the-power-of? If you aren't sure, then just assume it only has +, -, *, /, (, ).
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
How about putting the expression, into a string of char's:
Now if left == right and noGood still equals 0, it's OK.Code:Pseudocode: char exp[50] = { '\0' }; fgets() can get the expression from the user, and put it into the exp array then have 2 int variables to count up the left and right tally of brackets: int left, right, noGood; then we can scan the exp array, char by char: left = right = noGood = 0; for(i = 0; exp[i] != '\0'; i++) { if(exp[i] == '(') left++; else if(exp[i] == ')') right++; //there can't be more right brackets then left brackets in the expression, at any time if(right > left) noGood = 1; }
So print that message
Note that this is pseudocode, not ready-to-run code.
Oh, that could be the case. Okay I'll assume that only the brackets are to be checked. Though that makes it even stranger that the first bit of code posted would be for linked lists.
In that case, all you need to do is starting from the left hand side of the expression, look at one character at a time, looking for ( and ) characters. Then just make sure that the number of close brackets never exceeds the number open brackets at any point.
Some examples:
The for-loop with 'sum' in it is the kind of thing that would help here, though there are definitely a few problems with the original code for that. It should not use sizeof for a start. You'll probably want to use strlen.Code:( invalid - there are more open that close ( ) valid ) ( invalid - there is a point at which we have closed more than we have opened ( ( ) invalid - not all brackets are closed (the counts are not equal) ( ) ) ( ( ) invalid - the counts match, but again there is a point at which more have been closed than were previously opened
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Okay, now start with a basic Hello World program, and add in something to read in the expression from the user. You'll need something like fgets and sscanf and an array of chars, though there is certainly more than one way of doing it.
See if you can write a program that reads in a sentence and writes it out back to you again.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
hi angel_copra,
iMalc is already helping you . I just re-conveyed what your requirement was. What he was about to tell, could have drastically increased the complexity of the program. Dont worry you are in good hands. Keep trying. C is easy once you understand what you are doing.
Last edited by creeping death; 04-04-2009 at 08:31 PM.
Code:printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);
you can't use z, since you didn't declare it. sum is what you want, isn't it?
Please use code tags around your code. Click on the # sign after you highlight your code, to do that.
should it fix it or only detect if there its right or not ?
i coded a fix program for it
Code:#include <stdio.h> void string_fixer(char *buffer) { int i;//loop counter for(i=0;buffer[i]!=0;i++){ if(buffer[0]=='('){ if(buffer[i]=='/'){ buffer[i-2]=')'; } } } } int main(void) { char buffer[100]; double x;//for atoi sprintf(buffer,"( 3 * 4 + 2 ( / (8-2)"); string_fixer(buffer); puts(buffer); return getchar(); }
you should ident your code lil bit and put instead of z put num coz thats ur equation whish sum = x+y
here you go~~
code not tested!!
Code:main(){ char *input="(2+3)+((4-6)"; int flag=0; while(*input){ if(*input=='('){ flag++; }else if(*input==')'){ flag--; } if(flag<0){ printf("Incorrect brackets starting at: %s\n",input); exit(0); } *input++; } if(flag!=0){ printf("Probably %d closing bracket(s) missing brackets\n",flag); }else{ printf("Correct brackets!\n"); } }
main should be int main(void) - read FAQ
input shoudl be const char*
Why do you need * here?*input++;
Also your main is missing return 0; at the end
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler