CCF 201803-03(URL映射)wait

题目信息:首页 - 计算机软件能力认证考试系统

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct{
    char name[100];
    char num[100];
}rule;
void urlMap();
char *strApt(char *str);
void putRst(char *, char *);

//URL映射 !!!error:变量赋值前使用
void urlMap(){
    int n, m, i, j, x, mapi, tmp;
    char str[101];
    char map[50];
    char *s;
    rule *r;
    scanf("%d %d", &n, &m);
    r = (rule *)malloc(n * sizeof(rule));
    for(i = 0; i < n; i++){
        scanf("%s %s", r[i].num, r[i].name);
        //r[i].num = strApt(r[i].num);
        strcpy(r[i].num, strApt(r[i].num));
    }

    while(m){
        scanf("%s", str);
        x = 0;//第x条规则
lab:
        i = 0; j = 0; s = r[x].num; mapi = 0;//开始匹配
        memset(map, 0, 50);//!!!error

        while(str[i] != '\0' && s[j] != '\0'){
            if(s[j] != ',' && s[j] != ';' && s[j] != ':'){
                if(str[i] == s[j]){
                    i++; j++;
                }
                else{
                    x++;//匹配失败,指向下一条规则或url
                    if(x == n) {printf("404\n"); goto next;}
                    else goto lab;
                }
            }
            else{
                if(s[j] == ',' && str[i] >= '0' && str[i] <= '9'){//int
                    j++; tmp = 0;
                    while(str[i] == '0'){//<int>
                        i++; tmp = 1;
                    }
                    if(tmp == 1) {map[mapi] = i - 1; tmp = 0;}
                    else map[mapi] = i;
                    mapi++;//start
                    while(str[i] >= '0' && str[i] <= '9'){
                        i++;
                    }
                    map[mapi] = i - 1; mapi++;//stop
                    //匹配到的<int>索引
                }
                else if(s[j] == ';' && str[i] != '/'){//path 匹配至url末尾
                    j++;
                    map[mapi] = i; mapi++;
                    while(str[i] != '\0'){
                        i++;
                    }
                    map[mapi] = i - 1;//url匹配成功
                    printf("%s ", r[x].name);
                    putRst(map, str);
                    goto next;
                }
                else if(s[j] == ':' && str[i] != '/'){//str
                    j++;
                    map[mapi] = i; mapi++;
                    while(str[i] != '/'){
                        i++;
                    }
                    map[mapi] = i - 1; mapi++;
                }
                else{
                    x++;//匹配失败,指向下一条规则或url
                    if(x == n) {printf("404\n"); goto next;}
                    else goto lab;
                }
            }
            //s = r[j].num;
        }
        if(str[i] == '\0' && s[j] == '\0'){
            printf("%s ", r[x].name);
            putRst(map, str);//匹配成功
        }
        else{
            x++;//匹配失败,指向下一条规则或url
            if(x == n) {printf("404\n"); goto next;}
            else goto lab;
        }
next:
        m--;
    }
}

//处理规则串,替换<int>
char *strApt(char *str){
    int i = 0, j = 0;
    char *new = (char *)malloc(100);
    while(str[i] != '\0'){
        if(str[i] == '<'){
            i++;
            switch(str[i]){
                case 'i':new[j] = ','; i = i + 3; break;//int
                case 'p':new[j] = ';'; i = i + 4; break;//path
                case 's':new[j] = ':'; i = i + 3; break;//str
            }
        }
        else{
            new[j] = str[i];
        }
        i++;
        j++;
    }
    new[j] = '\0';
    return new;
}

void putRst(char *map, char *str){
    int i = 0, j;
    while(map[i] != 0){
        if(str[map[i]] == '0' && str[map[i] + 1] >= '1' && str[map[i] + 1] <= '9'){
            (map[i])++;//消除前导0
        }
        for(j = map[i]; j <= map[i + 1]; j++){
            printf("%c", str[j]);
        }
        if(map[i + 2] == 0){
            printf("\n");
        }
        else{
            printf(" ");
        }
        i += 2;
    }
}

int main(){
    urlMap();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值