|
发表于 2013-10-25 20:23:39
|
显示全部楼层
#include <iostream>
#include <cstring>
using namespace std;
#define SIZE 100
char exp[SIZE] = {}; //保存逆波兰表达式
typedef struct{
char data[SIZE];
int top;
}SqStack;
typedef struct{
double data[SIZE];
int top;
}DB_SqStack;
void InitStack(SqStack * &s);
int Push(SqStack * &s , char e);
int Pop(SqStack * &s , char &e);
int judge(char *str);
void convert(char *str);
double calculate(char *exp);
void DB_InitStack(DB_SqStack * &s);
int DB_Push(DB_SqStack * &s , double e);
int DB_Pop(DB_SqStack * &s , double &e);
int Pow(int n);
int main(){
char str[SIZE] = {}; //初始化字符串
int tmp ;
double rst;
gets(str);
tmp = judge(str);
while(tmp == 0){
memset(str , 0 , sizeof(str)) ;
cout<<"The input is illegal,please input again!"<<endl;
fflush(stdin);
gets(str);
tmp = judge(str);
}
str[strlen(str)] = '#';
convert(str);
rst = calculate(exp);
cout<<"The answer is : "<<rst<<endl;
system("pause");
return 0;
}
void InitStack(SqStack * &s){ //初始化栈
s = new SqStack ;
s -> top = -1;
}
int Push(SqStack * &s , char e){ //进栈
if(s -> top == (SIZE-1)) return 0;
s -> top++;
s -> data[s->top] =e ;
return 1;
}
int Pop(SqStack * &s , char &e){ //出栈
if(s ->top == -1) return 0;
e = s -> data[s->top];
s->top--;
return 1;
}
int judge(char *str){
int i = 0;
int left(0) , right(0);
if(str[i] == '*'||str[i] == '/') return 0;
for(i = 0 ; i < strlen(str) ; i ++){
if(str[i] == '(') left++;
if(str[i] == ')') right++;
if(right > left) return 0;
if((str[i]>=0x00&&str[i]<=0x27) || str[i]==0x2C || str[i]>=0x3A)
return 0;
if((str[i]>=0x2A&&str[i]<=0x2F) && (str[i+1]>=0x2A&&str[i+1]<=0x2F))
return 0;
if((str[i]>=0x30&&str[i]<=0x39) && str[i+1]=='(')
return 0;
}
if(str[i-1]=='+' || str[i-1]=='-' || str[i-1]=='*' || str[i-1]=='/' || str[i-1]=='.')
return 0;
if(left != right) return 0;
return 1;
}void convert(char *str){//转换成逆波兰表达式
SqStack *s;
char no_value;
char StackTop ;
int count = 0 ;
InitStack(s);
Push(s,'#');
for(int i = 0 ; i < strlen(str) ; i ++){
//cout<<str[i];
StackTop = s->data[s->top];
if(str[i] >= 0x30 && str[i] <= 0x39 || str[i] == 0x2E){
exp[count] = str[i] ;
count ++;
}
else if((str[i]=='+'||str[i]=='-') && (StackTop=='#'||StackTop=='(')){
exp[count] = ' ' ;
count ++;
Push(s,str[i]);
}
else if((str[i]=='+'||str[i]=='-') && (StackTop=='+'||StackTop=='-'||StackTop=='*'||StackTop=='/')){
exp[count] = ' ' ;
count ++;
Pop(s,exp[count]);
count++;
Push(s,str[i]);
exp[count] = ' ';
count++;
}
else if((str[i]=='*'||str[i]=='/') && (StackTop=='+'||StackTop=='-'||StackTop=='#'||StackTop=='(')){
exp[count] = ' ' ;
count ++;
Push(s,str[i]);
}
else if((str[i]=='*'||str[i]=='/') && (StackTop=='*'||StackTop=='/')){
exp[count] = ' ' ;
count ++;
Pop(s,exp[count]);
count++;
Push(s,str[i]);
exp[count] = ' ';
count++;
}
else if(str[i]=='('){
if(count!=0){
exp[count] = ' ' ;
count ++;
}
Push(s,str[i]);
}
else if(str[i]==')'){
while(StackTop!='('){
exp[count] = ' ';
count ++;
Pop(s,exp[count]);
count ++;
StackTop = s->data[s->top];
}
Pop(s,no_value);
}
}
while(s->top!=0){ //表达式读入结束后,让栈中剩余元素出栈
exp[count] = ' ';
count ++;
Pop(s,exp[count]);
count++;
}
cout<<exp<<endl;
}
double calculate(char *exp){
DB_SqStack *s;
DB_InitStack(s);
double rst = 0;
int length = strlen(exp);
int count = 0 ;
for(int i = 0 ; i < length ; i ++){
if(exp[i] == ' '){
double a = 0 ;
for(int j = count ; j < i ; j ++){
a += (exp[j]-'0')*Pow(i-1-j);
}
DB_Push(s,a);
//cout<<a<<" ";
//count = i + 1 ;
while(exp[i]==' '){
i++;
}
i = i - 1 ;
count = i + 1;
}
if(exp[i] == '+'){
double a , b , c;
DB_Pop(s,a);
DB_Pop(s,b);
c = a + b;
//cout<<c<<endl;
DB_Push(s,c);
i++;
while(exp[i]==' '){
i++;
}
i = i - 1 ;
count = i + 1;
}
if(exp[i] == '-'){
double a , b , c;
DB_Pop(s,a);
DB_Pop(s,b);
c = b - a;
DB_Push(s,c);
i++;
while(exp[i]==' '){
i++;
}
i = i - 1 ;
count = i + 1;
}
if(exp[i] == '*'){
double a , b , c;
DB_Pop(s,a);
DB_Pop(s,b);
c = a * b;
//cout<<a<<" "<<b<<endl;
DB_Push(s,c);
i++;
while(exp[i]==' '){
i++;
}
i = i - 1 ;
count = i + 1;
}
if(exp[i] == '/'){
double a , b , c;
DB_Pop(s,a);
DB_Pop(s,b);
c = b / a;
DB_Push(s,c);
i++;
while(exp[i]==' '){
i++;
}
i = i - 1 ;
count = i + 1;
}
}
DB_Pop(s,rst);
return rst ;
}
void DB_InitStack(DB_SqStack * &s){
s = new DB_SqStack ;
s -> top = -1;
}
int DB_Push(DB_SqStack * &s , double e){
if(s -> top == (SIZE-1)) return 0;
s -> top++;
s -> data[s->top] =e ;
return 1;
}
int DB_Pop(DB_SqStack * &s , double &e){
if(s ->top == -1) return 0;
e = s -> data[s->top];
s->top--;
return 1;
}
int Pow(int n){
int rst = 1;
for(int i = 0 ; i < n ; i ++){
rst *= 10 ;
}
return rst ;
}
|
|