J'ai un examen final demain dans les structures de données 'LAB'!Code d'évaluation de Postfix Utilisation de Stack
J'ai un problème lorsque je lance ce code! Ce code évalue l'expression de postfix. C'est tellement simple ... mais je ne sais pas pourquoi quand je le lance et essaie de diviser par exemple: 6/2. il imprime 2 au lieu de 3!
Les + et - et * tous sont corrects mais le problème avec la division! Pensez-vous que le problème avec la bibliothèque dans mon compilateur !! J'utilise Code :: Blocks et Visual C++.
Pourriez-vous m'aider s'il vous plaît? :(
Le code:
#include<iostream>
using namespace std;
#include<math.h>
#include<conio.h>
#define stacksize 6
struct stacktype{
float data[stacksize];
int top;
};
void push(stacktype*s, float d){
if (s->top < 6) {
s->data[s->top]=d;
s->top++;
}
}
float pop(stacktype*s){
if (s->top != 0){
s->top--;
return s->data[s->top];
}
return 0;
}
float oper(char symbol, float op1, float op2){
switch (symbol){
case '+': return (op1+op2);break;
case '-':return (op1-op2);break;
case '*':return (op1*op2);break;
case '/':return (op1/op2);break;
default: cout<<"illegal operation.......\n";
}
return 0;
}
int main() {
float op1,op2,symb,value;
char symbol;
stacktype *s;
s= new stacktype;
s->top=0;
cout<<"Enter The Postfix Expression To Evaluate:: \n\n";
cin>>symbol;
while (symbol != '.') {
if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'){
op2=pop(s);
op1=pop(s);
value=oper(symbol,op1,op2);
push(s,value);
}
else {
if (symbol == '0') symb =0 ;
else if (symbol == '1') symb = 1;
else if (symbol == '2') symb =2 ;
else if (symbol == '3') symb =3 ;
else if (symbol == '4') symb = 4;
else if (symbol == '5') symb =5 ;
else if (symbol == '6') symb =6 ;
else if (symbol == '7') symb =7;
else if (symbol == '8') symb =8 ;
else if (symbol == '9') symb =9 ;
push(s,symb);
}
cin>>symbol;
}
value=pop(s);
cout<<"The Value:: \n"<<value<<endl;
return 0;
}
Pourquoi utilisez-vous 'break' après l'instruction' return'? – Rakib
Vous entrez l'opérande en premier (c'est-à-dire "/ 6 2.") vous devez entrer l'opérande en dernier (c'est-à-dire, "6 2 /.") – 101010
c'est juste une syntaxe Switch! – Mariah