北邮OJ-273. Python List-14计院上机(第二套)D

又用到之前编的NameForm技巧了。这题不难就是很杂,字符串与下标映射表的管理,线性表的管理,字符串处理,简单排序。

题目

#include 
#include 
#include 
#include 
#define MAXSIZE 50
using namespace std;

char operation[30];
vector<int> list[MAXSIZE];
int p;

struct NameForm{
    char form[MAXSIZE][20];
    int nameSize;
    int getListId(char *name){
        for (int i=0;iif (strcmp(form[i],name)==0)
                return i;
        }
        //didnt find
        strcpy(form[nameSize],name);
        nameSize++;
        return nameSize-1;
    }
};

void getData(vector<int> &nowList){
    int id=0;
    //get id
    p++;
    while (operation[p]!=']'){
        id*=10;
        id+=operation[p]-'0';
        p++;
    }
    //output
    if (id>=nowList.size())
        printf("ERROR\n");
    else
        printf("%d\n",nowList[id]);
}
void append(vector<int> &nowList){
    int x=0;
    while (operation[p]!='(')
        p++;
    p++;
    while (operation[p]!=')'){
        x*=10;
        x+=operation[p]-'0';
        p++;
    }
    nowList.push_back(x);
}
void sort(vector<int> &nowList){
    sort(nowList.begin(),nowList.end());
}
int main(){
    int t,n;
    char name[20];
    NameForm nameForm;
    scanf("%d",&t);
    while (t--){
        //input
        scanf("%d",&n);
        //initiate
        for (int i=0;ilist[i].clear();
        }
        nameForm.nameSize=0;
        //operate
        for (int i=0;iscanf("%s",operation);
            p=0;//initiate p
            while (operation[p]!='.'&&operation[p]!='='&&operation[p]!='[')
                p++;
            //copy name && get nowList
            for (int i=0;i'\0';
            vector<int> &nowList=list[nameForm.getListId(name)];
            //operate
            if (operation[p]=='='){
                nowList.clear();
            }
            else if (operation[p]=='['){
                getData(nowList);
            }
            else{
                p++;
                if (operation[p]=='a'){
                    append(nowList);
                }
                else if (operation[p]=='s'){
                    sort(nowList);
                }
            }
        }
    }
    return true;
}

你可能感兴趣的:(北邮OJ-14计院2)