Witam!
Potrzebuję przerobić kalkulator rpn tak aby zastąpić instrukcję switch instrukcją if z funkcją strcmp
(
if(!strcmp(zmienna,'*'))
{
push(pop() * pop());
}
), z racji tego iż funkcja strcmp wymaga zmiennych jako const char* nie wiem jak to poprawnie zrobić.
Z góry dziękuję za pomoc.
Program :
#include <stdio.h>
#include <stdlib.h>
#define max 100
#define liczba '0'
int licz(char []);
void push(double);
double pop(void);
int main()
{
int zmienna;
double op2;
char s[max];
printf("Podaj wyrażenie w rpn:\n");
while ((zmienna = licz(s)) != EOF)
{
switch (zmienna)
{
case liczba:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("! Dzielenie przez zero ! \n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("Nieznany znak! \n", s);
break;
}
}
}
#define maxs 100
int el = 0;
double stos[maxs];
void push(double q)
{
if(el < maxs)
stos[el++]=q;
else
printf("Przepełnienie stosu !");
}
double pop(void)
{
if(el>0)
return stos[--el];
else
{
printf("Pusty stos");
return 0.0;
}
}
#include<ctype.h>
int pobcyf(void);
void ungetch(int);
int licz(char t[])
{
int i,c;
while((t[0] = c = pobcyf()) == ' ' || c =='\t')
;
t[1] = '\0';
i = 0;
if(!isdigit(c) && c!='.' && c!='-')
return c;
if(c=='-')
if(isdigit(c=pobcyf()) || c == '.')
t[++i]=c;
else
{
if(c!=EOF)
ungetch(c);
return '-';
}
if(isdigit(c))
while(isdigit(t[++i] =c =pobcyf()))
;
if(c=='.')
while(isdigit(t[++i] = c=pobcyf()))
;
t[i] = '\0';
if(c!=EOF)
ungetch(c);
return liczba;
}
#define tr 100
char buf[tr];
int pe = 0;
int pobcyf(void)
{
return (pe > 0) ? buf[--pe] : getchar();
}
void ungetch(int c)
{
if(pe >= tr)
printf("Zbyt wiele elementów \n");
else
buf[pe++] = c;
}