2019-3-3 損壞的RAID5

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;
int n, s, l, len, m;//n硬盤數,s每個條帶s塊,l可用硬盤數, m操作次數
string str[1001];
char f(char c1){
    if(c1<10)
        return c1+'0';
    if(c1<16)
        return c1+'A'-10;
    if(c1<'9')
        return c1-'0';
    if(c1<'F')
        return c1-'A'+10;
}
string func(string &ss, string s1){
    for(int i = 0; i < 8; i++){
        ss[i] = f(f(ss[i])^f(s1[i]));
    }
}
void read(int tmp){
    int disk = (tmp/s)%n;//該塊位於哪個硬盤
    int depth = (tmp/s)/(n-1);//改塊位於硬盤的第幾條,0爲第一條
    int ranknum = depth*s+tmp%s;//該塊位於硬盤第幾塊,0爲第一塊
    int currentlen = str[disk].length();
    if(tmp>len/8*(n-1)){
        cout << "-" << endl;
        return;
    }
    if(currentlen > 0){
        cout << str[disk].substr(ranknum*8,8) << endl;
        return;
    }
    if(l==n-1){
        string ss = "00000000";

        for(int i = 0; i < n; i++){
            if(i!=disk){
                func(ss,str[i].substr(ranknum*8,8));
            }
        }
        cout << ss << endl;
        return;
    }
    cout << "-" << endl;

}
int main()
{
    ios::sync_with_stdio(false);//避免c++中cin操作超時。
    cin>>n>>s>>l;
    int tmp;
    for(int i = 0; i < l; i++){
        cin>>tmp;
        cin>>str[tmp];//將數據放入第tmp塊硬盤
    }
    if(l==0)
        len = 0;
    else
        len = str[tmp].length();
    cin >> m;
    for(int i = 0; i < m; i++){
        cin>>tmp;
        read(tmp);//讀取tmp塊數據
    }
    return 0;
}

運行錯誤,是由於輸入的查詢塊數可能非法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章