xbubbax
23-03-2008, 14:57
Ho fatto questo esercizietto per testare l'algoritmo. Diciamo che può prendere solo numeri a una cifra, mi interessa sapere se è giusto l'algoritmo. Però stampa un risultato sbagliato, come mai?
#include <stdio.h>
#include <stdlib.h>
#include "funzioni_pile.h"
int Polacca(char s[]){
int i=0;
pilaPtr P=NULL;
char c;
int op1=0;
int op2=0;
int risultato=0;
while(c=s[i]){
i=i+1;
if(c!='+' && c!='*'){
if(c!='-' && c!='/'){
P=Push(P,c-'0');}}else{
op1=Pop(P);
op2=Pop(P);
switch(c){
case'+':P=Push(P,op1+op2);break;
case'-':P=Push(P,op1-op2);break;
case'/':P=Push(P,op1/op2);break;
case'*':P=Push(P,op1*op2);break;}}}
return Pop(P);}
int main(void){
char s[6]={'3','2','5','+','-'};
printf("%d\n", Polacca(s));
system("PAUSE");}
#include <stdio.h>
#include <stdlib.h>
#include "funzioni_pile.h"
int Polacca(char s[]){
int i=0;
pilaPtr P=NULL;
char c;
int op1=0;
int op2=0;
int risultato=0;
while(c=s[i]){
i=i+1;
if(c!='+' && c!='*'){
if(c!='-' && c!='/'){
P=Push(P,c-'0');}}else{
op1=Pop(P);
op2=Pop(P);
switch(c){
case'+':P=Push(P,op1+op2);break;
case'-':P=Push(P,op1-op2);break;
case'/':P=Push(P,op1/op2);break;
case'*':P=Push(P,op1*op2);break;}}}
return Pop(P);}
int main(void){
char s[6]={'3','2','5','+','-'};
printf("%d\n", Polacca(s));
system("PAUSE");}