2007winter

做自己真心想做的事,你就没事了

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

栈的应用1:

 1 #include <iostream>
 2 #include <malloc.h>
 3 #include <conio.h>
 4 #include <stdio.h>
 5 
 6 using namespace std;
 7 
 8 #define STACK_INT_SIZE 100
 9 #define STACKINCREMENT 10
10 typedef int SElemType;
11 typedef struct SqStack
12 {
13     SElemType *base;
14     SElemType *top;
15     int stacksize;
16 }SqStack;
17 
18 int InitStack(SqStack &s)
19 {
20     s.base = (SElemType*)malloc(STACK_INT_SIZE*sizeof(SElemType));
21     if(!s.base)
22     {
23         cout<<"overflow!"<<endl;
24         return 0;
25     }
26     s.top=s.base;
27     s.stacksize=STACK_INT_SIZE;
28     cout<<"Success to constructe a empty stack!"<<endl;
29     return 1;
30 
31 }
32 
33 int push(SqStack &s,SElemType e)
34 {
35     if(s.top-s.base>s.stacksize)
36     {
37         s.base=(SElemType *)realloc(s.base,(s.stacksize+STACKINCREMENT*sizeof(SElemType)));
38         if(!s.base)
39         {
40             cout<<"Failture to reallocate the Memory units"<<endl;
41             return 0;
42         }
43         s.top=s.base+s.stacksize;
44         s.stacksize+=STACKINCREMENT;
45     }
46     *s.top++=e;
47     return 1;
48 
49 }
50 
51 int pop(SqStack &s,SElemType &e)
52 {
53     if(s.top==s.base)
54     {
55         cout<<"underflow!"<<endl;
56         return 0;
57     }
58     e=*--s.top;
59     return 1;
60 }
61 
62 int StackEmpty(SqStack &s)
63 {
64     if(s.top==s.base)
65         return 1;
66     else
67         return 0;
68 }
69 
70 void Conversion()
71 {
72     SqStack S;
73     SElemType N,e;
74     InitStack(S);    
75     cout<<"please input the number:"<<endl;
76     cin>>N;
77     while(N)
78     {
79         push(S,N%8);
80         N=N/8;
81     }
82     while(!StackEmpty(S))
83     {
84         pop(S,e);        
85         cout<<e;
86     }
87 }
88 
89 void main()
90 {
91     cout<<"Conversion.cpp"<<endl<<"==========================="<<endl<<endl;
92     Conversion();
93     cout<<endl<<"...OK...!"<<endl;
94     
95     char wait;
96     cin>>wait;
97     
98 }

 

posted on 2012-10-23 09:59  2007winter  阅读(145)  评论(0编辑  收藏  举报