LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2002, 10:32 PM   #1
ChrisRain
LQ Newbie
 
Registered: Sep 2002
Location: Brisbane Australia
Distribution: Linux 7.2
Posts: 8

Rep: Reputation: 0
Post C Calculator


Hi there,
I am looking for any code people are happy to supply in helping me to create a Calculator in C language.

There are a lot of Calculators written in C++, however I was wondering if anyone has any knowledge of a C Calc.
The calculator needs to not accept any other entry that that of integers only, therefor if 2b + 2b were entred an error would occur. Yet if 2 + 2 were entered, the obvious answer would be displayed. The calculator also needs to accept *, - , / as well.

look forward and thank you, to all help given.
 
Old 09-10-2002, 03:44 AM   #2
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
I remember this was an assignment problem in my first year. I did it in pascal using recursion with great difficulty. But later I came to know that this a standard problem and can be easily solved using a stack. The Idea is to convert the normal expression to postfix expr and evaluate it using a stack. Search google for more info. If you just want 2 variables to be handled at a time you can just use a switch statement to solve it.
--Sarin
 
Old 09-10-2002, 07:57 AM   #3
ChrisRain
LQ Newbie
 
Registered: Sep 2002
Location: Brisbane Australia
Distribution: Linux 7.2
Posts: 8

Original Poster
Rep: Reputation: 0
Thankyou Sarin, I'll see how I can work your advice into a working prog, if I can come up with one.

Regards
Chris
 
Old 09-10-2002, 12:50 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I didn't know that (postfix, stack ...)
Is it possible to handle precedences (i.e. do '*' and '/' before '+' and '-') as well with a stack? In other words, can it handle correctly somthing like this:

2 + 4 * (24-12)/4
 
Old 09-10-2002, 03:58 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
if you just read a line, it's quite easy (recursion may be needed to calculate expressions in () ). A calculator is a popular projects for students during programming courses, so you should be able to find examples (also ideas from C++ code).
 
Old 09-11-2002, 01:05 AM   #6
ChrisRain
LQ Newbie
 
Registered: Sep 2002
Location: Brisbane Australia
Distribution: Linux 7.2
Posts: 8

Original Poster
Rep: Reputation: 0
Here below is what I have found and worked with up to this point.

What I need to achieve with the program is error checking and reporting on the numbers, ie if 2b + 2 were entered, then an error would follow, in the case of this code the error is displayed only if the error is occuring with the use of the +, -, * & / key.

Any further thoughts on this exercise?

Feel free to include within the code below whatever you feel may work, and post back if you prefer.

Thank you,
Chris.


#include <stdio.h>

main() {
double value1, value2;
char operand, junk;

/*Print out some information about the program*/
printf("\nThis program is a simple calculator.\n");
printf("It is able to do the four standard opeartions.\n");
printf("Enter the equation in the following fomrat:\n");
printf("\t[first value][operand][second value]\n");
printf("type '0q0' to quit\n\n");

/*This loop keeps the user in the program untill he wants to
*quit which is accomplies by typing 0q0
*/
while(!(operand == 'q' || operand == 'Q')) {
/*print the prompt and wait for the user to enter an equation*/
printf(": ");
scanf("%lf%c%lf%c", &value1, &operand, &value2, &junk);

/*look for the operand entered and perform the operation asked
*for by the user. If operand is not +, - ,*, or / then
*print out an error
*/
switch(operand) {
case '+': {
printf("\t%lf\n", value1 + value2);
break;
}
case '-': {
printf("\t%lf\n", value1 - value2);
break;
}
case '/': {
printf("\t%lf\n", value1/value2);
break;
}
case '*': {
printf("\t%lf\n", value1 * value2);
break;
}
case 'q':
case 'Q': {
break;
}
default: {
printf("**error: operand %c not known**\n", operand);
}
}/*end switch*/

}/*end while*/
}/*end main*/
 
Old 09-11-2002, 01:27 AM   #7
ChrisRain
LQ Newbie
 
Registered: Sep 2002
Location: Brisbane Australia
Distribution: Linux 7.2
Posts: 8

Original Poster
Rep: Reputation: 0
Just looking at the program again myself, appreciate thoughts if possible. Is it possible to have some kind of "If" statement within the code along the idea of this kind of pseudocode:

