-
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
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 :o
-
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.
-
Hmm...is there any good tutorial on stacks?
-
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?
-
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.
-
thanks, Ill try to implement that logic into some basic(limited) code.