Jump to content

How to implement a simple calculator?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Uninverted

Uninverted

    Newbie

  • Members
  • PipPip
  • 11 posts
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.

Edited by Uninverted, 30 March 2008 - 06:11 PM.

My other car return the first item in a list.

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
What's wrong with C? Try C# - it's great, and really powerful. You can use any .NET language, and I'm happy.

#3
Uninverted

Uninverted

    Newbie

  • Members
  • PipPip
  • 11 posts

Xav said:

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.

Edited by Uninverted, 31 March 2008 - 01:21 PM.

My other car return the first item in a list.

#4
zorocke

zorocke

    Newbie

  • Members
  • PipPip
  • 11 posts
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? :)

#5
Uninverted

Uninverted

    Newbie

  • Members
  • PipPip
  • 11 posts

zorocke said:

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.

#!/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!

eval(raw_input())

Edited by Uninverted, 31 March 2008 - 03:35 PM.

My other car return the first item in a list.

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

Uninverted said:

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 :-)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums