如何传递外界参数进函数?用&, void move(vector<int>& window,int& cursor,char operation, int num)

HJ64 MP3光标位置

#include 
using namespace std;

void move(vector<int>& window,int& cursor,char operation, int num){
    if(num <=4){
        if(operation == 'U'){
            if(cursor == 1) cursor = num;
            cursor--;
        }
        if(operation == 'D'){
            if(cursor == num) cursor = 1;
            cursor++;
        }
    }else{
        if(operation == 'U'){
            if(cursor == 1 && window[0] == 1){
                cursor = num;
                window[0] = num - 3;
                window[1] = num - 2;
                window[2] = num - 1;
                window[3] = num - 0;
            }else if(cursor == window[0]){
                cursor--;
                window[0] = cursor;
                window[1] = cursor+1;
                window[2] = cursor+2;
                window[3] = cursor+3;
            }else if(cursor != window[0]){
                cursor--;
            }
        }
        if(operation == 'D'){
            if(cursor == num && window[3] == num){
                cursor = 1;
                window[0] = cursor;
                window[1] = cursor+1;
                window[2] = cursor+2;
                window[3] = cursor+3;
            }else if(cursor == window[3]){
                cursor++;
                window[0]++;
                window[1]++;
                window[2]++;
                window[3]++;
            }else if(cursor != window[3]){
                cursor++;
            }
        }

    }

    return;
}

int main() {
    int num;
    string operation;
    cin >> num >> operation;
    vector<int> song(num, 0);
    for(int i = 0; i < num; i++){
        song[i] = i + 1;
    }
    int cursor = 1;
    vector<int> window(4, 0);
    vector<int> tmp(num, 0);
    if(num < 4)
        window = tmp;
    for(int i = 0; i < window.size(); i++){
        window[i] = i + 1;
    }
    //每次移动cursor, update cursor & window
    for(int j = 0; j < operation.size(); j++){
        move(window, cursor, operation[j], num);
    }
    for(auto &x : window){
        cout << x << " ";
    }
    cout << endl;
    cout << cursor;
}
// 64 位输出请用 printf("%lld")

你可能感兴趣的:(c++,开发语言)