suxxsfe

一言(ヒトコト)

ds作业2

3.7

bool check(char *a){//return true if the expression is legal
    stack<char> s;
    for(; *a; a++){
        if(*a == ')'){
            if(s.top() != '('){
                return false;
            }
            s.pop();
        }
        else if(*a == ']'){
            if(s.top() != '['){
                return false;
            }
            s.pop();
        }
        else if(*a == '}'){
            if(s.top() != '{'){
                return false;
            }
            s.pop();
        }
        else if(*a == '(' || *a == '[' || *a == '{'){
            s.push(*a);
        }
        else{
            return false;
        }
    }
    return s.empty();
}

3.8

int level(char c){
    if(c == '+' || c == '-'){
        return 1;
    }
    else if(c == '*' || c == '/'){
        return 2;
    }
    return 0;
}
void trans(char *a, char *t){
    stack<char> s;
    for(; *a; a++){
        if(*a >= 'a' && *a <= '9'){
            *t++ = *a;
        }
        for(;;){
            if(*a == '(' || s.empty() || level(*a) > level(s.top())){
                s.push(*a);
                break;
            }
            char now = s.top();
            s.pop();
            if(now != '('){
                *t++ = now;
            }
            else{
                break;
            }
        }
    }
    while(!s.empty()){
        *t++ = s.top();
        s.pop();
    }
}

3.9

double calc(char *a){
    stack<double> s;
    double x, y, z;
    bool ff = false;
    for(; *a; a++){
        if(*a == '+' || *a == '-' || *a == '*' || *a == '/'){
            ff = false;
            x = s.top();
            s.pop();
            y = s.top();
            s.pop();
        }
        if(*a == '+'){
            s.push(x+y);
        }
        else if(*a == '-'){
            s.push(x-y);
        }
        else if(*a == '*'){
            s.push(x*y);
        }
        else if(*a == '/'){
            s.push(x/y);
        }
        else if(*a >= '0' && *a <= '9'){
            ff = true;
            z = z*10+(*a-'0');
        }
        else if(*a == ' ' && ff){
            ff = false;
            s.push(z);
        }
    }
    return s.top();
}

3.10

void init(Queue *q){
    q->tail = NULL;
}
void push(Queue *q, Node x){
    QueueNode *t = new QueueNode;
    t->value = x;
    if(!q->tail){
        t->next = t;
        q->tail = t;
        return;
    }
    t->next = q->tail->next;
    q->tail->next = t;
    q->tail = t;
}
Node pop(Queue *q){
    if(q->tail->next == q->tail){
        Node ans = q->tail->value;
        delete q->tail;
        q->tail = NULL;
        return ans;
    }
    QueueNode ans = *(q->tail->next);
    delete q->tail->next;
    q->tail->next = ans->next;
    return ans->value;
}

3.11

队满条件:q->length == MAX_SIZE

void push(Queue *q, Node x){
    q->rear = (q->rear+1)%MAX_SIZE;
    q->a[q->rear] = x;
    q->length++;
}
Node pop(Queue *q){
    Node x = q->a[(q->rear-q->length+1+MAX_SIZE)%MAX_SIZE];
    q->length--;
    return x;
}
posted @ 2024-03-28 17:50  suxxsfe  阅读(4)  评论(0编辑  收藏  举报