SDUT1479数据结构实验之栈:行编辑器

先是普通的数组做法

 1 #include<stdio.h>

 2 #include<string.h>

 3 int main()

 4 {

 5     char ch[256] ;

 6     while(~scanf("%s",ch))

 7     {

 8         char sh[256] ;

 9         int top = -1 ;//底下用于判断数组里是否是空

10         int len = strlen(ch) ;

11         for(int i = 0 ; i <= len-1 ; i++)

12         {

13             if(ch[i]!='#'&&ch[i]!='@')

14             sh[++top] = ch[i] ;

15             else if(ch[i] == '#'&&top != -1)//判断#前边是否是无字符情况

16             {

17                 sh[top--] ;

18             }

19             else if(ch[i] == '@')

20             {

21                 while(top != -1)

22                 top-- ;//把存入数组里的字符全部清掉

23             }

24         }

25         for(int k =0 ; k<=top ; k++)

26         printf("%c",sh[k]) ;

27         printf("\n") ;

28     }

29     return 0 ;

30 }

这个是用的C++里的stack函数进行反复的出栈进栈操作

 1 #include<cstdio>

 2 #include<cstring>

 3 #include<stack>

 4 #include<algorithm>

 5 using namespace std;

 6 int main()

 7 {

 8     stack<char>q;

 9     char s[1000],s1[1000];

10     while(scanf("%s",s)!=EOF)

11     {

12         int len=strlen(s);

13         for(int i=0; i<len; i++)

14         {

15             if(s[i]!='#'&&s[i]!='@')

16             {

17                 q.push(s[i]);

18             }

19             else if(s[i]=='#'&&!q.empty())

20                 q.pop();

21             else if(s[i]=='@')

22             {

23                 while(!q.empty())

24                 {

25                     q.pop();

26                 }

27             }

28         }

29         int j=0;

30         while(!q.empty())

31         {

32             s1[j++]=q.top();

33             q.pop();

34         }

35         s1[j]='\0';

36         for(int i=j-1; i>=0; i--)

37             printf("%c",s1[i]);

38         printf("\n");

39     }

40     return 0;

41 }

42  
View Code

 

你可能感兴趣的:(数据结构)