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