Potrzebuję przerobić ten program w taki sposób, żeby wprowadzając formułkę np, "6+(4-1)*2-3/(7+5)=" wynik wyniósł 11.75. Strasznie pilne, zadanie na wtorek.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int pos;
char A[40];
}Stack_Type; //wskaznik do next
void Push (Stack_Type *, char); //dodanie elementu do stosu
char Pop (Stack_Type *); //zdjecie elementu ze stosu
char Peep (Stack_Type *); //usuwa objekt ze stosu i wysyla inf. ze stos jest pusty
int Prec (char); // funkcja opisujaca priprytety operatorow
int main (void)
{
Stack_Type X={0}, Y= {0};
int ch;
int i;
do {
ch=getchar();
switch (ch)
{
case'(':
Push (&Y,ch);
break;
case ')':
while (Peep (&Y)!='(')
Push (&X, Pop (&Y));
ch=Pop (&Y);
break;
case '+': case '-': case '*':case '/': case '=':
while(Peep (&Y)!=0 && Prec(ch)<=Prec(Peep(&Y)))
Push(&X, Pop(&Y));
Push (&Y,ch);
break;
default:
if (ch>= '0' && ch <= '9')
Push (&X, ch);
}
}
while (ch!= '=');
for (i=0; i<X.pos; ++i)
printf("%c", X.A[i]);
printf ("\n");
scanf("%d",&i);
return 0;
}
void Push (Stack_Type *q, char c) //trzeba ja jakos zmodyfikowac xd?
{
if ( q-> pos<40 )
q->A[q->pos++]=c;
}
char Pop (Stack_Type *q)
{
if ( q->pos>0)
return q-> A[--q -> pos];
else
return 0;
}
char Peep (Stack_Type *q)
{
if (q -> pos >0)
return q-> A[q-> pos -1];
else
return 0;
}
int Prec (char c)
{
switch (c)
{
case'=' : return 0;
case '(': return 1;
case '+': case '-': return 2;
case '*':case '/': return 3;
default: printf ("\n wybor");
}
system("PAUSE");
return 0;
}