3484: Design-Spreadsheet
Medium
table of contents
This question is basically just a simple implementation question.
The Spreadsheet itself is just a 2D array, with 26
vector<int> of size row+1:
class Spreadsheet {
public:
Spreadsheet(int rows) {
spreadsheet.resize(26, vector<int>(rows+1, 0));
}
// ...
private:
vector<vector<int>> spreadsheet;
// ...
};To make the operations simpler to implement, I created two helper functions.
The first one that I implemented was returnCoordinate,
which would return the numerical indices of a cell from its
string representation.
pair<int, int> returnCoordinate (string cell) {
int col = cell[0]-'A';
int row = stoi(cell.substr(1, cell.size()-1));
return {col, row};
}Since equations always followed the format of =v1+v2,
where v1 and v2, we can write a function
parseVariable to convert v1 &
v2 into numerical form, handling the cases where
v1 & v2 are either non-negative
integers or cell references:
int parseVariable(int index, string formula) {
if (formula[index]-'0' >= 0 && formula[index]-'0' <= 9) {
int endIndex = index;
while (endIndex < formula.size() && formula[endIndex]-'0' >= 0 && formula[endIndex]-'0' <= 9) {
++endIndex;
}
return stoi(formula.substr(index, endIndex-index));
} else {
int endIndex = index;
while(endIndex < formula.size() && formula[endIndex] != '+') {
++endIndex;
}
pair<int, int> coords = returnCoordinate(formula.substr(index, endIndex-index));
return spreadsheet[coords.first][coords.second];
}
}Now, using the two helper functions, we can define all the other class functions.
For setCell, we can simply use
returnCoordinate to update the respective cell in
spreadsheet with value:
void setCell(string cell, int value) {
pair<int, int> coords = returnCoordinate(cell);
spreadsheet[coords.first][coords.second] = value;
}For resetCell, we can do the same thing as
setCell, except update the respective cell to a value of
0:
void resetCell(string cell) {
pair<int, int> coords = returnCoordinate(cell);
spreadsheet[coords.first][coords.second] = 0;
}Finally for getValue, since we are always summing two
values together, we can use our handy parseVariable
function to get the firstVariable and
secondVariable, and simply return the sum of both
values:
int getValue(string formula) {
int index = 1;
int firstVariable = parseVariable(1, formula);
int secondVariable = parseVariable(formula.find('+')+1, formula);
return firstVariable + secondVariable;
}code
class Spreadsheet {
public:
Spreadsheet(int rows) {
spreadsheet.resize(26, vector<int>(rows+1, 0));
}
void setCell(string cell, int value) {
pair<int, int> coords = returnCoordinate(cell);
spreadsheet[coords.first][coords.second] = value;
}
void resetCell(string cell) {
pair<int, int> coords = returnCoordinate(cell);
spreadsheet[coords.first][coords.second] = 0;
}
int getValue(string formula) {
int index = 1;
int firstVariable = parseVariable(1, formula);
int secondVariable = parseVariable(formula.find('+')+1, formula);
return firstVariable + secondVariable;
}
private:
vector<vector<int>> spreadsheet;
pair<int, int> returnCoordinate (string cell) {
int col = cell[0]-'A';
int row = stoi(cell.substr(1, cell.size()-1));
return {col, row};
}
int parseVariable(int index, string formula) {
if (formula[index]-'0' >= 0 && formula[index]-'0' <= 9) {
int endIndex = index;
while (endIndex < formula.size() && formula[endIndex]-'0' >= 0 && formula[endIndex]-'0' <= 9) {
++endIndex;
}
return stoi(formula.substr(index, endIndex-index));
} else {
int endIndex = index;
while(endIndex < formula.size() && formula[endIndex] != '+') {
++endIndex;
}
pair<int, int> coords = returnCoordinate(formula.substr(index, endIndex-index));
return spreadsheet[coords.first][coords.second];
}
}
};
/**
* Your Spreadsheet object will be instantiated and called as such:
* Spreadsheet* obj = new Spreadsheet(rows);
* obj->setCell(cell,value);
* obj->resetCell(cell);
* int param_3 = obj->getValue(formula);
*/