Lost Password?


Go Back   CodeCall Programming Forum > Software Development > General Programming

General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-30-2008, 09:01 PM
Uninverted Uninverted is offline
Newbie
 
Join Date: Dec 2007
Posts: 11
Rep Power: 0
Uninverted is on a distinguished road
Default How to implement a simple calculator?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-31-2008, 01:03 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,606
Last Blog:
Web slideshow in JavaS...
Rep Power: 77
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: How to implement a simple calculator?

What's wrong with C? Try C# - it's great, and really powerful. You can use any .NET language, and I'm happy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-31-2008, 04:32 PM
Uninverted Uninverted is offline
Newbie
 
Join Date: Dec 2007
Posts: 11
Rep Power: 0
Uninverted is on a distinguished road
Default Re: How to implement a simple calculator?

Quote:
Originally Posted by Xav View Post
What's wrong with C? Try C# - it's great, and really powerful. You can use any .NET language, and I'm happy.
Not really an option; I use Linux, and this program's meant to be just a small Unix filter (works well with redirection & pipes). And this type of program goes fast enough to use a slow interpreted language for just about any of thew parts, if necessary; integer arithmetic might be the easiest thing for a computer to do, and I've already written the regex tester (just prints "Valid" or "Invalid" right now), and it's practically instant.

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-31-2008, 06:14 PM
zorocke zorocke is offline
Newbie
 
Join Date: Mar 2008
Posts: 11
Rep Power: 0
zorocke is on a distinguished road
Send a message via MSN to zorocke
Default Re: How to implement a simple calculator?

I am wondering what your code looks like because I am still very unfamiliar with Python. Do you think you could post those 10 lines?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-31-2008, 07:18 PM
Uninverted Uninverted is offline
Newbie
 
Join Date: Dec 2007
Posts: 11
Rep Power: 0
Uninverted is on a distinguished road
Default Re: How to implement a simple calculator?

Quote:
Originally Posted by zorocke View Post
I am wondering what your code looks like because I am still very unfamiliar with Python. Do you think you could post those 10 lines?
Sure, always glad to help a new pythonist.

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
But if you reduce it to the absolute minimum, you can do it in one line!

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-01-2008, 09:26 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,606
Last Blog:
Web slideshow in JavaS...
Rep Power: 77
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Wink Re: How to implement a simple calculator?

Quote:
Originally Posted by Uninverted View Post
Not really an option; I use Linux, and this program's meant to be just a small Unix filter (works well with redirection & pipes). And this type of program goes fast enough to use a slow interpreted language for just about any of thew parts, if necessary; integer arithmetic might be the easiest thing for a computer to do, and I've already written the regex tester (just prints "Valid" or "Invalid" right now), and it's practically instant.

EDIT: Nevermind, I did it in literally 10 lines of Python. I love Python.
Oh, all right then, I suppose Python is pretty cool. Did you know there's a library called Pygame, that you can use to create games using Python? I know Python's a tad slow, but Pygame's built on a C Game Library, so it's pretty fast.

Just thought it might be interesting. And the best part - you get to play your Python games when you've finished writing them!

Python + Speed + Game Creation = Pygame + Ultimate Joy

Xav :-)
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 05:48 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads