RPN calc

This is a discussion on RPN calc within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> void help(void); float sum(float plus[]); int operacija(char *arg); int main(int argc, char *argv[]) { int ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    RPN calc

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    void help(void);
    float sum(float plus[]);
    int operacija(char *arg);
    int main(int argc, char *argv[])
    {
        int i;
        int j=0;
        int z=0;
        int st_stevk=0;
        float sklad[20]={0.};
        float plus[20]={0.};
        float minus[20]={0.};
        float rez_plus=0;
        float rez_vsota;
        float rez_produkt;
        float rez_temp1;
    
        if(argc<4)
        {
             help();
    	     exit(EXIT_FAILURE);
    	}
    	 
    	 
       for(i=1;i<argc;i++)  
       {    
          if(atof(argv[i])>=0.001&&atof(argv[i])<=9999.999)
          {
    	     sklad[j]=atof(argv[i]);	  
    	     j++;
    	     st_stevk++;
    	     continue;
    	  }
    	  
          if(operacija(argv[i]))
    	  {
    	     z=j-st_stevk;
    	     switch(*argv[i])
    	     { //ascii kode od operatorjev
    	     
    	         case '+': 
    	                for(i=0;i<st_stevk;i++)
    	                {
    			           plus[i]=sklad[z];
    			           z++;			  
    			        } 	   
    			        break;
    	         case '-':
    		 	 for(i=0;i<st_stevk;i++)
    			  {
    			            minus[i]=sklad[z];
    				    z++;
    				 }
    				 break;
    		     default:
    		            break;
    	        }
    		
    	         rez_plus=sum(plus);
    		  printf("%f\n",sum(plus));
    
    		   
    		 
    		  	 
    	     }
    	     st_stevk=0;
    	     
          }
          
          return 0;
       }
      
    
    void help(void) 
    {
        printf("\n");
        printf("%s","Vpisati moras vsaj tri argumente!\nPrimer: ./sedma 3 4 +,  pri tem  pa\n");
        printf("%s", "mora biti operator vedno za zadnjim\n");
        printf("%s","operandom, ki ga operacija zajema\n");
    }
    
    float sum(float plus[])
    {
      int i;
      float rez=0;
      for(i=0;i<20;i++)
        {
           rez=rez + plus[i];
          
          }
          
          return rez;
         }
         
     float sub(float minus[])// dont bother this method,ill change it
     {
        int i=0;
        int j=i+1;
        float rez=minus[i]-minus[j];
        int z=i+2;
        while(isdigit(minus[i]))
         {
           rez=rez-minus[z];
           i++;
           z++;
           }
           return rez;
          }
          
     int operacija(char *arg)
     {
       if ((*arg=='+')||(*arg=='-'))
          return 1;
       else 
         return 0;
      }
    I have stumbeled on a little problem trying to make a simple rpn calculator.
    A rpn calculator first takes the operands and then the operator. For instance if I would type in (as a command line args-->in my prog) 3 4 + would mean 3+4...and so on. My program isn't finished yet and I have only implemented the plus ('+') operation. But here is the problem. If I type some args...for example
    Code:
     3.4 4.5 3.4 +
    the calculator will sucessfully calculate the sum of this numbers and output it( never mind for 20x time output, I will correct that), but the problem is , what if I want to continue?
    Code:
     3.4 4.5 3.4 + 2.3 3.4 5.5 +
    Then it would only output the sum of the numbers before the first '+' operator and ignore the others.

    Maybe Im doing this completly wrong, but I think that my logic is fine, I just dont know how to put it into practice

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you use a stack? Push until you hit an operator, then pop until it's empty. Repeat until you run out of input.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Hmm...is there any good tutorial on stacks?

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Hmm...is there any good tutorial on stacks?

    I figured out what the problem is , but Im still looking the way to repair it.

    Question about stack: Do I need to write the functions pop and push or is this already defined?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, just Google for one. Anyway, in C there is no standard stack library. Stacks are an abstract, and can be implemented in many ways. One of the simples, though limited for size, is to simply use an array, and one variable to store the "top", or where you are in the stack. Then basicly the 'push' function stores something at the current top, and then increments the top. (Or reverse that order, either way.) The 'pop' function does the reverse. Fetch the item, and decrement the 'top' variable. You can do a 'peek', which simply looks at the top, but doesn't decrement 'top' variable.

    Anyway, you can use linked lists, or whatever you like. The concept is the same as piling up pieces of paper on your desk. You can only get to the top one, and you remove them or add them one at a time.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    thanks, Ill try to implement that logic into some basic(limited) code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPN Calc
    By zensatori in forum C Programming
    Replies: 5
    Last Post: 06-11-2007, 01:38 AM
  2. RPN & Functions
    By Cactus_Hugger in forum Tech Board
    Replies: 3
    Last Post: 09-06-2006, 01:21 PM
  3. Calc age use Date of PC
    By miziri in forum C++ Programming
    Replies: 12
    Last Post: 03-18-2006, 04:01 AM
  4. Question regarding RPN and error detection
    By Roule in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 01:12 AM
  5. Basic Calc
    By Universe in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2002, 10:07 PM

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