Need help with my Calculator program.

R

Rafael

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;


}
 
T

Thomas Matthews

Rafael said:
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 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
 
J

James Hu

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
 
R

Rafael

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
 
J

Joona I Palaste

Rafael said:
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.
 
M

Mike Wahler

Rafael said:
(e-mail address removed) (Rafael) wrote in message

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
 
O

osmium

<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top