这是重新又打的代码,问题已经解决了,有兴趣可以来看看,这个程序的 cin 和 scanf 很关键,不能随便改,本人也不是清楚他们的区别。另外检测输入的函数,还没有打出来,可以来提点意见。#include"iostream"
#include"stdio.h"
#include"stdlib.h"
#include"ctype.h"
using namespace std;
#define sizestack 50
typedef double elemtype;
typedef char elemtype_1;
typedef char status;
typedef struct stacklink{
elemtype * base;
elemtype * top;
int stacksize;
}link;
typedef struct stacklink_1{
elemtype_1 * base;
elemtype_1 * top;
int stacksize;
}clink;
void initstack(link &s){
s.base=(elemtype*)malloc(sizestack*sizeof(elemtype));
s.top=s.base;
s.stacksize=sizestack;
}
void initstack_1(clink &s){
s.base=(elemtype_1*)malloc(sizestack*sizeof(elemtype_1));
s.top=s.base;
s.stacksize=sizestack;
}
void push(link &s,double c){
*s.top=c;
++s.top;
}
void push_1(clink &s,char c){
*s.top=c;
++s.top;
}
int pop(link& s,double &e){
--s.top;
e=*s.top;
return 1;
}
int pop_1(clink& s,char &e){
--s.top;
e=*s.top;
return 1;
}
double gettop(link s){
--s.top;
return *s.top;
}
char gettop_1(clink s){
--s.top;
return *s.top;
}
char precede(char q,char p){
char z;
if(q=='#')
switch(p){
case'#':z='=';break;
case')':break;
default:z='<';break;
}
if(q=='+'||q=='-')
switch(p){
case'+':z='>';break;
case'-':z='>';break;
case'*':z='<';break;
case'/':z='<';break;
case'(':z='<';break;
case')':z='>';break;
case'#':z='>';break;
}
if(q=='*'||q=='/')
switch(p){
case'+':z='>';break;
case'-':z='>';break;
case'*':z='>';break;
case'/':z='>';break;
case'(':z='<';break;
case')':z='>';break;
case'#':z='>';break;
}
if(q=='(')
switch(p){
case')':z='=';break;
case'#':exit(0);break;
default:z='<';break;
}
if(q==')')
switch(p){
case'(':exit(0);break;
default:z='>';break;
}
return z;
}
double operate(double a,char theta, double b){
if(theta=='-')return b-a;
if(theta=='*')return b*a;
if(theta=='/')return b/a;
if(theta=='+')return b+a;
}
int in(char c){
switch(c){
case'/':return 1;break;
case'+':return 1;break;
case'-':return 1;break;
case'*':return 1;break;
default :return 0;break;
}
}
double operatype(){
double a,b,d,n;
clink optr; link opnd;
initstack_1(optr);
push_1(optr,'#');
initstack(opnd);
char c,theta,x,s;
char str[10];
int i=0;
cin>>c;
while(c!='#'||gettop_1(optr)!='#'){
if(isdigit(c)||c=='.'){
str[i]=c;
++i;
str[i]='\0';
if(i>=10)return -1;
scanf("%c",&c);
if(c==' '){
d=atof(str);
push(opnd,d);
i=0;
scanf("%c",&c);
}
}
/* if(!in(c)){
scanf("%lf",&n);
push(opnd,n);
scanf("%c",&c);
cout<<c;
}*/
else{
if(precede(gettop_1(optr),c)=='<'){
cout<<'<';
push_1(optr,c);
cin>>c;
}else
if(precede(gettop_1(optr),c)=='='){
cout<<'=';
pop_1(optr,x);
cin>>c;
}
else
if(precede(gettop_1(optr),c)=='>'){
cout<<'>';
pop_1(optr,theta);
pop(opnd,a);
pop(opnd,b);
push(opnd,operate(a,theta,b));
}
}
}
return gettop(opnd);
}
int main(){
cout<<operatype();
return 0;
}
|