# help with calculator program.

• 09-02-2009
help with calculator program.
Hi guys. well im new in c programming and i want to program a formula calculator using stack.
Ive read in wikipedia about it algorithm but i really dont know how can i create a stack in c and whether i should use arrays instead of it or define structure or ...
can anyone guide me about it ? thanks alot.
• 09-02-2009
sean
Quote:

formula calculator
What do you mean by formula calculator? Do you mean a program that solves equations? Perhaps you could provide some examples of what types of problems you want the calculator to solve.

Quote:

using stack
When you say stack, do you mean the Operating System's stack? If so, then you just need to read some examples of how to use malloc() and free(). If you're talking about implementing a stack data structure, then you need to be more specific. That's a very general problem and we need more details before we can point you in the right direction.
• 09-02-2009
I suggest using either Reverse Polish Notation or postfix for your implementation, and using an array as your stack.

Although the recursive types are elegant, I'd recommend the iterative logic for anyone not too familiar with the subject.

Wikipedia has a good write up on RPN and postfix, and there are plenty of other examples you can google, as well.

Post up your code when and if you do get stuck.

Be sure and use [code[ (but reverse the last squared bracket), [/code[ tags around your code, and remember to indent it, OK?
• 09-09-2009
i came back!
Sean, in fact this calculator can get a mathematical term like 3+4*5/sin30 ! according to RPN at first program should put operands and operators in two different stacks

dear Adak I started writing this program, getting the string of numbers and operators and putting each in two arrays as stack, but my program during execution crashes . can any one check it ? Ive used "ispunct" function to recognize my operators.

another question is that how can i omit spaces from array?

Code:

```#include <stdio.h> #include <iostream.h> #include <ctype.h> #include <string.h> #define length 20 int main() {                 char s[length];         char adad[10];         char alamat[10];         int dec;         cout << "Enter a string" << endl;         //read in the users data from the input stream and ends by enter         cin.get(s,'\n');         //Display a message made up of two strings and the user data         cout << "ha ha! you entered the string "  << s << endl;                         /*************initializing arrays************/         for (int j=0 ; j<length ; j++){                 if (ispunct(s[j])){                          for (int k=0;k<10;k++)                                 adad[k]=s[j];                 }                 else {                         for (int l=0;l<10;l++)                                 alamat[l]=s[j];                 }         }         /**********preview arrays*************/         for (int m=0;m<10; m++)                 printf("%c" , adad[m]);         for (int n=0; n<10; n++)                 printf("%c" , alamat[n]);         scanf("%d" , dec);         printf("finally %d" , dec);         return(0); }```
• 09-09-2009
Not to be picky, but you're using C++, and I have no idea how to work with cin. :(

May I suggest using

Code:

```char Array[50]; fgets(Array, sizeof(Array), stdin);```
Then, pick out what you want from the Array, using sscanf() or "walking" through it, and putting ONLY your operators into the stack for operators, and ONLY your numbers into the numbers stack.

So you have no spaces to get rid of (hint: sscanf() will skip spaces when it's looking for some data items).

If you want your program in C++, just say so, and I'll shut up. If it should be a C program, then be sure the program has a c filename extension, and not a cpp extension. Otherwise, you'll be accidentally working with the wrong compiler (C++ instead of C).

You may need to check your compiler and/or workspace options to see what compiler is set to work with what filename extensions. The above is the normal default, but you can set it up to do either:

1) always use C (with any extension), or

2) always use C++ (with any extension), or

3) use C compiler only with filenames that end with c, and C++ for filenames that have the cpp extension.

I recommend initializing your array stacks to '\0', for char's, and 0's for numbers, first.

THEN put in your operators and numbers. You'll see the errors, quite easily.

I think it's much easier also, if you use the common terms pop() and push() (or off and on, if you prefer).

Say you are going through the string, and find an operator - then push() that operator onto the stack. In the push() function, you will

1) first move all the current stack down (up in index number), by one. You'll do that from the highest index number, to the lowest, not the other way around, so no temporary variable is needed.

2) Then you'll push the newest value, into the array[0] position.

pop() will work about the same, but in reverse, you'll call pop() when you need to retrieve the next operator or number.

1) You'll pop off the top (lowest index number) item in the array, and then

2) You'll move all the lower items, (which have a higher index number), up one, decrementing by one.

Think of the spring loaded plate holders in a restaurant. Perfect imagery for a first in, last out, stack.
• 09-09-2009
BEN10
Quote:

i camed back!
Sean, in fact this calculator can get a mathematical term like 3+4*5/sin30 ! according to RPN at first program shud put operands and operators in two different stacks

dear Adak I started writing this program, getting the string of numbers and operators and putting each in two arrays as stack, but my program during execution crashes . can any one check it ? Ive used "ispunct" function to recognize my operators.

another question is that how can i omit spaces from array?

Code:

```#include <stdio.h> #include <iostream.h> #include <ctype.h> #include <string.h> #define length 20 int main() {                 char s[length];         char adad[10];         char alamat[10];         int dec;         cout << "Enter a string" << endl;         //read in the users data from the input stream and ends by enter         cin.get(s,'\n');         //Display a message made up of two strings and the user data         cout << "ha ha! you entered the string "  << s << endl;                         /*************initializing arrays************/         for (int j=0 ; j<length ; j++){                 if (ispunct(s[j])){                          for (int k=0;k<10;k++)                                 adad[k]=s[j];                 }                 else {                         for (int l=0;l<10;l++)                                 alamat[l]=s[j];                 }         }         /**********preview arrays*************/         for (int m=0;m<10; m++)                 printf("%c" , adad[m]);         for (int n=0; n<10; n++)                 printf("%c" , alamat[n]);         scanf("%d" , dec);         printf("finally %d" , dec);         return(0); }```

You are mixing C with C++.
• 09-09-2009
tabstop
Quote:

i camed back!
Sean, in fact this calculator can get a mathematical term like 3+4*5/sin30 ! according to RPN at first program shud put operands and operators in two different stacks

You must do nothing of the sort. 7 8 9 10 + - * and 7 8 + 9 - 10 * (or, in infix 7*(8-(9+10)) and ((7+8)-9)*10) are very different expressions that would give you the same "stacks" if you did them this way. You need to process operators as they happen.
• 09-10-2009
Quote:

1) always use C (with any extension), or

2) always use C++ (with any extension), or

3) use C compiler only with filenames that end with c, and C++ for filenames that have the cpp extension.
Your help is much appreciated, I modified code and omit c++ codes. adak your suggestions are so useful.
this code has problem again, after initializing number array, this array changes to 0 again! ...
Code:

```#include <stdio.h> #include <conio.h> #include <ctype.h> #include <string.h> #define LENGTH 20 int main() {                 char s[LENGTH] = {'\0'};         char number[10] = {'\0'};         char operation[10]= {'\0'};                         /* read in the users data and ends by enter */         printf("enter mathematical term: \n");         gets(s);         printf("printing s\n");         for (int i=0 ; i<LENGTH ; i++){                 printf("%c" , s[i]);                }         printf("\n");         /*  initializing arrays  */         int k=0,l=0;         for (int j=0 ; j<LENGTH ; j++){                 if (('0'<= s[j]) && (s[j]<= '9'))                 {                         number[k] = s[j];                         printf("%c\n" , s[j] ); //to see                         k++;                 }                                                 else                 {                                                        operation[l]=s[j];                         l++;                 }         }         /*  preview arrays  */         printf("printing number\n");         for (int m=0 ; m<10; m++)                 printf("%c" , number[m]);         printf("\n");         printf("printing operation\n");         for (int n=0; n<10; n++)                 printf("%c" , operation[n]);         printf("\n");                 return(0); }```
(wish they would understand this project is heavy for a beginner :( )
thanks.
• 09-10-2009
BEN10
I dont know what you're trying to achieve but at first glance your array 's' is of 20 length, but operation is of smaller length, this will create problem coz your j loop is running beyond the size of the operation array, so make it of the same size as 's' i.e declare as:
Code:

`char operation[20]={'\0'};`
Besides it dont use gets.
• 09-10-2009