数据结构实验之栈与队列九:行编辑器

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

一个简单的行编辑程序的功能是:接受用户从终端输入的程序或数据,并存入用户的数据区。

由于用户在终端上进行输入时,不能保证不出差错,因此,若在编辑程序中,“每接受一个字符即存入用户数据区”的做法显然不是最恰当的。较好的做法是,设立一个输入缓冲区,用以接受用户输入的一行字符,然后逐行存入用户数据区。允许用户输入出差错,并在发现有误时可以及时更正。例如,当用户发现刚刚键入的一个字符是错的时,可补进一个退格符"#",以表示前一个字符无效;

如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符"@",以表示当前行中的字符均无效。

如果已经在行首继续输入'#'符号无效。

Input

输入多行字符序列,行字符总数(包含退格符和退行符)不大于250。

Output

按照上述说明得到的输出。

Sample Input

whli##ilr#e(s#*s)

outcha@putchar(*s=#++);

Sample Output

while(*s)

putchar(*s++);

Hint

Source

cz

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct node
{
char data;
struct node *next;
}Node; typedef struct stack
{
Node *base,*top;
int len;
}Stack; Node *newnode()//建立节点
{
Node *t;
t = (Node *)malloc(sizeof(Node));
t->next = NULL;
return t;
}; Stack *Newstack()//建立新栈
{
Stack *t;
t = (Stack *)malloc(sizeof(Stack));
t->top = newnode();
t->base = t->top;
t->len = 0;
return t;
} void push(Stack *t,char x)//入站
{
Node *p = newnode();
p->data = x;
p->next = t->top->next;
t->top->next = p;
t->base = p;
t->len++;
} char top(Stack *t)//询问栈顶元素
{
return t->top->next->data;
} void pop(Stack *t)//出栈
{
Node *p;
p = t->top->next;
t->top->next = t->top->next->next;
free(p);
t->len--;
} int empty(Stack *t)//询问栈是否为空
{
if(t->top->next==NULL)
return 1;
return 0;
} void del(Stack *t)//清空栈
{
while(!empty(t))
pop(t);
} void show(Node *t)
{
if(t==NULL)
return;
show(t->next);
if(t)
printf("%c",t->data);
} int main()
{
char s[255];
int i,n;
Stack *t;
t = Newstack();
while(gets(s)!=NULL)
{
del(t);
n = strlen(s);
for(i=0;i<n;i++)
{
if(s[i]=='@')
del(t);
else if(s[i]=='#')
{
if(!empty(t))
pop(t);
}
else
push(t,s[i]);
}
show(t->top->next);
printf("\n");
}
return 0;
}

顺序表

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct Static
{
char *top,*base;
}Static; Static newStatic()
{
Static t;
t.top = (char *)malloc(100050*sizeof(char));
t.base = t.top;
return t;
} char top(Static t)
{
return *(t.top-1);
} void pop(Static *t)
{
t->top--;
} void push(Static *t,char x)
{
*(t->top++) = x;
} int empty(Static t)
{
if(t.base==t.top)
return 1;
return 0;
} void clear(Static *t)
{
while(!empty(*t))
pop(t);
} int main()
{
char s[255];
int n,i;
Static k;
k = newStatic();
while(scanf("%s",s)!=EOF)
{
n = strlen(s);
if(!empty(k))
clear(&k);
for(i=0;i<n;i++)
{
if(s[i]=='@')
clear(&k);
else if(s[i]=='#')
{
if(!empty(k))
pop(&k);
}
else
push(&k,s[i]);
}
i = 0;
while(!empty(k))
{
s[i++] = top(k);
pop(&k);
}
s[i] = '\0';
i--;
while(i>=0)
{
printf("%c",s[i]);
i--;
}
printf("\n");
}
return 0;
}

SDUT-1479_数据结构实验之栈与队列九:行编辑器的更多相关文章

  1. SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...

  2. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  3. SDUT-3335_数据结构实验之栈与队列八:栈的基本操作

    数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...

  4. SDUT-3334_数据结构实验之栈与队列七:出栈序列判定

    数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...

  5. SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值

    数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...

  6. SDUT-2134_数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...

  7. SDUT-2133_数据结构实验之栈与队列三:后缀式求值

    数据结构实验之栈与队列三:后缀式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运算符的后缀表示式 ...

  8. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

  9. SDUT-2131_数据结构实验之栈与队列一:进制转换

    数据结构实验之栈与队列一:进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制非负整数,将其转换成对 ...

随机推荐

  1. 我的常用vs code 插件

    换了台电脑重新装上了VS CODE,但是用起来后发现非常不顺手,突然醒悟原来还没有装上插件. 正动手装插件,但又一脸茫然了,我以前都装了些什么插件来着?因为平时根本不会去几插件的名字啊,只能靠搜搜一些 ...

  2. Vue-cli3.x在开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chunk {/d} failed.或者Uncaught SyntaxError: Unexpected token <错误

    使用Vue-cli3.x开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chu ...

  3. localStorage对象简单应用 - - 访问次数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. ubuntu忘记root密码

    解决办法: 选择GRUB第2个选项(恢复模式) 按e进入编辑模式 将ro recovery nomodeset修改成rw single init=/bin/bash 然后再按F10进入单用户模式,进入 ...

  5. CentOS7 下的 firewall 用法

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld  停止: systemctl disab ...

  6. 深入浅析python中的多进程、多线程、协程

    深入浅析python中的多进程.多线程.协程 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源 ...

  7. Boost Asio教程集合

    http://zh.highscore.de/cpp/boost/ 第七章 https://mmoaay.gitbooks.io/boost-asio-cpp-network-programming- ...

  8. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  9. DFA算法实现敏感词过滤

    DFA算法:即确定有穷自动机,简单点说就是,它是是通过event和当前的state得到下一个state,即event+state=nextstate.理解为系统中有多个节点,通过传递进入的event, ...

  10. 使用idea工具的几个个性化步骤

    1.更改背景样式2.添加激情代码插件 Power mode II3.安装省略 getset 插件 Lombok 引入pom.xml <!-- 此组件可以用来实体类 省略 getset 构造等等 ...