Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. RPN notation in C

RPN notation in C

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelplearning
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12495620
    wrote on last edited by
    #1

    Hello, I am trying to code a program which evaluates expressions in reverse polish notations. For now, it should work only with numbers and operands ( +, -, * and / ). Here is the code I have written:

    #include #include #include #include #include #define MAX 41

    int t, i, v1, v2;
    int top = -1;
    char seq[40];
    char rpn[40];
    char fin[40];
    int main()
    {
    printf("introduce the numbers and opreands in rpn \n");
    fgets(seq,40,stdin);

    t=strlen(seq);

    for (i=0;i<=t;i++)
    if (isdigit(seq[i]))
    {
    Push(rpn, seq[i]);
    }
    if (ispunct(seq[i]));

    v1=pop(rpn);
    v2=pop(rpn);
    
    if (seq\[i\] = '+');
        push(fin, v1+v2);
    if (seq\[i\] = '-');
        push(fin, v1-v2);
    if (seq\[i\] = '\*');
        push(fin, v1\*v2);
    if (seq\[i\] = '/');
        push(fin, v1/v2);
    

    printf(fin);
    }

    int isempty() {

    if(top == -1)
    return 1;
    else
    return 0;
    }

    int isfull() {

    if(top == MAX)
    return 1;
    else
    return 0;
    }

    int peek() {
    return seq[top];
    }

    int pop() {
    int data;

    if(!isempty()) {
    data = seq[top];
    top = top - 1;
    return data;
    }else {
    printf("Could not retrieve data, Stack is empty.\n");
    }
    }

    int push(int data) {

    if(!isfull()) {
    top = top + 1;
    seq[top] = data;
    }else {
    printf("Could not insert data, Stack is full.\n");
    }
    }

    There are, I think, some big mistakes i can't solve by myself, since im a beginner in programming. I have searched for a solution on the internet, but i don't truly understand them. The idea of my code is that the user types some numbers and operands like this: 2 3 +. In this case, the program should add 2 and 3 in a stack and when it reads and operator, pop the lasts 2 numbers on the pile and do the corresponding opperation. When i build it I have this errors:

    ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
    C:\Users\Jordi\Documents\RPN.o:RPN.c|| undefined reference to `Push|
    ||error: ld returned 1 exit status|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    L 2 Replies Last reply
    0
    • U User 12495620

      Hello, I am trying to code a program which evaluates expressions in reverse polish notations. For now, it should work only with numbers and operands ( +, -, * and / ). Here is the code I have written:

      #include #include #include #include #include #define MAX 41

      int t, i, v1, v2;
      int top = -1;
      char seq[40];
      char rpn[40];
      char fin[40];
      int main()
      {
      printf("introduce the numbers and opreands in rpn \n");
      fgets(seq,40,stdin);

      t=strlen(seq);

      for (i=0;i<=t;i++)
      if (isdigit(seq[i]))
      {
      Push(rpn, seq[i]);
      }
      if (ispunct(seq[i]));

      v1=pop(rpn);
      v2=pop(rpn);
      
      if (seq\[i\] = '+');
          push(fin, v1+v2);
      if (seq\[i\] = '-');
          push(fin, v1-v2);
      if (seq\[i\] = '\*');
          push(fin, v1\*v2);
      if (seq\[i\] = '/');
          push(fin, v1/v2);
      

      printf(fin);
      }

      int isempty() {

      if(top == -1)
      return 1;
      else
      return 0;
      }

      int isfull() {

      if(top == MAX)
      return 1;
      else
      return 0;
      }

      int peek() {
      return seq[top];
      }

      int pop() {
      int data;

      if(!isempty()) {
      data = seq[top];
      top = top - 1;
      return data;
      }else {
      printf("Could not retrieve data, Stack is empty.\n");
      }
      }

      int push(int data) {

      if(!isfull()) {
      top = top + 1;
      seq[top] = data;
      }else {
      printf("Could not insert data, Stack is full.\n");
      }
      }

      There are, I think, some big mistakes i can't solve by myself, since im a beginner in programming. I have searched for a solution on the internet, but i don't truly understand them. The idea of my code is that the user types some numbers and operands like this: 2 3 +. In this case, the program should add 2 and 3 in a stack and when it reads and operator, pop the lasts 2 numbers on the pile and do the corresponding opperation. When i build it I have this errors:

      ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
      C:\Users\Jordi\Documents\RPN.o:RPN.c|| undefined reference to `Push|
      ||error: ld returned 1 exit status|
      ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Unless this is a fragment, it seems like you have done no effort to actually implement push and pop. `strlen` (the usual one) is from string.h which you should therefore include.

      U 1 Reply Last reply
      0
      • L Lost User

        Unless this is a fragment, it seems like you have done no effort to actually implement push and pop. `strlen` (the usual one) is from string.h which you should therefore include.

        U Offline
        U Offline
        User 12495620
        wrote on last edited by
        #3

        You are right, sorry, I even forgot to put them in the code! This is something a partner gave and explained to me. There are some functions i dont even use. It's because I understand it i didn't put in here. My bad. The full code looks like:

        #include <stdio.h>
        #include <stdlib.h>
        #include <ctype.h>
        #include <string.h>
        #include <conio.h>
        #define MAX 41

        int t, i, v1, v2;
        int top = -1;
        char seq[40];
        char rpn[40];
        char fin[40];
        int main()
        {
        printf("introduce the numbers and opreands in rpn \n");
        fgets(seq,40,stdin);

        t=strlen(seq);

        for (i=0;i<=t;i++)
        if (isdigit(seq[i]))
        {
        Push(rpn, seq[i]);
        }
        if (ispunct(seq[i]));

        v1=pop(rpn);
        v2=pop(rpn);
        
        if (seq\[i\] = '+');
            push(fin, v1+v2);
        if (seq\[i\] = '-');
            push(fin, v1-v2);
        if (seq\[i\] = '\*');
            push(fin, v1\*v2);
        if (seq\[i\] = '/');
            push(fin, v1/v2);
        

        printf(fin);
        }

        int isempty() {

        if(top == -1)
        return 1;
        else
        return 0;
        }

        int isfull() {

        if(top == MAX)
        return 1;
        else
        return 0;
        }

        int peek() {
        return seq[top];
        }

        int pop() {
        int data;

        if(!isempty()) {
        data = seq[top];
        top = top - 1;
        return data;
        }else {
        printf("Could not retrieve data, Stack is empty.\n");
        }
        }

        int push(int data) {

        if(!isfull()) {
        top = top + 1;
        seq[top] = data;
        }else {
        printf("Could not insert data, Stack is full.\n");
        }
        }

        L J D 3 Replies Last reply
        0
        • U User 12495620

          You are right, sorry, I even forgot to put them in the code! This is something a partner gave and explained to me. There are some functions i dont even use. It's because I understand it i didn't put in here. My bad. The full code looks like:

          #include <stdio.h>
          #include <stdlib.h>
          #include <ctype.h>
          #include <string.h>
          #include <conio.h>
          #define MAX 41

          int t, i, v1, v2;
          int top = -1;
          char seq[40];
          char rpn[40];
          char fin[40];
          int main()
          {
          printf("introduce the numbers and opreands in rpn \n");
          fgets(seq,40,stdin);

          t=strlen(seq);

          for (i=0;i<=t;i++)
          if (isdigit(seq[i]))
          {
          Push(rpn, seq[i]);
          }
          if (ispunct(seq[i]));

          v1=pop(rpn);
          v2=pop(rpn);
          
          if (seq\[i\] = '+');
              push(fin, v1+v2);
          if (seq\[i\] = '-');
              push(fin, v1-v2);
          if (seq\[i\] = '\*');
              push(fin, v1\*v2);
          if (seq\[i\] = '/');
              push(fin, v1/v2);
          

          printf(fin);
          }

          int isempty() {

          if(top == -1)
          return 1;
          else
          return 0;
          }

          int isfull() {

          if(top == MAX)
          return 1;
          else
          return 0;
          }

          int peek() {
          return seq[top];
          }

          int pop() {
          int data;

          if(!isempty()) {
          data = seq[top];
          top = top - 1;
          return data;
          }else {
          printf("Could not retrieve data, Stack is empty.\n");
          }
          }

          int push(int data) {

          if(!isfull()) {
          top = top + 1;
          seq[top] = data;
          }else {
          printf("Could not insert data, Stack is full.\n");
          }
          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Just move them to above `main` then. Or use forward declarations.

          1 Reply Last reply
          0
          • U User 12495620

            You are right, sorry, I even forgot to put them in the code! This is something a partner gave and explained to me. There are some functions i dont even use. It's because I understand it i didn't put in here. My bad. The full code looks like:

            #include <stdio.h>
            #include <stdlib.h>
            #include <ctype.h>
            #include <string.h>
            #include <conio.h>
            #define MAX 41

            int t, i, v1, v2;
            int top = -1;
            char seq[40];
            char rpn[40];
            char fin[40];
            int main()
            {
            printf("introduce the numbers and opreands in rpn \n");
            fgets(seq,40,stdin);

            t=strlen(seq);

            for (i=0;i<=t;i++)
            if (isdigit(seq[i]))
            {
            Push(rpn, seq[i]);
            }
            if (ispunct(seq[i]));

            v1=pop(rpn);
            v2=pop(rpn);
            
            if (seq\[i\] = '+');
                push(fin, v1+v2);
            if (seq\[i\] = '-');
                push(fin, v1-v2);
            if (seq\[i\] = '\*');
                push(fin, v1\*v2);
            if (seq\[i\] = '/');
                push(fin, v1/v2);
            

            printf(fin);
            }

            int isempty() {

            if(top == -1)
            return 1;
            else
            return 0;
            }

            int isfull() {

            if(top == MAX)
            return 1;
            else
            return 0;
            }

            int peek() {
            return seq[top];
            }

            int pop() {
            int data;

            if(!isempty()) {
            data = seq[top];
            top = top - 1;
            return data;
            }else {
            printf("Could not retrieve data, Stack is empty.\n");
            }
            }

            int push(int data) {

            if(!isfull()) {
            top = top + 1;
            seq[top] = data;
            }else {
            printf("Could not insert data, Stack is full.\n");
            }
            }

            J Online
            J Online
            jeron1
            wrote on last edited by
            #5

            Keep in mind that the c/c++ is case sensitive. I'm looking at this line in particular.

            Push(rpn, seq[i]);

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

            1 Reply Last reply
            0
            • U User 12495620

              You are right, sorry, I even forgot to put them in the code! This is something a partner gave and explained to me. There are some functions i dont even use. It's because I understand it i didn't put in here. My bad. The full code looks like:

              #include <stdio.h>
              #include <stdlib.h>
              #include <ctype.h>
              #include <string.h>
              #include <conio.h>
              #define MAX 41

              int t, i, v1, v2;
              int top = -1;
              char seq[40];
              char rpn[40];
              char fin[40];
              int main()
              {
              printf("introduce the numbers and opreands in rpn \n");
              fgets(seq,40,stdin);

              t=strlen(seq);

              for (i=0;i<=t;i++)
              if (isdigit(seq[i]))
              {
              Push(rpn, seq[i]);
              }
              if (ispunct(seq[i]));

              v1=pop(rpn);
              v2=pop(rpn);
              
              if (seq\[i\] = '+');
                  push(fin, v1+v2);
              if (seq\[i\] = '-');
                  push(fin, v1-v2);
              if (seq\[i\] = '\*');
                  push(fin, v1\*v2);
              if (seq\[i\] = '/');
                  push(fin, v1/v2);
              

              printf(fin);
              }

              int isempty() {

              if(top == -1)
              return 1;
              else
              return 0;
              }

              int isfull() {

              if(top == MAX)
              return 1;
              else
              return 0;
              }

              int peek() {
              return seq[top];
              }

              int pop() {
              int data;

              if(!isempty()) {
              data = seq[top];
              top = top - 1;
              return data;
              }else {
              printf("Could not retrieve data, Stack is empty.\n");
              }
              }

              int push(int data) {

              if(!isfull()) {
              top = top + 1;
              seq[top] = data;
              }else {
              printf("Could not insert data, Stack is full.\n");
              }
              }

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Member 12529159 wrote:

              This is something a partner gave...

              Ahh, the classic "copy & paste" bug. I'm usually the one to get chided for printing off code snippets and retype them line by line into my project. Consequently, I've never been a victim of this type of problem.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              1 Reply Last reply
              0
              • U User 12495620

                Hello, I am trying to code a program which evaluates expressions in reverse polish notations. For now, it should work only with numbers and operands ( +, -, * and / ). Here is the code I have written:

                #include #include #include #include #include #define MAX 41

                int t, i, v1, v2;
                int top = -1;
                char seq[40];
                char rpn[40];
                char fin[40];
                int main()
                {
                printf("introduce the numbers and opreands in rpn \n");
                fgets(seq,40,stdin);

                t=strlen(seq);

                for (i=0;i<=t;i++)
                if (isdigit(seq[i]))
                {
                Push(rpn, seq[i]);
                }
                if (ispunct(seq[i]));

                v1=pop(rpn);
                v2=pop(rpn);
                
                if (seq\[i\] = '+');
                    push(fin, v1+v2);
                if (seq\[i\] = '-');
                    push(fin, v1-v2);
                if (seq\[i\] = '\*');
                    push(fin, v1\*v2);
                if (seq\[i\] = '/');
                    push(fin, v1/v2);
                

                printf(fin);
                }

                int isempty() {

                if(top == -1)
                return 1;
                else
                return 0;
                }

                int isfull() {

                if(top == MAX)
                return 1;
                else
                return 0;
                }

                int peek() {
                return seq[top];
                }

                int pop() {
                int data;

                if(!isempty()) {
                data = seq[top];
                top = top - 1;
                return data;
                }else {
                printf("Could not retrieve data, Stack is empty.\n");
                }
                }

                int push(int data) {

                if(!isfull()) {
                top = top + 1;
                seq[top] = data;
                }else {
                printf("Could not insert data, Stack is full.\n");
                }
                }

                There are, I think, some big mistakes i can't solve by myself, since im a beginner in programming. I have searched for a solution on the internet, but i don't truly understand them. The idea of my code is that the user types some numbers and operands like this: 2 3 +. In this case, the program should add 2 and 3 in a stack and when it reads and operator, pop the lasts 2 numbers on the pile and do the corresponding opperation. When i build it I have this errors:

                ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
                C:\Users\Jordi\Documents\RPN.o:RPN.c|| undefined reference to `Push|
                ||error: ld returned 1 exit status|
                ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Member 12529159 wrote:

                There are, I think, some big mistakes

                Yes, this will not compile, and even if it did, none of this code is going to work. You are reading a string into the array seq and then using Push to copy characters back into the same array. You are also storing everything as characters but when you use Pop, you think you are rectrieving an integer. And you assume that each string will be two digits followed by a mathematical symbol.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups