|
||||||
| General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I'm writing a really really simple calculator (Basically just for the heck of it) and I'm not sure exactly how to implement it. I was thinking of using Lex or something else for creating compilers, but I don't want to learn a new tool just for a fairly trivial program like this. I also thought of using straight C, but I honestly just don't like C (All my C programs fail randomly, it seems like it doesn't like me either). I've pretty much decided on writing a regex validator in Perl or Python (something like "/\d+ *[+\-*\/] *\d+/" so it doesn't try to solve "Hamburger") then passing that to some fast compiled language (like C or Pascal or something) whether by making a function that's accessible in the high level language or making the overarching program a shell script (I don't want to do this) and then finally sending the result to stdout. I might just do the whole thing in Perl or Python, which has the big advantage of making the whole thing one standalone program.
__________________
My other car return the first item in a list. Last edited by Uninverted; 03-30-2008 at 10:11 PM. |
| Sponsored Links |
|
|
|
|||
|
Quote:
EDIT: Nevermind, I did it in literally 10 lines of Python. I love Python.
__________________
My other car return the first item in a list. Last edited by Uninverted; 03-31-2008 at 05:21 PM. |
|
|||
|
Quote:
Code:
#!/usr/bin/python
import re #Import the regular expression module
pat = re.compile('\d+ *[+\-*\/] *\d+') #Set up the Regex pattern for a math problem
prob = raw_input() #Get the problem
if pat.match(prob):
print eval(prob) #Print the evaluated problem, but only if it matches the pattern
else:
print "Invalid math problem" #If it doesn't, then it isn't a math problem
Code:
eval(raw_input())
__________________
My other car return the first item in a list. Last edited by Uninverted; 03-31-2008 at 07:35 PM. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial - A Simple Stropwatch! | travy92 | VB Tutorials | 17 | 10-26-2008 11:10 AM |
| Basic Calculator | AfTriX | VB Tutorials | 3 | 02-29-2008 09:53 AM |
| Help with Square root and calculator program!!! | 123456789asdf | C and C++ | 10 | 12-02-2007 05:35 PM |
| Flex, bison multifunction calculator | annatsos | C and C++ | 1 | 01-04-2007 07:00 AM |
| Calculator program in C | roger | C and C++ | 4 | 07-04-2006 03:24 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |
Goal: 100,000 Posts
Complete: 98%