help me
i want to make infix to prefix program but this code is that two operand is success but more 3 operand fail plz help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
stack.cpp
#include <iostream>
#include "stack.h"
using namespace std;
Stack::Stack(int stackCapcity){
capacity=stackCapcity;
stack=new char[capacity];
top=-1;
}
bool Stack::IsEmpty(){
return top==-1;
}
char Stack::Top(){
if(IsEmpty())throw "Stack is empty";
return stack[top];
}
void Stack::Push(char item){
stack[++top]=item;
}
void Stack::Pop(){
if(IsEmpty())throw "Stack is empty";
top--;
}
main.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main(void){
string line;
char propmpt;
int length;
int i;
while(getline(cin,line)){
length=line.length();
Stack s(length);
i=0;
if(line.at(0)=='+'){
i++;
}
if(line.at(0)=='-'){
cout<<line.at(0);
i++;
}
for(;i<length;i++){
propmpt=line.at(i);
if(propmpt>47&&propmpt<58){
cout<<propmpt;
}
else{
if(propmpt=='+'||propmpt=='-'){
if(s.IsEmpty()){
s.Push(propmpt);
}
else
{
while(s.IsEmpty()==false||s.Top()!='(')
{
cout<<s.Top();
s.Pop();
}
s.Push(propmpt);
}
}
else if(propmpt=='*'||propmpt=='/'){
if(s.IsEmpty()){
s.Push(propmpt);
}
else
{
while(s.IsEmpty()!=false||s.Top()!='+'||s.Top()!='-'||s.Top()!='('){
cout<<s.Top();
s.Pop();
}
s.Push(propmpt);
}
}
else if(propmpt=='('){
if(s.IsEmpty()){
s.Push(propmpt);
}
else
{
s.Push(propmpt);
}
}
else if(propmpt==')'){
while(s.Top()!='('){
cout<<s.Top();
s.Pop();
}
s.Pop();
}
if(line.at(i+1)=='+'){
i++;
}
if(line.at(i+1)=='-'){
cout<<"-";
i++;
}
}
}
while(s.IsEmpty()==false){
cout<<s.Top();
s.Pop();
}
cout<<endl;
}
}
|
What is your Problem?
What does not work?
What do you want to accomplish?
Last edited on
Topic archived. No new replies allowed.