473,908 Members | 3,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calculator

Hello all,
my very first perl program, a calculator that takes formulas;

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~

##
# CALC 1.0
# running: $perl calc.pl
# elmakko Feb 2005
##

use strict;
use warnings;

my $delimit_arr = "\n\t++++++++++ ++\n\n";
my $argc = scalar(@ARGV);
if($argc != 1)
{
print "\n/*\n * usage: perl calc.pl <formula>\n */\n\n";
exit -1;
}
my $ans = eval($ARGV[0]);
system "clear";
banner();
print "\t$ans \n";
print "$delimit_a rr";

sub banner()
{
print "\n\t********** **\n";
print "\t* CALC 1.0 *\n";
print "\t************ \n";
print "\n\n";
}

exit 0;

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

regards,
makko.

Jul 19 '05 #1
2 3752
makko wrote:
Hello all,
my very first perl program, a calculator that takes formulas;

First of all, this newsgroup is defunct. In the future, use
comp.lang.perl. misc, or perhaps even better, perl.beginners.
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~

##
# CALC 1.0
# running: $perl calc.pl
# elmakko Feb 2005
##

use strict;
use warnings;
Good!

my $delimit_arr = "\n\t++++++++++ ++\n\n";
my $argc = scalar(@ARGV);
No need for scalar: my $argc = @ARGV;
if($argc != 1)
{
print "\n/*\n * usage: perl calc.pl <formula>\n */\n\n";
exit -1;
}
Doesn't take into acount spaces in the formulas
my $ans = eval($ARGV[0]);
Ouch. Possibly very dangerous to eval() without any trapping of malicious
entries. Not as big an issue for somthing YOU are going to run on YOUR
computer, but definately not somthing you should get in the habit of doing.
system "clear";
"clear" is not recognized as an internal or external command.
banner();
print "\t$ans \n";
print "$delimit_a rr";

sub banner()
Don't use prototypes unless you absolutly need them.

sub banner
{
print "\n\t********** **\n";
print "\t* CALC 1.0 *\n";
print "\t************ \n";
print "\n\n";
}

exit 0;

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~


Not a horrible first script. Big plusses for use warnings and use strict.
You need to be careful using eval()s though. They may not work exactly
as you expect.

For instance, try the following; does this return the answer you expect?

perl calc.pl 01.5+01.5

Jul 19 '05 #2
That ruturns 165 instead of 3, thanx for the advice.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1938
by: mwh | last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a calculator with javascript. I can't get this to work: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <title>Calculator</title> <script language="Javascript"> <!-- Begin Hiding var total = 0
6
7311
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... #include <stdio.h> #include <stdlib.h>
3
5221
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
3
2087
by: Art | last post by:
Hi, In part of my application the user may need to do a simple arithmetic calculation in order to get the value to put in a text box. I was thinking that it would be good if I could display the Windows calculator and then put the result in the textbox for the user. Additionally, I'd like to keep the text of the calculation - if possible. Is any of this possible? How would I go about it?
3
15149
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows calculator. Ive made 4 classes 2 do this, my reasoning so it was easier to look through( when programming) rather than getting mixed up in my own code! My questions and problems: Ive been messing around with the windows look and feel,...
24
6359
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
19
4042
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*;
5
5768
Deathwing
by: Deathwing | last post by:
Hi everyone one I'm playing around with trying to make an expense calculator. I would like it so that the user can keep enter expenses until they have no more expenses. Then I would like for the program to give the users total expenses then subtract this from their total income and inform them how much they have left after monthly expenses. Any ideas ? this is what I've come up with thus far. # Expense calculator # This program calculates...
3
11886
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However, my teacher wants the operators to follow the algebraic order of operations by chaining multiple operations. Such as, 7 + 4 * 2= 15. The operatorListener is the ActionListener for the operator buttons. Thanks for any help you can give me. ...
3
2901
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of the last operator entered /* Digit entered as integer value i * Updates the value of input accordingly to (input * 10) + i */
0
9876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11339
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10915
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10537
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8096
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4772
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3357
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.