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

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Post please ....i need help with this program

    Write a program that read an expression and tell the user whether the bracket are correct or not (brackets checker)
    for example:
    ( 3 * 4 + 2 ) / ( 8 - 2 ) is correct

    while the expreesion ( 3 * 4 + 2 ( / (8-2) is incorrect

    i wrote this incomplete prog...... please help me to complete it...


    the code....


    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    struct Node;
    typedef struct Node * PtrToNode;
    struct Node
    {
    int element;
    PtrToNode Next;
    };

    PtrToNode MakeEmpty(PtrToNode L)
    {
    L= new(Node);
    L->Next=NULL;
    return L;
    }

    void Push(PtrToNode L,int x)
    {
    PtrToNode S;
    S= new(Node);
    S->element=x;
    S->Next=L->Next;
    L->Next=S;
    }

    int Pop(PtrToNode L)
    {
    PtrToNode P;
    P=L->Next;
    int x=P->element;
    L->Next=P->Next;
    free(P);
    return x;
    }

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    For some reason I have a feeling that you have just dumped your homework to us. It smells like your teacher had given the code to you, and asked you to complete it. I doubt that you would've written that if your goal is what you described. Students tend to start by writing a main, not by trying to implement a list...

    So please, tell us more specifically waht your problem is. Simple "help me to write a program" does not quite cut it...

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    you are thinking this too complicated...

    think it this way... for every "(" there has to be a ")" otherwise the expression is wrong...

    why all this pushing and poping and use of struct? unless you are simulating push-down automata
    Last edited by creeping death; 04-04-2009 at 11:54 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by creeping death View Post
    you are thinking this too complicated...

    think it this way... for every "(" there has to be a ")" otherwise the expression is wrong...

    why all this pushing and poping and use of struct? unless you are simulating push-down automata
    So this expression is correct then:
    Code:
    ())(()
    since it contains the right number of left and right match.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    i wrote the main but i am completely sure that it is incorrect......if you can correct it....please help



    main()
    {
    PtrToNode L;
    int choise;
    L = MakeEmpty(NULL);


    printf("please enter your numbers without space");
    scanf("%d",&choise);
    while(L->Next!=NULL)
    {
    if(L->element=='+')
    break;

    else if(L->element=='-')
    break;

    else if(L->element=='*')
    break;

    else if(L->element=='/')
    break;

    else if(L->element=='(')
    {
    Push(L,choise);
    }
    else if(L->element==')')
    {
    Pop(L);
    }


    getch();
    }

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    No! if my "flag" variable if it becomes -1, the expression is wrong(it becomes -1 and does not check any further)...hehe...you almost got me on that occasion

    Code:
    [c_d@localhost c scratchpad]$ ./a.out
    enter expression())(()
    expr is wrong
    like i said earlier for every "(" there got to be a ")" ... since ")" came before "(" the expression became wrong...
    Last edited by creeping death; 04-04-2009 at 12:19 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    hi angel_copra,
    Code:
    printf("please enter your numbers without space");
    scanf("%d",&choise);
    what is this?
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    lol sorry....x input from the user eg, ( 3 + 3 ) / 8

    main()
    {
    PtrToNode L;
    int x;
    L = MakeEmpty(NULL);


    printf("please enter your numbers without space");
    scanf("%d",&x);
    while(L->Next!=NULL)
    {
    if(L->element=='+')
    break;

    else if(L->element=='-')
    break;

    else if(L->element=='*')
    break;

    else if(L->element=='/')
    break;

    else if(L->element=='(')
    {
    Push(L,choise);
    }
    else if(L->element==')')
    {
    Pop(L);
    }


    getch();
    }

  9. #9
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    you cant store an expression in an integer.

    you need to store the expression as a string.
    and then consider each character of the string, one at a time.

    also are you supposed to only check if the brackets are balanced or if the expression is correct also?

    example: (a +b + ) * ( a / ) here the brackets are balanced but expression is wrong.

    if you have to check for brackets only , then push the character whenever you find ( and pop whenever you find ) ... you are doing that ...except you should handle the situation when you try to pop() when the stack is empty....(like in the example matsp gave)

    but why break whenever you encounter a binary operator?
    Last edited by creeping death; 04-04-2009 at 12:35 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    i am check for brackets only... i have 2 remove this ...right

    if(L->element=='+')
    break;

    else if(L->element=='-')
    break;

    else if(L->element=='*')
    break;

    else if(L->element=='/')
    break;

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    what about this code....


    for (int i=0;i<sizeof b;i++)
    {
    if (b[i]=='(')
    sum++;
    if (b[i]==')')
    sum--;
    if (sum<0)
    break;
    }

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's clear you don't have a clue what you're doing and this problem is too far above your difficulty level at this stage. All you've got are a bunch of random bits of code thrown together and you're just hoping that it in some way does something useful. Even then I'd guess that those bits were copied from someone else.
    So there are two problems here:
    1. You don't understand how to fully solve the problem
    2. You aren't yet a proficient C Programmer

    Which one of those would you like help with first?
    How long have you got left for this assignment?
    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"

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    sum=0;

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by angel_copra View Post
    what about this code....

    Code:
    for (int i=0;i<sizeof b;i++)
        {
        if (b[i]=='(')
            sum++;
        if (b[i]==')')
            sum--;
        if (sum<0)
            break;
        }
    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.
    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"

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    1.yes i don't understand how to solve the problem

    my assignment 8/4/2009 about 4 days

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