上帝!喝酒不?
发表于 2013-10-25 12:00:56
强烈支持楼主ing……
xajh2010
发表于 2013-10-25 12:17:55
打酱油的 看看
xiakev123
发表于 2013-10-25 12:20:17
来看看是什么
shark_cf
发表于 2013-10-25 12:30:02
我只是路过打酱油的。
禁忌白
发表于 2013-10-25 12:40:43
强烈支持楼主ing……
unicornxiaodong
发表于 2013-10-25 12:52:26
强烈支持楼主ing……
DXT
发表于 2013-10-25 12:54:21
看看啦。。。。。。。。。。
铖子
发表于 2013-10-25 12:54:33
真是难得给力的帖子啊。
の祉崭之殇__
发表于 2013-10-25 13:05:23
强烈支持楼主ing……
paddx
发表于 2013-10-25 13:05:31
我只是路过打酱油的。
Captainfyc
发表于 2013-10-25 13:30:39
来看看~~~{:7_148:}
人间烟火
发表于 2013-10-25 19:25:44
淡定,淡定,淡定……
ζ_Y°シ
发表于 2013-10-25 19:34:31
强烈支持楼主ing……
wzw85718086
发表于 2013-10-25 19:40:51
为什么要回复呢
编程浪子
发表于 2013-10-25 19:41:41
初来咋到,看看。
牡丹花下死做鬼
发表于 2013-10-25 19:47:53
淡定,淡定,淡定……
孩子,跟我走、
发表于 2013-10-25 19:48:12
我只是路过打酱油的。
沾衣晨露
发表于 2013-10-25 19:51:00
强烈支持楼主ing……
mt880607
发表于 2013-10-25 20:07:58
真是被感动的痛哭流涕……
厍妹子
发表于 2013-10-25 20:22:46
#include <iostream>
#include <cstring>
using namespace std;
#define SIZE 100
char exp = {}; //保存逆波兰表达式
typedef struct{
char data;
int top;
}SqStack;
typedef struct{
double data;
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 = {}; //初始化字符串
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 = '#';
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 =e ;
return 1;
}
int Pop(SqStack * &s , char &e){ //出栈
if(s ->top == -1) return 0;
e = s -> data;
s->top--;
return 1;
}
int judge(char *str){
int i = 0;
int left(0) , right(0);
if(str == '*'||str == '/') return 0;
for(i = 0 ; i < strlen(str) ; i ++){
if(str == '(') left++;
if(str == ')') right++;
if(right > left) return 0;
if((str>=0x00&&str<=0x27) || str==0x2C || str>=0x3A)
return 0;
if((str>=0x2A&&str<=0x2F) && (str>=0x2A&&str<=0x2F))
return 0;
if((str>=0x30&&str<=0x39) && str=='(')
return 0;
}
if(str=='+' || str=='-' || str=='*' || str=='/' || str=='.')
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;
StackTop = s->data;
if(str >= 0x30 && str <= 0x39 || str == 0x2E){
exp = str ;
count ++;
}
else if((str=='+'||str=='-') && (StackTop=='#'||StackTop=='(')){
exp = ' ' ;
count ++;
Push(s,str);
}
else if((str=='+'||str=='-') && (StackTop=='+'||StackTop=='-'||StackTop=='*'||StackTop=='/')){
exp = ' ' ;
count ++;
Pop(s,exp);
count++;
Push(s,str);
exp = ' ';
count++;
}
else if((str=='*'||str=='/') && (StackTop=='+'||StackTop=='-'||StackTop=='#'||StackTop=='(')){
exp = ' ' ;
count ++;
Push(s,str);
}
else if((str=='*'||str=='/') && (StackTop=='*'||StackTop=='/')){
exp = ' ' ;
count ++;
Pop(s,exp);
count++;
Push(s,str);
exp = ' ';
count++;
}
else if(str=='('){
if(count!=0){
exp = ' ' ;
count ++;
}
Push(s,str);
}
else if(str==')'){
while(StackTop!='('){
exp = ' ';
count ++;
Pop(s,exp);
count ++;
StackTop = s->data;
}
Pop(s,no_value);
}
}
while(s->top!=0){ //表达式读入结束后,让栈中剩余元素出栈
exp = ' ';
count ++;
Pop(s,exp);
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 == ' '){
double a = 0 ;
for(int j = count ; j < i ; j ++){
a += (exp-'0')*Pow(i-1-j);
}
DB_Push(s,a);
//cout<<a<<" ";
//count = i + 1 ;
while(exp==' '){
i++;
}
i = i - 1 ;
count = i + 1;
}
if(exp == '+'){
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 - 1 ;
count = i + 1;
}
if(exp == '-'){
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 - 1 ;
count = i + 1;
}
if(exp == '*'){
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 - 1 ;
count = i + 1;
}
if(exp == '/'){
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 - 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 =e ;
return 1;
}
int DB_Pop(DB_SqStack * &s , double &e){
if(s ->top == -1) return 0;
e = s -> data;
s->top--;
return 1;
}
int Pow(int n){
int rst = 1;
for(int i = 0 ; i < n ; i ++){
rst *= 10 ;
}
return rst ;
}