By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

Need help with my Calculator program.

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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.programming. 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.learn.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

P: n/a
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

P: n/a
sp***@rloteck.com (Rafael) wrote in message news:<a5**************************@posting.google. 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

P: n/a
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.helsinki.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

P: n/a

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

news:<a5**************************@posting.google. 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:
........expression....... op expression
expression op expression * 5
3 + 4
-Mike

Nov 13 '05 #6

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.