1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cmath> #define MAXSIZE 100 #define OK 1 using namespace std; typedef struct { char no[20]; char name[50]; double price; }BOOK; typedef struct { BOOK *base; BOOK *top; int stacksize; }SqStack; bool InitStack(SqStack &S) { S.base=new BOOK[MAXSIZE]; if(!S.base) exit(0); S.top=S.base; S.stacksize=MAXSIZE; return OK; } bool Push(SqStack &S,BOOK e) { if((S.top-S.base)==S.stacksize) return 0; *S.top++=e; return OK; } bool Pop(SqStack &S,BOOK &e) { if(S.top==S.base) return 0; e=*(--S.top); //栈顶元素出栈 return OK; } BOOK GetTop(SqStack &S) { if(S.top!=S.base) return *(--S.top); } int main() { SqStack S; InitStack(S); BOOK p; while(cin>>p.no>>p.name>>p.price) { if(p.no[0]=='0'&&p.name[0]=='0'&&fabs(p.price-0.0)<1e-6) break; Push(S,p); } p=GetTop(S); cout<<"栈的最高的元素"<<p.no<<" "<<p.name<<" "<<p.price<<endl; S.top++; BOOK pp; cout<<"栈的每个元素"<<endl; while(Pop(S,pp)) { cout<<pp.no<<" "<<pp.name<<" "<<pp.price<<endl; } return 0; }
|