zoukankan      html  css  js  c++  java
  • SDUT-1479_数据结构实验之栈与队列九:行编辑器

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

    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("
    ");
        }
        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] = '';
            i--;
            while(i>=0)
            {
                printf("%c",s[i]);
                i--;
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    Java实现八大排序算法
    Java实现二分查找算法
    Win10下通过IIS调试ASP程序遇到的问题和解决方案
    Nginx几种负载均衡算法及配置实例
    Java解决CSRF问题
    Nginx Https配置不带www跳转www
    面试中的Https
    面试中的DNS
    java系列视频教程下载
    关于Mysql DATE_FORMAT() 日期格式
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9748053.html
Copyright © 2011-2022 走看看