我把两位数的数字转化成字符然后压到栈里了,但是多位数就不能转化成字符了,要怎么处理啊
char* zhuanhuan(char* mix){
//读取中缀表达式,将其改为后缀表达式
char* a=new char[100];int i=0;
LinkedStack ss;
char ch='#',ch1,op;
ss.Push(ch);
int j=0;ch = mix[j];
while(ss.isEmpty()==false&&a[i]!='#'){
if(ch<='9'&&ch>='0'){
int x = ch-'0';
while( ch = mix[++j]){
if(ch<='9'&&ch>='0'){
x = x10+ch-'0';
}
else break;
}
a[i]=x+'0';
i++;
}
else{
ss.getTop(ch1);
if(isp(ch1)<icp(ch)){
ss.Push(ch);
ch = mix[++j];
}
else if(isp(ch1)>icp(ch)){
ss.Pop(op);
a[i]=op;i++;
}
else{
ss.Pop(op);
if(op=='(')
ch = mix[++j];
}
}
}
a[i]='#';
return a;
}
//输入一个中缀表达式,转换成后缀表达式,用后缀表达式计算
class Calculator{
private:
LinkedStack s;//定义栈s
void AddOperand(double value);//将操作数的值value 入操作栈
bool get2Operand(double& left,double& right);//从栈中退出两个操作数
void DoOperator(char op);//形成运算指令,进行一次计算,op是运算符+-/
public:
Calculator():s(){}//构造函数
void Run(char* mix);//计算
void Clear();
};
void Calculator::DoOperator(char op){
double left,right,value;
int result;
result = get2Operand(left,right);
if(result = true){
switch (op)
{
case '+':value = left+right;s.Push(value);break;
case '-':value = left-right;s.Push(value);break;
case '':value = leftright;s.Push(value);break;
case '/':if(right ==0){cerr<<"Divide by 0!"<<endl;Clear();}
else {value = left/right;s.Push(value);break;}
}
}
else Clear();
};
bool Calculator::get2Operand(double &left, double &right){
if(s.isEmpty()==true){
cerr<<"缺少右操作数"<<endl;return false;
}
s.Pop(right);//先取出的是右操作数(栈先进后出)
if(s.isEmpty()==true){
cerr<<"缺少左操作数"<<endl;return false;
}
s.Pop(left);
return true;
}
void Calculator::AddOperand(double value){
s.Push(value);
}
void Calculator::Run(char* mix){
chara=zhuanhuan(mix);
int i=0; double x;
while(a[i]!='#'){
switch (a[i]){
case '+':case '-':case '':case '/':
DoOperator(a[i]);break;
default:
x=a[i]-48;
s.Push(x);//把数字放到栈里面
}
i++;
}
s.getTop(x);
cout<<x<<endl;
};