Thread: please ....i need help with this program

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Quote Originally Posted by iMalc View Post
    Ah now that code could actually do something useful. It doesn't solve the whole problem, but it does solve part of it, if you know what comes after it. However, unless you can demonstrate that you understand how that works, I'm inclined to believe that it is someone else's code.

    Perhaps you could attempt to describe in your own words, the algorithm you will use to solve the problem. It doesn't matter if you get it wrong or get stuck, just give it a try and we'll give you a nudge.

    You are right it’s not my code; I can’t solve it by myself….

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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:
    Code:
    1 ( 2 )
    + 3
    )
    The first one is invalid because a bracket should not come directly after a number
    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"

  3. #18
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Quote Originally Posted by iMalc View Post
    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:
    Code:
    1 ( 2 )
    + 3
    )
    The first one is invalid because a bracket should not come directly after a number
    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 +, -, *, /, (, ).
    k what else...?

  4. #19
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by iMalc View Post
    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:
    Code:
    1 ( 2 )
    + 3
    )
    The first one is invalid because a bracket should not come directly after a number
    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 +, -, *, /, (, ).
    hi, i think the OP just wants to check if the brackets are correct and balanced out...but anyway...please ignore me and continue...you're doing an awesome thing btw.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Quote Originally Posted by creeping death View Post
    hi, i think the OP just wants to check if the brackets are correct and balanced out...but anyway...please ignore me and continue...you're doing an awesome thing btw.
    whyyyyyyyyy? please i need your help

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about putting the expression, into a string of char's:

    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;
         
    }
    Now if left == right and noGood still equals 0, it's OK.

    So print that message

    Note that this is pseudocode, not ready-to-run code.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by creeping death View Post
    hi, i think the OP just wants to check if the brackets are correct and balanced out...but anyway...please ignore me and continue...you're doing an awesome thing btw.
    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:
    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
    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.
    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"

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  9. #24
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by angel_copra View Post
    whyyyyyyyyy? please i need your help
    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);

  10. #25
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Quote Originally Posted by iMalc View Post
    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.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int x,y,sum;
    printf("please enter your value");
    scanf("%d%d",&x,&y);
    sum=x+y;
    printf("the answer is %d ",z);
    getch();
    }


    loool

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  12. #27
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    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();
    }

  13. #28
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    you should ident your code lil bit and put instead of z put num coz thats ur equation whish sum = x+y

  14. #29
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    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");
    	}		
    }

  15. #30
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main should be int main(void) - read FAQ
    input shoudl be const char*
    *input++;
    Why do you need * here?

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM