help with calculator program.

This is a discussion on help with calculator program. within the C Programming forums, part of the General Programming Boards category; Hi guys. well im new in c programming and i want to program a formula calculator using stack. Ive read ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    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.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?
    Last edited by Adak; 09-02-2009 at 08:15 PM.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    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);
    }
    Last edited by sadr; 09-10-2009 at 01:22 AM. Reason: Misspelling

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 09-09-2009 at 09:51 AM.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sadr View Post
    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++.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sadr View Post
    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.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    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.

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    Lightbulb

    yes you are right. I never could understand it by myself!

    aha! at last I can get over this step !
    Last edited by sadr; 09-10-2009 at 11:44 AM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21