| | |
Reverse Polish Notation
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 57
Reputation:
Solved Threads: 1
So I have written a reverse polish notation calculator:
I now need to run the infix string " ~(((202%16) + (292/16)*16) ^292 " through my calculator...however the string is in infix, RPN takes postfix. I have been having trouble converting this expression to postfix and need help. The program itself is done I just want to test that input and don't quite understand how to make it work in postfix.
C Syntax (Toggle Plain Text)
#include <ctype.h> #include <stdio.h> #include <stdlib.h> #define NUMBER '0' #define MAXOP 100 int main () { int type; int op1, op2; char s[MAXOP]; while ((type = getop(s)) != EOF) switch (type) { case NUMBER: push(atoi(s)); break; case '^': push ((unsigned int) pop() ^ (unsigned int) pop()); break; case '~': push(~(unsigned int) pop()); break; case '+': case '*': if (type == '+') push(pop() + pop()); else push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': case '%': if ((op2 = pop()) != 0.0) { if (type == '/') push(pop() / op2); else { op1 = pop(); push(op1 - op2 * ((int) (op1/op2))); } } else printf("Error: Zero divisor!\n"); break; case '\n': printf("The answer is %d\n", pop()); break; default: printf("Error: Unknown command %s!\n", s); break; } return 0; }
Last edited by Ineedhelpplz; Oct 27th, 2009 at 10:33 am. Reason: fixed code
![]() |
Similar Threads
- Polish notation and traversals (Computer Science)
- leaves and nodes mathematical expression (C++)
- js calculator (JavaScript / DHTML / AJAX)
- Discrete Math (IT Professionals' Lounge)
- calculator program in C -help needed (C)
- binary integer logic (IT Professionals' Lounge)
- static variable (C)
- Help, i suppose (C++)
- Math problem in C (C)
Other Threads in the C Forum
- Previous Thread: socket alive after exe closes
- Next Thread: Structures And Unions
Views: 312 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student systemcall test testautomation unix user variable visualstudio voidmain() wab win32 win32api windows.h





