qiaobaaa / response3

0 stars 0 forks source link

intsant #14

Open qiaobaaa opened 1 year ago

qiaobaaa commented 1 year ago

include

include

include

using namespace std;

string dbStr[1007]; int value[1007]; int len; int idx; int total;

int calculate(string str, int start, int end) { int ans = 0; int sign = 1;

for (register int i = start; i < end; i++)
{
    int tem = 0;
    if (str[i]=='+')
        sign = 1;
    if (str[i] == '-')
        sign = -1;
    if (str[i] >= '0' && str[i] <= '9')
    {
        tem = str[i] - '0';
        if (i + 1 < end && str[i + 1] >= '0' && str[i + 1] <= '9')
        {
            tem = tem * 10 + (str[i + 1] - '0');
            i++;
        }
    }
    if (i + 1 < end && str[i + 1] == '*')
    {
        int x = 0;
        i += 2;
        x = str[i] - '0';
        if (i + 1 < end && str[i + 1] >= '0' && str[i + 1] <= '9')
        {
            x = x * 10 + (str[i + 1] - '0');
            i++;
        }
        tem = tem * x;
    }
    ans += tem * sign;
}

return ans;

}

void init(int mLen, char mSubexp[]) { len = mLen + 1; idx = 0; total = 0; dbStr[idx] = mSubexp; dbStr[idx] = '+' + dbStr[idx]; value[idx] = calculate(dbStr[idx], 0, len); total += value[idx]; idx++; }

int append(char mLink, char mSubexp[]) { dbStr[idx] = mSubexp; dbStr[idx] = mLink + dbStr[idx]; value[idx] = calculate(dbStr[idx], 0, len); total += value[idx]; idx++; return total; }

int erase(int mFrom) { int r = mFrom / len; int c = mFrom % len;

total -= value[r];
if (c != 0) {
    total -= value[r + 1];
    for (int i = c; i < len; i++) {
        dbStr[r][i] = dbStr[r + 1][i];
    }
    value[r] = calculate(dbStr[r], 0, len);
    total += value[r];
    r++;
}
for (int i = r; i < idx; i++) {
    dbStr[i] = dbStr[i + 1];
    value[i] = value[i + 1];
}
idx--;
return total;

}

int select(int mFrom, int mTo) { int ans = 0; int sr = mFrom / len; int sc = mFrom % len; int er = mTo / len; int ec = mTo % (len);

if (sr == er) {
    ans = calculate(dbStr[sr], sc, ec + 1);
    return ans;
}
ans += calculate(dbStr[sr], sc, len);
ans += calculate(dbStr[er], 0, ec + 1);
for (int x = sr + 1; x < er; x++) {
    ans += value[x];
}
return ans;

}