1 条题解
-
0
C :
#include<stdio.h> #include<stdlib.h> typedef char ElemType; typedef enum {TRUE,FALSE,OVERFLOW} Status; #define MAXSIZE 200 //定义栈的最大容量 typedef struct { ElemType elem[MAXSIZE]; //存放栈中元素的一维数组空间 int top; //栈顶指针变量 }SeqStack; void InitStack (SeqStack *S) { //构造一个空栈S S -> top = 0; } Status StackEmpty (SeqStack *S) { //判断S是否为空栈,为空时返回TRUE,否则返回FALSE return (S->top == 0 ? TRUE : FALSE); } Status Push (SeqStack *S, ElemType e) { //将数据元素e压入栈顶 if (S -> top == MAXSIZE) return FALSE;//栈已满,进栈失败 S -> elem[S -> top] = e; //将e插入栈顶 S -> top ++; //修改栈顶指针 return TRUE; } Status GetTop (SeqStack *S, ElemType *e) { //若栈不空,则用e返回栈顶元素 if (S->top == 0) return FALSE; *e = S->elem[S->top - 1]; //保存栈顶元素 return TRUE; } Status Pop (SeqStack *S, ElemType *e) { //若栈不空,将栈顶元素弹出,并用e返回其值 if (S -> top == 0) return FALSE; else { S -> top --; //修改栈顶指针 *e = S -> elem[S -> top]; //保存栈顶元素 return TRUE; } } //全局变量 SeqStack S; char C[MAXSIZE]; int main() { int i; ElemType e; scanf("%s",C); for(i=0;C[i]!='\0';i++) { if((C[i]>='A' && C[i]<='Z') || (C[i]>='a' && C[i]<='z')) printf("%c",C[i]); else if(C[i]=='+' || C[i]=='-') { while(GetTop(&S,&e)==TRUE) { if(e=='+' || e=='-' || e=='*' || e=='/') { Pop(&S,&e); printf("%c",e); } else break; } Push(&S,C[i]); } else if(C[i]=='*' || C[i]=='/') { while(GetTop(&S,&e)==TRUE) { if(e=='*' || e=='/') { Pop(&S,&e); printf("%c",e); } else break; } Push(&S,C[i]); } else if(C[i]=='(') { Push(&S,C[i]); } else { while(GetTop(&S,&e)==TRUE) { if(e=='(') { Pop(&S,&e); break; } else { Pop(&S,&e); printf("%c",e); } } } } while(Pop(&S,&e)==TRUE) { printf("%c",e); } printf("\n"); return 0; }
C++ :
#include<iostream> #include<cstring> #include<stack> #include<cstdio> using namespace std; string s; stack<char> a; bool isoperator(char); int getpre(char,bool); void work(); int main() { //freopen("hz.in","r",stdin); //freopen("hz.out","w",stdout); cin>>s; work(); return 0; } void work() { int i; for(i=0;i<s.size();++i) { if(!isoperator(s[i])) { cout<<s[i]; continue; } else { if(a.empty()) { a.push(s[i]); continue; } else { if(s[i]==')') { while(a.top()!='('&& !a.empty()) { cout<<a.top(); a.pop(); } a.pop(); continue; } while(!a.empty()&&getpre(s[i],false)<=getpre(a.top(),true)) { cout<<a.top(); a.pop(); } a.push(s[i]); } } } while(!a.empty()) { cout<<a.top(); a.pop(); } } bool isoperator(char ch) { if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch==')'||ch=='(')return true; else return false; } int getpre(char ch,bool flag) { if(ch=='+'||ch=='-') return 1; if(ch=='*'||ch=='/') return 2; if(ch=='('&&flag) return 0; if(ch=='('&&!flag) return 3; }
Pascal :
program acmb; type stack=array[1..200] of char; var top,i,j,h:integer; ch,w:char; a,s:stack; st:string; Function t(p1:char):integer; begin case p1 of '#':t:=-1; '(':t:=0; '+':t:=1; '-':t:=1; '*':t:=2; '/':t:=2; ')':t:=3; end; end; procedure push(vars:stack;ch:char); begin inc(top); s[top]:=ch; end; procedure pop(vars:stack); begin dec(top); end; function readtop(s:stack):char; begin readtop:=s[top]; end; begin readln(st); st:=st+'#'; push(s,'#'); j:=1; i:=1; ch:=st[i]; while ch<>'#'do begin if (ch in['A'..'Z']) or (ch in ['a'..'z']) then begin while (ch in['A'..'Z']) or (ch in ['a'..'z']) do begin a[j]:=ch; inc(j); inc(i); ch:=st[i]; end; {inc(j);} end else begin case ch of '+','-','*','/': begin w:=readtop(s); {取出栈顶符号} while t(ch)<=t(w)do begin a[j]:=w;inc(j); pop(s); w:=readtop(s); end; push(s,ch); end; '(':push(s,ch); ')':begin while s[top]<>'('do begin a[j]:=s[top];inc(j); pop(s); end; top:=top-1; end; end; {end case} inc(i); ch:=st[i]; end; end; w:=readtop(s); while w<>'#' do begin a[j]:=w;inc(j); pop(s);w:=readtop(s); end; a[j]:='#';pop(s); h:=1; repeat write(a[h]);inc(h); until a[h]='#'; writeln; end.
- 1
信息
- ID
- 870
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者