Thread: problems on polish notation

  1. #1
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy problems on polish notation

    i have an algorithm on polish notation

    content of the file eq.txt
    Code:
    ((3+2)*5)+5
    i will open the code using file io

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <iostream.h>
    #include "stack.h"
    void main()
    {
    clrscr();
    char str[1500], file[10], sfile[10];
    int x;
    FILE *fp1, *fp2;
    printf("Enter filename:");
    gets(file);
    fp1=fopen(file,"r");
    printf("Save to:");
    gets(sfile);
    fp2=fopen(sfile,"w");
    
    if(fgets(str, sizeof(str), fp1)!=NULL)
    {
    fputs(str,fp2);
    printf("Content is:\n %s", str);
    }
    else
    {
    printf("cannot open file!!!");
    }
    fclose(fp1);
    fclose(fp2);
    getch();
    }
    the result is the equation ((3+2)*5)+5

    but if this program reads the equation which is the content of the file and the output of this program will be the result of this equation

    Code:
    ((3+2)*5)5+5
    (5*5)+5
    25+5
    30
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    so parse the equation yourself or use a 3rd party maths parser to do it for you. And you haven't asked any questions, thats just what I assume you wanted.

  3. #3
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50
    Quote Originally Posted by zacs7 View Post
    so parse the equation yourself or use a 3rd party maths parser to do it for you. And you haven't asked any questions, thats just what I assume you wanted.
    but how?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    http://muparser.sourceforge.net/ Which is a C++ math parser library, but there are wrappers available, plus your "stack.h" in the other thread is in C++... So assumed your using C++ not C.

  5. #5
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Unhappy

    Quote Originally Posted by zacs7 View Post
    http://muparser.sourceforge.net/ Which is a C++ math parser library, but there are wrappers available, plus your "stack.h" in the other thread is in C++... So assumed your using C++ not C.
    but how and where do you evaluate an expression?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by blacksnake View Post
    but how and where do you evaluate an expression?
    I STRONGLY recommend that you goto Wikipedia's website and read up on Polish Notation. You need to know EXACTLY what it is that you're trying to work with - is it:

    *Polish Notation
    *Reverse Polish Notation
    or
    Postfix Notation

    You surely will want to see some examples of each of these, and decide what type of notation you want to work with, before engaging a lot of time into the project.

    If you want to parse it all out, a good start would be:

    1) Scan the digits and locate the innermost bracketed items.
    2) Follow the rules of mathematical precedence and solve that one item.
    3) Re-write the original equation, substituting your result for the innermost bracketed item.

    4) Loop back to number 1 above, and repeat until it's reduced fully to a single value.

    There are several sites that explain the above much better than I can. If you have trouble with your CODE or algorithm, please post your code and explain what the problem is and what output you're currently getting from your program that's not correct.

    You need to be quite specific in order to get valuable help.

    Good luck, this is an ambitious project.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Nevermind all that advanced crap, there's a whole bunch of basic horrors which need to be fixed.

    > #include <stdio.h>
    This is a C header

    > #include <conio.h>
    This is an ancient platform specific header

    > #include <iostream.h>
    This is an old C++ header

    > void main()
    Bzzzt - try again

    > gets(file);
    Woohoo - next stop, buffer overflow city, all change please!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    Quote Originally Posted by Adak View Post
    I STRONGLY recommend that you goto Wikipedia's website and read up on Polish Notation. You need to know EXACTLY what it is that you're trying to work with - is it:

    *Polish Notation
    *Reverse Polish Notation
    or
    Postfix Notation

    You surely will want to see some examples of each of these, and decide what type of notation you want to work with, before engaging a lot of time into the project.

    If you want to parse it all out, a good start would be:

    1) Scan the digits and locate the innermost bracketed items.
    2) Follow the rules of mathematical precedence and solve that one item.
    3) Re-write the original equation, substituting your result for the innermost bracketed item.

    4) Loop back to number 1 above, and repeat until it's reduced fully to a single value.

    There are several sites that explain the above much better than I can. If you have trouble with your CODE or algorithm, please post your code and explain what the problem is and what output you're currently getting from your program that's not correct.

    You need to be quite specific in order to get valuable help.

    Good luck, this is an ambitious project.
    algorithm:
    Code:
    *open a file
    *read the file character by character
    *for( i=0;i<x;i++)//assuming that x is the number of times you operate inside the parenthesis, for example, ((2+3)5)+5, this requires three times in parsing to get the result
    *follow step 1 in reading characters if it is digit, place it on a stack, if it is an operand solve the number of digits inside the parenthesis
    if the operand is addition or so, follow step number 2
    **if it is addition, add the two numbers on a stack
    **same through for subtract, multiply and divide and also solving for exponents
    *the result will be the first number to be place on a stack for another parsing until the parsing is finished
    *display the result
    pseudo code
    Code:
    main()
    {
    in(filename)
    f1=open(filename, read)
    in(saved filename)
    f2=open(filename, write)
    
    if(filename!=NULL)
    {
    show original content
    }
    else
    {
    error
    }
    int x
    for(i=0;i<x;i++)
    {
    isdigit
       placing stack
    isoperand
       do step number 2
    }
    
    return result
    }
    but im find hard to convert my pseudo code to my C++ code, on my algorithm translate the code is my problem..how to convert my algorithm/pseudo code to C++ code?

    NOTE: stack.h is include in the program
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by blacksnake View Post

    but im find hard to convert my pseudo code to my C++ code, on my algorithm translate the code is my problem..how to convert my algorithm/pseudo code to C++ code?

    NOTE: stack.h is include in the program
    If you're working in C++, then you should go to the C++ forum.

    You should also read and study the several examples of what you want to do, on the net. Thats's why you can't make up good pseudo code yet.

    You have a good general idea of what you want to do, and how you want to do it, but it's lacking in the detail it needs - and just what you would likely gain from - reading and studying the websites that deal with RPN or postfix, in great detail.

    You need details to your pseudo code, and these websites have the details - go and get them, they're free!!

  10. #10
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50
    Quote Originally Posted by Adak View Post
    If you're working in C++, then you should go to the C++ forum.

    You should also read and study the several examples of what you want to do, on the net. Thats's why you can't make up good pseudo code yet.

    You have a good general idea of what you want to do, and how you want to do it, but it's lacking in the detail it needs - and just what you would likely gain from - reading and studying the websites that deal with RPN or postfix, in great detail.

    You need details to your pseudo code, and these websites have the details - go and get them, they're free!!
    where are the website in relation to this notation?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by blacksnake View Post
    where are the website in relation to this notation?

    Google is your friend:
    http://www.google.com/search?q=Rever...ient=firefox-a

    That's what I got when I entered "Reverse Polish Notation" in the Google searcher, but you can
    also get things customized for your OS (I'm using Ubuntu with Firefox as my browser), and your own browser; as well as your preferred language (if it's available).

  12. #12
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50
    i have an idea:

    after reading the content of the file, follow the conversion of infix to postfix code below
    Code:
    main(int argc, char argv[])
    {
    	char *a=argv[1];
    	int i, N=strlen(a);
    
    	STACKinit(N);
    	for(i=0;i<N;i++)
    	{
    		if((a[i]=='+')||(a[i]=='*'))
    		    STACKpush(a[i]);
    		if(a[i]++')')
    		    printf("&#37;c",STACkpop());
    		if((a[i]>='0')&&(a[i]<='9'))
    		    printf("%c", a[i]);
    	}
    	printf("\n");
    }
    then the evaluation of postfix:
    Code:
    main(int argc, char argv[])
    {
    	char *a=argv[1];
    	int i, N=strlen(a);
    
    	STACKinit(N);
    	for(i=0;i<N;i++)
            {
                    if(a[i]=='+')
                       STACKpush(STACKpop()+STACKpop());
                    if(a[i]=='-')
                       STACKpush(STACKpop()-STACKpop());
                    if(a[i]=='*');
                       STACKpush(STACKpop()*STACKpop());
                    if((a[i]>='0')&&(a[i]<=9))
                       STACKpush(0);
                    while((a[i]>='0')&&(a[i]<='9'))
                       STACKpush(10*STACKpop()+a[i++]-'0'));
            }
            printf("%d \n", STACKpop());
    }
    how to combine these code to FILE IO on the first page?
    Last edited by blacksnake; 06-22-2007 at 07:47 PM.
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would treat file IO as a detail, and put it off until later.

    Right now, you need to give your algorithm and code you have written, a try with a few expressions, and make sure it's doing what you need it to.

    THEN go back and look at what you want to do with file IO. It won't be a problem if you use an expression with the same data type now, as you want to use later, for the file input.

    So if you're going to want to use strings to take in the char's from a file, then use strings here to store your expression, right off.

    Then parse it in the order you need, change it to either integers or floats as appropriate, and then feed them onto your stack.

    fgets() will do a great job of putting your file chars into a string from the file, don't worry.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    int main ( ) {
      char *tests[] = {
        "1",
        "1+2",
        "((3+2)*5)+5",
      };
      stack s;
      double answer;
      int i;
    
      for ( i = 0 ; i < sizeof(tests)/sizeof(*tests) ; i++ ) {
        StackInit(s);
        parse( tests[i], s );
        answer = eval(s);
        printf("Expr=%s, Result=%f\n", tests[i], answer );
      }
    
      return 0;
    }
    No I/O to worry about, and you can add more and more complicated tests to the array as you go, and keep all the earlier tests around to make sure you don't break anything.

    When you're happy with it, then you can worry about the I/O you really want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    what are the header file do you used in this program?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Impix or reverse polish notation
    By P4R4N01D in forum C Programming
    Replies: 10
    Last Post: 11-18-2008, 11:42 PM
  2. Postfix (reverse polish notation) calculator
    By ottomated in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:32 PM
  3. polish notation calc problem
    By deedlit in forum C Programming
    Replies: 6
    Last Post: 06-14-2004, 10:17 PM
  4. infix vs Reverse Polish notation
    By dionys in forum C Programming
    Replies: 1
    Last Post: 05-10-2004, 02:25 PM
  5. polish notation
    By CLIE_ZETA in forum C Programming
    Replies: 1
    Last Post: 12-01-2001, 03:07 AM