| | |
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: 311 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user visualstudio voidmain() wab win32 win32api windows.h





