473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with my Calculator program.

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>

int main ()
{

float a, b;
char oper;
float outp;

printf("Welcome to calculator 1.0.\n");
printf("Please enter 2 or 3 numbers to evaluate: ");
scanf("%f %c %f", &a, &oper, &b);

switch (oper){
case '+':{
outp = (a + b);
break;
}
case '-':{
outp = (a - b);
break;
}
case '*':{
outp = (a * b);
break;
}
case '/':{
outp = (a / b);
break;
}
default:{
printf("**ERROR your %c not known**.", oper);
}
}

printf("Your answer is %f\n", outp);

system("PAUSE") ;
return 0;
}
Nov 13 '05 #1
6 7306
Rafael wrote:
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>

int main ()
{

float a, b;
char oper;
float outp;

printf("Welcome to calculator 1.0.\n");
printf("Please enter 2 or 3 numbers to evaluate: ");
scanf("%f %c %f", &a, &oper, &b);

switch (oper){
case '+':{
outp = (a + b);
break;
}
case '-':{
outp = (a - b);
break;
}
case '*':{
outp = (a * b);
break;
}
case '/':{
outp = (a / b);
break;
}
default:{
printf("**ERROR your %c not known**.", oper);
}
}

printf("Your answer is %f\n", outp);

system("PAUSE") ;
return 0;
}


When working with more than two numbers and one operator,
you will have to handle operator precedence and expressions.

For example, the expression 4 + 2 * 3
is _not_ evaluated as (4 + 2) * 3, but as 4 + (2 * 3).

You will need to input the line into a buffer, then parse
the buffer. A popular data structure for evaluating
expressions is a binary tree. For information on evaluating
expressions, consult news:comp.progr amming. For information
on I/O, see the C Faq below.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #2
On 2003-12-02, Rafael <sp***@rloteck. com> wrote:
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.


Consider picking up a copy of K&R2:

Brian W. Kernighan and Dennis M. Ritchie, _The C Programming Language_,
Second Edition, Prentice Hall, 1988, ISBN 0-13-110362-8, 0-13-110370-9.
-- James
Nov 13 '05 #3
sp***@rloteck.c om (Rafael) wrote in message news:<a5******* *************** ****@posting.go ogle.com>...
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>

int main ()
{

float a, b;
char oper;
float outp;

printf("Welcome to calculator 1.0.\n");
printf("Please enter 2 or 3 numbers to evaluate: ");
scanf("%f %c %f", &a, &oper, &b);

switch (oper){
case '+':{
outp = (a + b);
break;
}
case '-':{
outp = (a - b);
break;
}
case '*':{
outp = (a * b);
break;
}
case '/':{
outp = (a / b);
break;
}
default:{
printf("**ERROR your %c not known**.", oper);
}
}

printf("Your answer is %f\n", outp);

system("PAUSE") ;
return 0;
}


Can anyone help me on this program? ....: )

Rafael
Nov 13 '05 #4
Rafael <sp***@rloteck. com> scribbled the following:
Can anyone help me on this program? ....: )


We heard you the first time. Usenet is not a chat room and all that.
*If* someone wants to help you, he/she will eventually help you. Asking
over and over again only serves to irritate people, making them *less*
likely to help you.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
Nov 13 '05 #5

"Rafael" <sp***@rloteck. com> wrote in message
news:a5******** *************** ***@posting.goo gle.com...
sp***@rloteck.c om (Rafael) wrote in message

news:<a5******* *************** ****@posting.go ogle.com>...
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>

int main ()
{

float a, b;
char oper;
float outp;

printf("Welcome to calculator 1.0.\n");
printf("Please enter 2 or 3 numbers to evaluate: ");
scanf("%f %c %f", &a, &oper, &b);

switch (oper){
case '+':{
outp = (a + b);
break;
}
case '-':{
outp = (a - b);
break;
}
case '*':{
outp = (a * b);
break;
}
case '/':{
outp = (a / b);
break;
}
default:{
printf("**ERROR your %c not known**.", oper);
}
}

printf("Your answer is %f\n", outp);

system("PAUSE") ;
return 0;
}


Can anyone help me on this program? ....: )


Sure, but we won't do it for you. Read Thomas' reply again
(note the reminder about precedence), give it a try, and post
your code with your questions if/when you get stuck.

Hint:
........express ion....... op expression
expression op expression * 5
3 + 4
-Mike

Nov 13 '05 #6
Rafael writes:
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>

int main ()
{

float a, b;
char oper;
float outp;

printf("Welcome to calculator 1.0.\n");
printf("Please enter 2 or 3 numbers to evaluate: ");
scanf("%f %c %f", &a, &oper, &b);

<snip>

You really do not say enough so that anyone could help you, even if they
were willing. I think most of us have thought you were trying to come up
with a real and usable calculator, one that permits parenthesis. I have
come to the conclusion (based on the scanf above) that you only want to
solve *perfect* inputs of the form:

a + b * c

scanf will only permit a perfect user, a real calculator must have a string
of *characters* to work from. IOW the input typing must be perfect and the
user must be willing to accept the wrong answer sometimes, as in the case
above. It will be evaluated as (a+b)*c. The rules of mathematics want
a+(b*c).

There is a real calculator in K&R starting at about p. 73. I suspect you
are not up to, nor even need to, come up with such a thing. If you want a
usable calculator, start with pseudo code, not C.

To continue your current course think of revising the scanf to something
along the lines of

scanf("%f%c%f%c %f", &a, &op1, &b, &op2, &c);

BTW, change a and b to double, not float. That is the standard way of doing
things.

There are probably errors in there but you should get the idea. Think of
what you have so far as evaluating a sub-expression and returning a double
that shows the result. Put it in a function. Now call the function twice.
Display the answer. Done.


Nov 13 '05 #7

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

Similar topics

3
2570
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to use the calculator. "document.Calculate.Input is null or not an object" Can someone please tell me why this is?
3
5213
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
21
1994
by: Mandar Mitra | last post by:
Hello, I'd like to print floating point numbers without trailing zeros. I tried using %g, but now small numbers are printed in the %e style ("Style e is used if the exponent from its conversion is less than -4"). Is there some way to control this number (-4)? I.e. can I get printf to print something like 0.0000001, but no trailing zeros? Or should I do this by hand? Thanks,
13
3261
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
14
4969
by: arnuld | last post by:
Stroustrup starts chapter 6 with a programme for desk-calculator: here is a grammer for the langugae accepted by the calcualtor: program: END // END is end-of-input expr_list END expr_list: expression PRINT // PRINT is semicolon expression PRINT expr_list
1
6843
by: phjones | last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the following error message: --------------------Configuration: <Default>-------------------- C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\mortgageloan\src\mortgageloan.java:30: operator > cannot be applied to double,int ...
19
4033
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.*;
1
3333
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 two text boxes for a user to input two numbers. It also has a button for the user to get the result after adding these two numbers from input. Can somebody write such a program in C#? I think the program is small. How do I make it online so that...
9
3811
Deathwing
by: Deathwing | last post by:
Hey everyone, I have written a code that figures out a users total montly expenses. I have it working except for the fact that it does not add up their cumulative entries. For example, the code ask the user to enter their net montly income then an expense name and the cost of this expense it then ask them if they have another expense to enter if they hit no it calculates their monthly expenses by subtracting their net income from the expense and...
0
9498
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
10363
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
10172
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
9964
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...
1
7517
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
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.