473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

coding a simple calculator

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
=
the result is 15.

Iw would keep adding or subtracting... until the user enters '='

I want to loop this so that if the person would enter 'q' it would
quit.

Iam thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.

Dont post the whole code just need a little help getting started.

BTW: Iam still learning C from a book called C Primer Plus and I just
finished a chapter about if else switch() break. So I dont know much
but I think its enough for this simple project.
Nov 14 '05 #1
3 5182
* Xm******@verizon.net (Paul) wrote:
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
=
the result is 15.

Iw would keep adding or subtracting... until the user enters '='

I want to loop this so that if the person would enter 'q' it would
quit.

Iam thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.


Read a line - check if it is a number or a known operater and put it
on a stack or refuse the entry.

If the user enters '=', then go through the stack and pop the elements
from the stack. May be this is not the correct way to do it and you
have to do some operations on the stack already when a operator is
entered.
hth, mabu

--
Are you Anonymous? Where? ... I don't think so ...

[ devnull{at}chaosfactory{dot}org | http://www.chaosfactory.org/ ]
Nov 14 '05 #2
Paul writes:
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
=
the result is 15.

Iw would keep adding or subtracting... until the user enters '='

I want to loop this so that if the person would enter 'q' it would
quit.

Iam thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.


What you describe is an adding machine, not a calculator. It recognizes
integer numbers, +, - and =. You might assume an error free input to get
started. That is not a good assumption for a usable thing but I don't think
that is your goal, anyway. I think you could have an inner loop that has
(partial pseudocode):

do
get number
get operator
if (+ or -)
add to accumulator
else
display result note: assumed '='
set done flag to true
while not done

This does one series of adds and then quits, so it isn't quite what you
asked for. You could enclose this in a menu to get the 'q' thingy.
Providing for user errors will require that you parse the input yourself,
IOW the user inputs a string of characters and *you* do the conversion to
numbers etc.

If you decide to later do an actual calculator, I suggest you look into
reverse Polish notation (RPN). An RPN calculator is easier to code than the
usual (algebraic) calculator. K&R has a lengthy (for them) calculator
example.
Nov 14 '05 #3

"Paul" <Xm******@verizon.net> wrote in message
I want to make a simple calculator program but dont know where > to get started. This is not GUI but a simple terminal program.
I am thinking maybe of a while loop in the beginning like this

while (scanf("%d", &num) == 1)

but after this I dont know where to go.

The first thing is that you want to do all your calulations in double. This
is no harder than using ints.

You just keep a running total, as a handheld calculator does.

Your user has got to enter numbers and operations alternately.

int main(void)
{
double x = 0; /* this is your accumulator */
char op = '+'; /* hold the operation, '+', '*' etc */
/* set as a dummy + to start */
char buff[1024]; /* your input buffer */

/* print out a message saying how to use the program */
/* run this do while loop until you get a q */
do
{
fgets(buff, 1024, stdin);

/* if op is 0 we are expecting an operation */
if(op == 0)
{
/* read a character from the input, which should be +-/*= or q*/
/* set op, or print x if you have = */
}
/* if op is non-zero we are expecting a long */
else
{
double y;

/* read in y. Take action if not a number */
if(sscanf(buff, "%lf", &y) != 1)
{
/* check for 'q' here */
printf("Must have a number\n");
/* continue */
}
/* do the arithmetic */
switch(op)
{
case '+':
case '-':
case '/':
case '*':
default:
/* default should never happen */
}
op = 0;
}
}
while( !strchr(buff, 'q'))

return 0;
}
Nov 14 '05 #4

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

Similar topics

24
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...
19
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...
15
by: colinNeedsJavaHelp | last post by:
I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5 The program needs to compute this and display the result as "The...
1
by: Synapse | last post by:
Hello... We were asked to create a simple calculator program in our C++ subject by using loops only. i have a problem in creating a loop in the multiplication and division operation so please can...
1
by: Bl00dFox | last post by:
Hi I am making a simple program to calculate interest. At the beginning when the user has to pick 1 or 2 (to select simple or compound interest respectively), if the user enters a letter (eg, a)...
1
by: gzeng | last post by:
Hi Everybdy: I am a beginner in C#. I'd like to write a simple online calculator in C#. This calculator will ask a user to input two numbers and then add them and display the result. So, it has...
5
Cowbie
by: Cowbie | last post by:
Hello everyone, I'm very new to PHP and I'm keen to learn how it all works. I've been looking for help all day and reading tutorials etc which is how I came accross this forum - so here's hoping...
3
by: kimiraikkonen | last post by:
Hi there, I want to begin understanding how class libraries are written under VB.NET and how can i call them under my executable project. For example think an arithmetic calculator includes only...
6
by: NoviceJava | last post by:
I'm new to JAVA and need some help writing a simple calculator program. Here are the instructions: -expects espression with 2 operands together with either +, -, *, or / operator -espects only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.