解题思路:
①:输入字符串,将字符串中字符逐个入栈
②:入栈时,非'#' 和非'@'字符入栈,遇到'#'字符,出栈前一个入栈字符,遇到'@'字符,前面所有已入栈的字符出栈,即栈置空
③:自栈底到栈顶,依次输出字符
参考代码:
#include<stdio.h> #include<string.h> typedef struct Stack_{ char data[201]; int top; /*栈顶指针*/ int base; /*栈底指针*/ }*Stack,STACK; void init(Stack s); void push_s(Stack s,char c); void pop_s(Stack s,char *e); int stack_empty(Stack s); int main() { char e,input_[201]; STACK s; while(gets(input_)!=NULL) { init(&s); for(int i=0;i<strlen(input_);i++) { if(input_[i]=='#') pop_s(&s,&e); else if(input_[i]=='@') init(&s); else push_s(&s,input_[i]); } /*输出,从栈底base向上到栈顶结束*/ while(s.base!=s.top) printf("%c",s.data[s.base++]); printf("\n"); } return 0; } /*========================================*/ void init(Stack s) { s->top=0; s->base=0; } /*========================================*/ void push_s(Stack s,char c) { s->data[s->top++]=c; } /*========================================*/ void pop_s(Stack s,char *e) { (*e)=s->data[--s->top]; } /*========================================*/ int stack_empty(Stack s) { if(s->top==0) return 1; else return 0; } /*========================================*/
0.0分
1 人评分
A+B for Input-Output Practice (I) (C++代码)浏览:1412 |
C语言训练-尼科彻斯定理 (C语言代码)。。。内含证明定理浏览:2069 |
不知道哪里错了浏览:1145 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:522 |
C语言训练-最大数问题 (C语言代码)浏览:633 |
人见人爱A+B (C语言代码)浏览:628 |
A+B for Input-Output Practice (II) (C语言代码)浏览:999 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:1195 |
蛇行矩阵 (C语言代码)浏览:536 |
DNA (C语言代码)浏览:540 |