问题:
欠嵌套括号格式错误
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxSize 25
typedef struct {
char elem[maxSize];
int top;//top 指向当前元素
}SeqStack;
SeqStack* initStack ( ) {
SeqStack* S0=NULL;
S0=(SeqStack*)malloc(sizeof(SeqStack));
S0->top =0;
return S0;
}
void Push (SeqStack *S0, char x ) {
S0->elem[S0->top] = x;
S0->top++;
}
char Pop (SeqStack *S0 ) {
char x;
S0->top--;
x = S0->elem[S0->top];
return x;
}
char getTop (SeqStack *S0 ) {
return S0->elem[S0->top-1];
}
int empty(SeqStack *S0){
if(S0->top==0)
return 1;
else
return 0;
}
int main(){
char a[maxSize];
char m;
int n=0;
gets(a);
int len;
len=strlen(a);
SeqStack* S;
S=initStack ( );
SeqStack* fuhao;
fuhao=initStack ( );
int index=0;
if(a[0]=='-') {
printf("-");
index = 1;
}
for(int i=index;i<len;i++){
if(a[i]>='0'&&a[i]<='9'){
if(i!=index)
Push(S,' ');
Push(S,a[i]);
while((a[i+1]>='0'&&a[i+1]<='9')||a[i+1]=='.'){
i++;
Push(S,a[i]);
}
continue;
}
else if(a[i]=='('){
Push(fuhao,a[i]);
continue;
}
else if(a[i]==')'){
while(getTop(fuhao)!='(') {
Push(S,' ');
Push(S,Pop(fuhao));
}
Pop(fuhao);
}
//+
else if(a[i]=='+'){
if(a[i-1]=='('&&(a[i+1]>='0'&&a[i+1]<='9'))
continue;
else{
if(getTop(fuhao)=='(') {
Push(fuhao, a[i]);
continue;
}
else {
while (getTop(fuhao) != '('&&!empty(fuhao)){
Push(S,' ');
Push(S,Pop(fuhao));
}
Push(fuhao, a[i]);
continue;
}
}
}
//-
else if(a[i]=='-'){
if(a[i-1]=='('&&(a[i+1]>='0'&&a[i+1]<='9')) {
Push(S,' ');
Push(S, a[i]);
i++;
Push(S,a[i]);
continue;
}
else{
if(getTop(fuhao)=='(') {
Push(fuhao, a[i]);
continue;
}
else {
while (getTop(fuhao) != '('&&!empty(fuhao)){
Push(S,' ');
Push(S,Pop(fuhao));
}
Push(fuhao, a[i]);
continue;
}
}
}
//*/
else if(a[i]=='*'||a[i]=='/'){
if(getTop(fuhao)=='('||getTop(fuhao)=='+'||getTop(fuhao)=='-')
Push(fuhao,a[i]);
else{
while(getTop(fuhao)!='(' && getTop(fuhao)!='+' && getTop(fuhao)!='-'&&!empty(fuhao)) {
Push(S,' ');
Push(S,Pop(fuhao));
}
Push(fuhao, a[i]);
}
}
}
while(!empty(fuhao)) {
Push(S,' ');
Push(S, Pop(fuhao));
}
SeqStack* S1;
S1=initStack ( );
while(!empty(S))
Push(S1,Pop(S));
while(!empty(S1)){
printf("%c",Pop(S1));
}
return 0;
}