Set value1 & value2 to Double
IF value1, value2 character Then
Print: Error "You must only have numbers for calculations, please try again."
Else
Continue Program

If that work, where and how would I put it?
Once again please feel free to insert it into the code, for trial.
Many thanks.
 
Old 09-11-2002, 02:59 PM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Hmm, you need to read them as strings. It's a better idea.
Or maybe just one string (before switch), then searching for errors and switch at the end.
Something like that (line is char[512]):
Code:
int i=0;
while((line[i]>='0') && (line[i]<='9')) i++;
if((line[i]=='+')||(line[i]=='-')||(line[i]=='*')||(line[i]=='/')) i++;
else  {
      printf("You must only have numbers for calculations, please try again.");
      return;
}
while((line[i]>='0') && (line[i]<='9')&&(line[i]!='\0') i++;
Of course, it's not perfect. It won't handle spaces before a number and +-*/ and it won't handle brackets. But I suppose it's now easier.
Oh, and you need to add a function to change the numbers (in strings) into integers or floats.

Last edited by Mara; 09-11-2002 at 03:00 PM.
 
Old 09-11-2002, 08:48 PM   #9
ChrisRain
LQ Newbie
 
Registered: Sep 2002
Location: Brisbane Australia
Distribution: Linux 7.2
Posts: 8

Original Poster
Rep: Reputation: 0
Cool Thanks Mara

I will see what I can work out with your advice on the coding.

Regards

Chris.
 
Old 09-12-2002, 07:20 AM   #10
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
If you want to avoid spaces, you can do so in the begining.
( I haven't seen your code fully. So consider this as the code only to remove white spaces. )

i=0;
j=0;
while ( line[i] != '\0' )
{
if ( !isspace(line[i]) )
{
line2[j]=line[i];
j++;
}
i++;
}
line2[j]='\0';

And then make use of line2 in place of line.
--Sarin
 
Old 09-13-2002, 09:15 AM   #11
ChrisRain
LQ Newbie
 
Registered: Sep 2002
Location: Brisbane Australia
Distribution: Linux 7.2
Posts: 8

Original Poster
Rep: Reputation: 0
So by white space am I to take it you mean a (-> <-) space as indicated between the arrows above. The code you have given me will diregard the code and allow the calculator to function with out error on a space between integer and operand?

Well thankyou and I'll see where I can include it best for operating the program.

Regards Chris.
 
Old 03-26-2007, 11:19 PM   #12
Kurz16
LQ Newbie
 
Registered: Mar 2007
Location: Cebu, Philippines
Posts: 1

Rep: Reputation: 0
:cry: waaaa! I have the same proj T_T

i have finished the computations for simple numbers and numbers with functions... now my problem is how do i make the program compute first for the multiplication(*) division(/) before addition(+) subtraction(-)? Please.. i need an idea.. thanx advance ^^
 
Old 03-27-2007, 02:12 AM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
***Another thread back from the dead***
The solution will depend upon what is expected of you.
If prefix notation has been discussed then use that along with a stack.
If none of that has been discussed then you are probably expected to use a cunning combination of if statements and function calls.
 
Old 03-27-2007, 02:33 AM   #14
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
Quote:
Originally Posted by Kurz16
i have finished the computations for simple numbers and numbers with functions... now my problem is how do i make the program compute first for the multiplication(*) division(/) before addition(+) subtraction(-)? Please.. i need an idea.. thanx advance ^^
I think that starting your own thread might help. You might also consider how you picture the operator precedence working - maybe a few test cases. Also, multiple passes and/or parenthesis may be useful mechanisms.

Edit: I guess I took a while to page through the old thread and post my reply. The post by [graemef] should point you in the right direction.

Last edited by nmh+linuxquestions.o; 03-27-2007 at 02:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need a calculator Starch Programming 5 02-14-2005 06:06 PM
shell calculator DropSig Linux - Newbie 3 11-01-2004 09:18 PM
A Calculator......... 320mb Programming 0 08-13-2004 04:55 PM
TI calculator adam_boz Linux - General 1 08-22-2002 10:15 AM
Calculator KyleYankan Linux - Software 6 08-03-2002 01:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration