conquer3
发表于 2013-10-25 18:23:49
淡定,淡定,淡定……
▲_禦焱_2amor〃
发表于 2013-10-25 18:29:25
我只是路过打酱油的。真是被感动的痛哭流涕……我只是路过打酱油的。
更替的四季
发表于 2013-10-25 18:38:18
真是难得给力的帖子啊。
zxbccc1
发表于 2013-10-25 18:46:20
真是难得给力的帖子啊。
寻找自己的舞台
发表于 2013-10-25 19:21:07
楼主加油,鱼C加油!我们都看好你哦!
殇丶逝
发表于 2013-10-25 19:35:03
真是难得给力的帖子啊。
摸三个
发表于 2013-10-25 19:38:12
激动人心,无法言表!
影子
发表于 2013-10-25 20:19:30
激动人心,无法言表!
厍妹子
发表于 2013-10-25 20:23:39
#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 ;
}
厍妹子
发表于 2013-10-25 20:26:03
我整个人都不好了……
青草
发表于 2013-10-25 20:27:56
楼主加油,鱼C加油!我们都看好你哦!
so。
发表于 2013-10-25 20:52:59
我只是路过打酱油的。
天玄邪帝
发表于 2013-10-25 23:27:31
真是难得给力的帖子啊。
XXX的XXX
发表于 2013-10-26 00:10:00
{:7_148:}和越南版电视剧比,我们的真心碉堡了
xhbuming
发表于 2013-10-26 00:13:20
我真的很感悟……
T_未命名
发表于 2013-10-26 00:19:35
楼主加油,鱼C加油!我们都看好你哦!
scuchx1
发表于 2013-10-26 00:23:57
又是干嘛的?
小明上广州
发表于 2013-10-26 00:46:47
真是难得给力的帖子啊。
ルー鵬
发表于 2013-10-26 01:16:55
太生气了,无法HOLD啦 >_<......
傻鸟joker
发表于 2013-10-26 01:17:56
激动人心,无法言表!