数据结构——行编辑

这道题花了我一个下午加一个晚上的时间,再在今天中午Debug掉一个错误后,AC了

整个过程痛苦无比

将起大体代码写出来没用什么时间,主要就是后面的Debug阶段,各种没注意,各种RE

先上代码

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct text{
    char word;
    struct text *next;
}text;
typedef struct text_head{
    text *head;
    struct text_head *next;
}text_head;
int count_hang(text_head *all_first)
{
    int hang;
    text_head *all;
    for (all=all_first->next,hang=0; all!=NULL; all=all->next) {
        hang++;
    }
    return hang;
}//计算有多少行数据
text* input(char *sentence)
{
    text* make_text,*pre,*head;
    int i=0;
    char now;
    if (sentence[0]=='\0') {
        return NULL;
    }
    else {
        head=(text*)malloc(sizeof(text));
        head->word=sentence[i];
        head->next=NULL;
        pre=head;
        i++;
        while (now=sentence[i],now!='\0') {
        make_text=(text*)malloc(sizeof(text));
        make_text->word=now;
        make_text->next=NULL;
        pre->next=make_text;
        pre=make_text;
        i++;
    }
        return head;
    }
}//将字符串转变为链表存储
void output(text_head* all_first)
{
    text_head *head;
    text *word;
    for (head=all_first->next; head!=NULL; head=head->next) {
        for (word=head->head; word!=NULL; word=word->next) {
            printf("%c",word->word);
        }
        printf("\n");
    }
}//输出所有数据
void insert(text_head* all_first)
{
    int length_hang,i;
    char sentence[100];
    int hang;
    text_head *make_head,*p;
    length_hang=count_hang(all_first);
    scanf("%d/",&hang);
    gets(sentence);
    make_head=(text_head*)malloc(sizeof(text_head));
    make_head->head=input(sentence);
    if (hang<=length_hang) {
        for (p=all_first,i=1; i!=hang; p=p->next) {
            i++;
        }
        make_head->next=p->next;
        p->next=make_head;
    }
    else{
        for (p=all_first;p->next!=NULL; p=p->next) {
            ;
        }
        p->next=make_head;
        make_head->next=NULL;
    }
}//i 指令
void change(text_head* all_first)
{
    char replaced[100],replace[100];
    char letter;
    int num_replaced=0,flag,count,num_replace=0;
    text_head *p_head;
    text *p_text,*p_judge,*head_replace,*end_replace,*end_replaced,*p_replaced;
    getchar();
    while (scanf("%c",&letter),(num_replaced==0)?1:letter!='/') {
        if (letter==92) {
            if(scanf("%c",&letter),letter=='/'){
                replaced[num_replaced]='/';
                num_replaced++;
            }
        }
        else {
            replaced[num_replaced]=letter;
            num_replaced++;
        }
    }
    while (scanf("%c",&letter),letter!='\n') {
        if (letter==92) {
            if (scanf("%c",&letter),letter=='/') {
                replace[num_replace]='/';
                num_replace++;
            }
        }
        else{
            replace[num_replace]=letter;
            num_replace++;
        }
    }
    replace[num_replace]='\0';
    for (p_head=all_first->next; p_head!=NULL; p_head=p_head->next) {
        for (p_text=p_head->head,flag=1; p_text!=NULL; p_text=p_text->next,flag=1) {
            for (count=0,p_judge=p_text; count<num_replaced; p_judge=p_judge->next,count++) {
                if (p_judge==NULL) {
                    flag=0;
                    break;
                }
                if (p_judge->word!=replaced[count]) {
                    flag=0;
                    break;
                }
            }
            if (flag==1) {
                if (replace[0]=='\0') {
                    for (end_replaced=p_text,count=1; count<num_replaced; count++,end_replaced=end_replaced->next) {
                        ;
                    }
                    if(p_head->head==p_text){
                        p_head->head=end_replaced->next;
                    }
                    else {
                        for (p_replaced=p_head->head; p_replaced->next!=p_text; p_replaced=p_replaced->next) {
                            ;
                        }
                        p_replaced->next=end_replaced;
                    }
                }
                else {
                    head_replace=input(replace);
                    for (end_replace=head_replace; end_replace->next!=NULL; end_replace=end_replace->next) {
                        ;
                    }
                    for (end_replaced=p_text,count=1; count<num_replaced; count++,end_replaced=end_replaced->next) {
                        ;
                    }
                    end_replace->next=end_replaced->next;
                    if(p_head->head==p_text){
                        p_head->head=head_replace;
                    }
                    else {
                        for (p_replaced=p_head->head; p_replaced->next!=p_text; p_replaced=p_replaced->next) {
                            ;
                        }
                        p_replaced->next=head_replace;
                    }
                    p_text=end_replace;
                }
            }
        }
    }
}//c 指令
void delete(text_head* all_first)
{
    int hang,i;
    text_head *num_hang,*de_hang;
    scanf("%d",&hang);
    for (i=1,num_hang=all_first; i!=hang; num_hang=num_hang->next) {
        i++;
    }
    de_hang=num_hang->next;
    num_hang->next=de_hang->next;
    free(de_hang);
}//d 指令
int cmp(const void *a,const void *b)
{
    return(*(int *)b-*(int *)a);
}
void combin(text_head* all_first)
{
    int combined[100],combin_hang,i=0,count,i_1,num_hang,flag=1;
    text_head *head_text,*combin_text;
    text *end_text;
    num_hang=count_hang(all_first);
    scanf("%d",&combin_hang);
    for (head_text=all_first,count=0; count!=combin_hang; head_text=head_text->next,count++) {
        ;
    }
    while (scanf("/%d",&combined[i])) {
        if (combined[i]>num_hang) {
            flag=0;
        }
        if(flag){
            for (end_text=head_text->head; (head_text->head==NULL)?0:end_text->next!=NULL; end_text=end_text->next) {
                ;
            }
            for (combin_text=all_first,count=0; count!=combined[i]; combin_text=combin_text->next,count++) {
                ;
            }
            (head_text->head==NULL)?head_text->head=combin_text->head:(end_text->next=combin_text->head);
        }
        (flag==1)?i++:(flag=1);
    }
    qsort(combined, i, sizeof(int), cmp);
    for (i_1=0; i_1<i; i_1++) {
        for (head_text=all_first,count=1; count!=combined[i_1]; count++,head_text=head_text->next) {
            ;
        }
        head_text->next=head_text->next->next;
    }
}// f 指令
int main()
{
    char sentence[100],order;
    gets(sentence);
    text_head *all_first,*make_head,*pre;
    all_first=(text_head*)malloc(sizeof(text_head));
    all_first->next=NULL;
    all_first->head=NULL;
    pre=all_first;
    while (gets(sentence),strcmp(sentence,"[/Text]")) {
        make_head=(text_head*)malloc(sizeof(text_head));
        pre->next=make_head;
        make_head->next=NULL;
        make_head->head=input(sentence);
        pre=make_head;
    }
    while (scanf("%c",&order)!=EOF) {
        switch (order) {
            case 'i':
                insert(all_first);
                break;
            case 'c':
                change(all_first);
                break;
            case 'd':
                delete(all_first);
                getchar();
                break;
            case 'f':
                combin(all_first);
                getchar();
                break;
            case'\n':
                break;
            default:
                break;
        }
    }
    output(all_first);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值