Last active
December 31, 2015 09:48
-
-
Save hwchiu/7968819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<fstream> | |
#include <cstdlib> | |
using namespace std; | |
#define NUM 100 | |
class Stackf | |
{ | |
private: | |
int top; | |
float array[NUM]; | |
public: | |
Stackf(){ | |
top=0; | |
} | |
void push(float item) | |
{ | |
array[top]=item; | |
top++; | |
} | |
int pop() | |
{ | |
if(top==0) | |
cout<<"Stack is empty"<<endl; | |
else | |
{ | |
float item; | |
top--; | |
item=array[top]; | |
return item; | |
} | |
} | |
bool isEmpty(){ | |
return top==0? true:false; | |
} | |
}; | |
class Stackc | |
{ | |
private: | |
int top; | |
char array[NUM]; | |
public: | |
Stackc(){ | |
top=0; | |
} | |
void push(char item) | |
{ | |
array[top]=item; | |
top++; | |
} | |
void pop() | |
{ | |
if(top!=0) | |
top--; | |
} | |
char getTop(){ | |
if(top!=0) | |
return array[top-1]; | |
return NULL; | |
} | |
bool isEmpty(){ | |
return top==0? true:false; | |
} | |
}; | |
bool isChar(char input){ | |
if(input=='+' || input=='-' || input=='*' || input=='/' ) | |
return true; | |
return false; | |
} | |
bool isDigit(char input){ | |
if(input=='0' || input=='1' || input=='2' || input=='3' || input=='4' || input=='5' || input=='6' || input=='7' || input=='8' || input=='9') | |
return true; | |
return false; | |
} | |
int getPriority(char input){ | |
if(input=='*' || input=='/') return 4; | |
else if (input=='+' || input=='-') return 3; | |
else if (input=='(') return 2; | |
else if (input==')') return 1; | |
} | |
int main() | |
{ | |
float sumf; | |
float tmp; | |
int point=0,divide=1; | |
//Read the file into some buffer. | |
ifstream fin("input1.txt"); | |
string input; | |
fin >> input; | |
//Do infix to prefix. | |
//Create two stack, one for float number, one for operator. | |
Stackf stackf ; | |
Stackc stackc ; | |
Stackc output; | |
for(int i=input.size()-1;i>=0;i--) | |
{ | |
//if it is a digit, we calculate | |
if(isDigit(input[i])) | |
{ | |
tmp = input[i]-'0'; | |
tmp*=divide; | |
sumf+=tmp; | |
divide*=10; | |
output.push(input[i]); | |
} | |
//if it is a '.', we rest the divide. | |
else if(input[i]=='.') | |
{ | |
sumf/=divide; | |
divide=1; | |
output.push(input[i]); | |
} | |
// if it is a operator, match the operator's priority | |
else if(isChar(input[i])) | |
{ | |
sumf=0; | |
divide=1; | |
//push to stack | |
if(stackc.isEmpty()) //如果是空白的,就放進去 | |
stackc.push(input[i]); | |
else //不是空白的話,只要遇到比我強的,就丟掉 | |
{ | |
while(getPriority(stackc.getTop()) > getPriority(input[i])){ | |
output.push(stackc.getTop()); | |
stackc.pop(); | |
} | |
stackc.push(input[i]); | |
} | |
} | |
else if(input[i]=='('){ //一路丟掉,直到遇到) | |
while(stackc.getTop()!=')'){ | |
output.push(stackc.getTop()); | |
stackc.pop(); | |
} | |
stackc.pop(); | |
} | |
else if(input[i]==')'){ //(直接放進去 | |
stackc.push(input[i]); | |
} | |
} | |
while(!stackc.isEmpty()){ | |
output.push(stackc.getTop()); | |
stackc.pop(); | |
} | |
while(!output.isEmpty()){ | |
cout<<output.getTop(); | |
output.pop(); | |
} | |
//Do infix to postfix. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment