|
|
Question About General |
|
|
General Details |
| Title: |
C++ programming for HW (#343E) |
| Category: |
General |
| Inquirer: |
needhelpnow |
|
Initial Price: |
$30.00 |
| Description: |
Homework 1
A spreadsheet is a program that enables people to rapidly perform complex mathematical calculations with less risk of mistakes than traditional tools. The traditional spreadsheet is comprised of a two-dimensional grid of cells where each cell is labeled based on its coordinate in the grid, with numbers labeling the rows and letters labeling the columns. For example, the cell on the top row and left-most column would be labeled A1, the cell to its right would be B1, and the cell below it would be A2, etc.
A given cell in a spreadsheet can hold one of two different items: either a value (e.g. 3.14159) or a formula (=a1*3+a6/a2 or =average(a1:a15)). Each time the user updates the spreadsheet, it automatically recomputes the value of each cell and displays this on the screen for the user. This enables the user to enter complex formulas into the cells and rapidly change the inputs to the formulas in other cells to evaluate different scenarios.
In Homework #1, your job is to build a simple, one-dimensional spreadsheet. A one dimensional spreadsheet is much simpler than the traditional two-dimensional spreadsheet, since it contains only a single column rather than multiple columns. Each column in a one-dimensional spreadsheet is numbered by a value (the first cell is in slot #0).
As with a traditional spreadsheet, each cell may contain either a floating-point number or a formula. All formulas are of the form: =func(param1,param2) or =func(param1). Your spreadsheet must support the following formulas:
=SUM(start_row#,end_row#) Sums up the values in all cells between the starting row # and the ending row #, inclusive. (end_row# must be >= start_row#) Store the result in the cell containing this formula.
=AVG(start_row#,end_row#) Compute the average of all values in all cells between the starting row # and the ending row #. (end_row# must be >= start_row#) Store the result in the cell containing this formula.
=ADD(rowa#,rowb#) Add the value in cell[rowa] to the value in cell[rowb] and store the result in the cell containing this formula.
=SUB(rowa#,rowb#) Subtract the value in cell[rowb] from the value in cell[rowa] and store the result in the cell containing this formula.
=MUL(rowa#,rowb#) Multiply the value in cell[rowa] with the value in cell[rowb] and store the result in the cell containing this formula.
=DIV(rowa#,rowb#) Divide the value in cell[rowa] by the value in cell[rowb] and store the result in the cell containing this formula.
=SQT(rowa#) Compute the square root of the value in cell[rowa] and store the result in the cell containing this formula.
While in more complex spreadsheets, a cell can have an arbitrarily complex equation, e.g., =a1*5+sqt(b5)/c7+c5, our spreadsheet only allows the above formulas, which can be written in upper or lower case, or any combination of upper or lower-case characters.
So, consider a spreadsheet with the following contents:
Cell[0]: “5.70000”
Cell[1]: “-2”
Cell[2]: “=Sub(0,1)” // cell[2] = cell[0] – cell[1]
Cell[3]: “=gaussian(0,1)” // invalid: we don’t have a guassian formula!
Cell[4]: “12a3” // invalid: this isn’t a value or a formula
Each cell has two attributes:
1. The text the user inserted into the cell, or “user text”, e.g. “5.70000” or “=sub(0,1)”. This is obviously specified by the user and does not change unless the user manually deletes or updates the contents of the cell.
2. The “evaluated value” of a cell, which is the floating-point value of the cell if you were to evaluate the cell’s formula/value.
a. Cell 0’s evaluated value is 5.7
b. Cell 1’s evaluated value is -2
c. Cell 2’s evaluated value is 7.7 (5.7 minus -2)
d. Cell 3 doesn’t have an “evaluated value” since its contents are invalid
e. Cell 4 doesn’t have an “evaluated value” since its contents are invalid
If an arbitrary cell X contains a formula that relies upon other cells, and one or more of the other cells are invalid (i.e. they contain an invalid formula, or refer to yet other invalid cells), then cell X’s is also invalid, since it is relying upon other invalid data. For example, consider what would happen if we added a new cell, #5, to the spreadsheet above:
Cell[5]: “=add(0,3)” // cell[5] = cell[0].evaluated_value + cell[3].evaluated_value
In this case, cell #5 contains a formula that refers to one or more other cells that have an invalid “evaluated value.” Since cell #3 has an invalid formula, cell #5 also is also invalid and does not have an evaluated value.
In addition, a formula in a given cell X is considered invalid if it refers to any cells that are >= X. Said another way, a formula may only refer to cells earlier in the spreadsheet, or it is considered an invalid formula and the cell will be invalid. For example, consider the following spreadsheet:
Cell[0]: “4.444”
Cell[1]: “-2”
Cell[2]: “=avg(0,2)”
Since Cell #2 (X=2) computes the average of cells 0,1 and 2, and thus refers to cells >= X, it would have an invalid “evaluated value.” Similarly:
Cell[0]: “2.3”
Cell[1]: “=mul(0,2)”
Cell[2]: “9”
Cell #1 would have an invalid “evaluated value” because it attempts to multiply cell #0 with cell #2, and cell #2 comes after the formula in cell #1.
Finally, a cell will have an invalid “evaluated value” if it attempts any of the following operations:
The cell has a formula that performs a division by zero.
The cell has a formula that attempts to take the square-root of a cell whose “evaluated value” is negative.
Here is a C++ class definition for a one-dimensional SpreadSheet ADT:
class SpreadSheet
{
public:
SpreadSheet(),
bool insertCell(int slotNum, const std::string &userText),
bool updateCell(int slotNum,const std::string &userText),
int getNumCells(void),
bool deleteCell(int slotNum),
bool getCellText(int slotNum, std::string &userText),
bool getCellValue(int slotNum, double &value),
},
The following is a set of functional requirements for your SpreadSheet class:
Construction
SpreadSheet(),
Initially, when you create a new SpreadSheet object, it should start out empty (i.e. it should have no cells whatsoever).
The spreadsheet can hold a maximum of 100 cells.
insertCell
bool insertCell(int slotNum, const std::string &userText),
When the user uses the insertCell command, this inserts a new cell into the spreadsheet. Here are the details:
The first parameter to insertCell specifies where the newly-inserted cell will be placed. The second string parameter specifies the numeric value or formula that should be placed in that cell.
Lets assume that the spreadsheet already has N cells, numbered (0,N-1), and we’re attempting to insert a new value/formula in cell number P: insertCell(P,“42”). Here are the cases you must deal with:
If N > P, for example the spreadsheet has N=10 cells numbered 0-9, and we’re trying to insert a new cell at position P=5, then the insertCell command must shift all existing cells from position P and beyond, down by one. In other words, the old cell #5 will now become cell #6, the old cell #6 will become cell #7, etc. After the old items have been shifted down, the new value “42” will be placed in cell #P (e.g. cell #5).
If P>N (i.e. the spreadsheet currently has less than P cells, for example N=2 and P=5), then the insertCell command must first append P-N new cells to the end of the spreadsheet, each containing a value of “0” (not “0.0” and not “0.0000” but “0”), into positions N+1, N+2, …, until cell number P-1. Then insertCell should add a new cell P to the end of the spreadsheet containing the specified value/formula.
If the spreadsheet already contains 100 cells before the insertCell call, then this function should fail and return false since there is no room for additional cells in the spreadsheet.
The user may specify cell numbers between 0 and 99 to insert new cells. All other cell numbers are invalid and should return an error.
The userText parameter can either be a value, e.g. “.718”, or “-5.5”, or a formula, e.g. “=sum(3,5)”.
All formulas except for square root (SQT) will be of the form: “=fnc(##,##) where fnc is one of {add,sub,mul,div,avg,sum} and ## is a cell number (i.e. 0-99). You can never pass a value (e.g. 3.14) to a formula (e.g. =add(2,3.14) is invalid), you may only pass cell numbers.
The square root formula is of the form: “=sqt(##)” where ## is a cell number from 0 to 99. You can never pass a value (e.g. 3.14) to a sqt, you may only pass a cell number.
If a formula or a value is formatted incorrectly, e.g. “=sum[0,3}”, the insertCell method should insert the value or formula into the spreadsheet anyway.
Your method should return true on success.
Consider the following scenario:
insertCell(0,”10”), // cell[0] = 10
insertCell(2,”100”), // cell[2] = 100
insertCell(3,”1000”), // cell[3] = 1000
insertCell(4,“=ADD(0,2)”), // set cell[4] = cell[0] + cell[2]
If we were to evaluate the formula in cell #4, its value would be 110. Now consider what happens if we insert a new cell in position #2:
insertCell(2,”50”),
The above command will move all of the existing cells, in slots 2 and beyond, down by 1 and then insert the new value into slot number 2:
Cell[0]: 10
Cell[1]: 0 (automatically set to zero when 100 was inserted in cell #2)
Cell[2]: 50
Cell[3]: 100
Cell[4]: 1000
Cell[5]: =ADD(0,2)
Notice that all of our existing cells were shifted down, however, also note that the user text of each of the cells was not changed. Our formula (formerly of cell #4, now in cell #5) still refers to the same original cells 0 and 2, even though it has been shifted down by one. If we were to evaluate the formula in cell #5 now, its value would be 60.
updateCell
bool updateCell(int slotNum,const std::string &userText),
The updateCell method should update an existing slot within the spreadsheet to a new value or formula.
You may not update a cell number that does not exist. In other words, if your spreadsheet currently has 3 cells, numbered 0-2, you may not use: updateCell(3, "1245"), or updateCell(10, "=sum(0,2)"), as these would attempt to update non-existent cells. Nor may you update a negative cell #.
The updateCell method should return false in this case and not update the spreadsheet in any way.
If you specify an improperly formatted or invalid value or formula (see specifications for insertCell), then the updateCell method should still update the cell in question.
Your method should return true on success.
getNumCells
int getNumCells(void),
The getNumCells method should return the current number of cells in the spreadsheet. If the spreadsheet is empty, this should return 0.
deleteCell
bool deleteCell(int slotNum),
This deleteCell method should remove the specified cell and shift all cells after the deleted cell up by one to fill in the deleted cell’s slot.
You may not delete a cell number that does not exist and should return false without modifying the spreadsheet in this case.
As with insertCell, deleteCell does not actually change the user text in any of the remaining cells. Note that this can cause interesting problems in some situations:
insertCell(0,"42"),
insertCell(1,"2"),
insertCell(2,"=div(0,1)"),
The above spreadsheet would be completely valid, and the “evaluated value” of cell 2 would be 21 (42/2). However, if we then issued the following delete command:
deleteCell(1),
Our formula will now refer to its own cell, making it invalid:
Cell[0]: 42
Cell[1]: =div(0,1)
Thus, the next time the user attempts to retrieve cell #1’s “evaluated value,” they should get an error result.
Your method should return true on success.
getCellText
bool getCellText(int slotNum, std::string &userText),
This function should set the second parameter, userText, equal to the string text that was placed in the cell via the insertCell or updateCell command (even if the cell’s contents are formatted incorrectly or the “evaluated value” of the cell is invalid). For example:
insertCell(0,”10”), // cell[0] = 10
insertCell(2,”=sqt(5,5,5)”), // formatted incorrectly
insertCell(3,”1000”), // cell[3] = 1000
insertCell(4,“=ADD(0,3)”), // set cell[4] = cell[0] + cell[3]
string s,
getCellText(0,s), // s will be equal to “10”
getCellText(2,s), // s will be equal to “=sqt(5,5,5)” even
// though the formula is invalid
getCellText(1,s), // s should be “0” since slot #1 was
// automatically filled with a “0” when we
// inserted the formula into cell #2
If slotNum is >= the maximum number of items in the spreadsheet, or is otherwise invalid (e.g. negative), then return false and don’t update userText.
Otherwise, the function should return true.
getCellValue
bool getCellValue(int slotNum, double &value),
This method returns the “evaluated value” of a cell to the user in the value parameter, returning true on success or false on failure.
If the cell being evaluated (e.g. slotNum) contains a value and not a formula, then you can quickly convert this value from a string into a double (floating-point variable) and return it to the user via the second reference parameter.
However, if the cell being evaluated contains a formula, then the formula must be evaluated before returning a result. However, since a formula, by definition, refers to earlier cells in the spreadsheet, each potentially with its own formula, you have to ensure that all cells referred to by the current cell are evaluated, and all cells referred to by referred cells are evaluated, etc. before returning the value to the user. There are several ways to do this, the easiest one (but least efficient) is as follows: Your method can (either on its own or by calling another method) calculate all “evaluated values” in the spreadsheet before and including the evaluated cell. For each cell, j, in the spreadsheet up to and including slotNum (from 0 to slotNum):
Compute the value of cell[j]
If cell[j] is a number, then the “evaluated value” of the cell is simply the floating point value of that number. Set the “evaluated value” for cell j to this value.
If cell[j] is a formula, then you should attempt to evaluate the formula:
Given a formula that refers to cells X and Y (e.g. “=add(5,7)” refers to X=5 and Y=7), you should fetch the “evaluated value” of cell #X (call this Ex), and fetch the “evaluated value” of cell #Y (call this Ey) and then apply the formula to Ex and Ey. So in the above example, we’d add Ex + Ey to obtain our result.
Since all valid formulas must refer to earlier cells, we know that by the time we evaluate a formula in cell j that refers to cells X and Y, X |
| Question Expires: |
Question closed |
| End Time: |
2006-04-27 20:11:53 Started:
2006-04-13 20:11:53
|
| |
|
|