/* * CSE 373, Project #3 * Autumn 2001 * * Name: ??? * Date: ??? * * main.cpp * * The driver program of a spreadsheet application. * */ #include #include "mystring.h" #include "util.h" /* You will need to define a Spreadsheet class * in spreadsheet.h and spreadsheet.cpp that has * at least the following functions. */ class Spreadsheet { public: void printValues() {}; void printFormulas() {}; void changeCellFormula(CellToken cellID) {}; void read(ifstream infile) {}; void save(ofstream outfile) {}; }; void MenuPrintValues(Spreadsheet & s) { s.printValues(); } void MenuPrintFormulas(Spreadsheet & s) { s.printFormulas(); } void MenuChangeCellFormula(Spreadsheet & s) { CellToken cellID; Stack expTreeTokenStack; ExpressionTreeToken expTreeToken; cout << "Enter the cell to change: "; getcellToken (cin, cellID); cout << "column: " << cellID.column << endl; cout << "row : " << cellID.row << endl; // error check to make sure the row and column // are within spreadsheet array bounds. cout << "Enter the cell's new formula: "; getformula (cin, expTreeTokenStack); // This code prints out the expression stack from // top to bottom (that is, reverse of postfix). while (!expTreeTokenStack.isEmpty()) { expTreeToken = expTreeTokenStack.topAndPop(); printExpressionTreeToken(expTreeToken); } s.changeCellFormula(cellID); } void MenuReadSpreadsheet(Spreadsheet & s) { string filename; ifstream infile_stream; // Get the input file name from the user cout << "Enter a file name to read from: "; getword (cin, filename); infile_stream.open(filename.c_str(), ios::nocreate); if (!infile_stream) { cout << "Error: Could not open " << filename << "." << endl; return; } cout << "Reading " << filename << " ... "; s.read(infile_stream); cout << "Done." << endl; } void MenuSaveSpreadsheet(Spreadsheet & s) { string filename; ofstream outfile_stream; // Get the output file name from the user cout << "Enter a file name to save to: "; getword (cin, filename); outfile_stream.open(filename.c_str()); if (!outfile_stream) { cout << "Error: Could not open " << filename << "." << endl; return; } cout << "Writing " << filename << " ... "; s.save(outfile_stream); cout << "Done." << endl; } int main() { Spreadsheet s; bool done = false; string command; cout << ">>> Welcome to the CSE 373 Spreadsheet <<<" << endl << endl; while (!done) { cout << "Choose from the following commands:" << endl; cout << endl; cout << "p: print out the values in the spreadsheet" << endl; cout << "f: print out the formulas in the spreadsheet" << endl; cout << "c: change the formula of a cell" << endl; cout << "r: read in a spreadsheet from a textfile" << endl; // BONUS: // cout << "s: save the spreadsheet to a textfile" << endl; cout << endl; cout << "q: quit" << endl; cout << endl; cout << "Enter your command: "; getword (cin, command); // We care only about the first character of the string switch (command[0]) { case 'p': MenuPrintValues(s); break; case 'f': MenuPrintFormulas(s); break; case 'c': MenuChangeCellFormula(s); break; case 'r': MenuReadSpreadsheet(s); break; /* BONUS: case 's': MenuSaveSpreadsheet(s); break; */ case 'q': done = true; break; default: cout << command[0] << ": Bad command." << endl; break; } cout << endl; } cout << "Thank you for using our spreadsheet." <