Stored Formulae

Discussion in 'C++' started by Tazsweet19, Nov 11, 2009.

  1. Tazsweet19

    Tazsweet19 Banned

    Joined:
    Nov 11, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I tried to understand about A calculate function which causes all of the stored formulae to be executed and the result stored in the cell that contains the formula while retaining the formula for reuse.

    There is code

    Code:
    
    #include "stdafx.h"
    #include <stdlib.h>
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <exception>
    #include <string>
    #include <cstring>
    #include <math.h>
    using namespace std;
    
    
    // The Calculator will take +,*,-,/ with the cell for A-F and numbers.
    class Calculator { 
    
    private:
    
        float result; //The result is total
    
    public:
        //bool flag; // Printed cannot divide by zero message flag
        //The calcResult will figure out with +,-,*, or / then result where is cell with A-F and number.
        // but The divide can't take zero and order the follow.
        float calcResult(char oper, float num1, float num2) {
            if (oper == '+') {
                result = (num1 + num2);
            } else if (oper == '-') {
                result = (num1 - num2);
            } else if (oper == '*') {
                result = (num1 * num2);
            } else if (oper == '/') {
                if (num2 == 0) {
                    //if (!flag) {
                    cout << "cannot divide by zero.\a\n\n";
                //    }
                } else {
                    result = (num1 / num2);
                }
            } else {
                cout << "Please follow the formatting guide.\n\n";
                result = 0;
            }
            return result;
        }
    };
    
    
    
    //The spreadsheet will take where is rows and cols with max 10.
    class SpreadSheet { 
    
    int x,y;
    private:
    
    static const int nRows = 10; //y number of rows (max 10)
    static const int nCols = 6; //x letter of columns (max 10)
    float sheet[nRows][nCols]; //Sheet nRows & nCols
    char* sheetFunction[nRows][nCols]; //sheetFunction nRows & nCols*****
    string usrDef[10];
    Calculator cal;
    
    // The initArray will loop with Rows and cols for numbers.
    public:
    
    ofstream toFile;
    ifstream fromFile;
    
        void initArray() {
            x=65; y=48;
            float k = 0;
            for (int i = 0; i < nRows; i++) {
                for (int j = 0; j < nCols; j++) {
                    sheet[i][j] = k; 
                    sheetFunction[i][j] =0;// (char *)malloc(80); //max chars
                    // replace x with x++ to enumerate cells
                }
            }
            for (int i = 0; i < 10; i++) {
                usrDef[i] = "";
            }
        }
    
        //The line is set up A-F and number with rows and cols.
        void displayHdr() { 
            //system("clear");
            cout << "\n|" << setw(4) << "" << "\t";
            for (int i = 0; i < nCols; i++) {
                cout << "|" << setw(9) << static_cast<char> (i + 65) << "\t";
            }
            cout << "|\n" << "        ";
            for (int x = 0; x < nCols; x++) {
                cout << "----------------";
            }
            cout << "\n";
        }
    
        // The displayRows will show output for line and help.
        void displayRows() { 
            for (int i = 0; i < nRows; i++) {
                cout << "|" << setw(4) << i << "\t";
                for (int j = 0; j < nCols; j++) {
                    if (sheetFunction[i][j]!=0) processInput(sheetFunction[i][j]);
                    cout << "| " << setw(8) << sheet[i][j] << "\t";
                }
                cout << "|\n" << "        ";
                for (int x = 0; x < nCols; x++) {
                    cout << "----------------";
                }
                cout << "\n";
            }
            cout << "\t**enter H to display the help screen**";
        }
    
        //The DisplayUrsDef will loop with i then set/get will
        //call usrDef.
        void displayUsrDef() {
            for (int i = 0; i < 10; i++) {
                if (usrDef[i] != "") {
                    cout << setw(4) << i << ") " << usrDef[i] << "\n";
                }
            }
            cout << "\n";
        }
        
        // SetUsrDef with string X and int i.
        void setUsrDef(int i, string x) {
            usrDef[i] = x;
        }
    
        //string getUsrDef will call i.
        string getUsrDef(int i) {
            return usrDef[i];
        }
    
        // setCell will call the int with row and col
        void setCell(int r1, int c1, float x) {
            sheet[r1][c1] = x;
        }
    
        // float getcell with row and col
        float getCell(int r1, int c1) {
            return sheet[r1][c1];
        }
        bool processInput(string input){
            if (toupper(input[0]) == 'Q') {
                    cout << "\nGoodbye\n";
                    exit(0);
                }
    
                else if (toupper(input[0]) == 'H') {
                    //system("clear");
                    cout << "\n**Welcome to the Help Screen**\n\n";
                    cout << "  Enter a value:          = A0 12.34\n";
                    cout << "  Perform operation:      + A0 A1 A2\n";
                    cout << "    +  -  *  /  \n\n";
                    cout << "  User Defined Functions:  \n";
                    displayUsrDef();
                    cout << "  Quit program:            Q\n";
                    cout << "\n\n**press any key to continue**\n";
                    //system("read $variable");
                }
                else if (input[0] == '=') { 
                    cout << "entered '=' sign\n";
                    char temp[10] = {};
                    for (int i = 5; i < input.length(); i++) {
                        temp[i - 5] = input[i];
                    }
                    setCell((input[3] - y), (toupper(input[2]) - x), atof(temp));
                } else if (input[0] == '?') {
                    cout << "entered new function\n";
                    char* in=&input[0];
                    sheetFunction[input[3] - y][toupper(input[2]-x)]=(char *)malloc(80);
                    strcpy(sheetFunction[input[3] - y][toupper(input[2]) - x],in);
                    //then when redrawing you need to execute stored code
                    char temp[10] = {};
                    for (int i = 0; i < input.length(); i++) {
                        temp[i] = input[i];
                    }
                    // no input[2] if just ? poorly coded you assume things trying to read 3rd char but there is none
                    setUsrDef((input[2] - y), temp);
                } else if (input[0] >= 0 && input[0] <= 9) {
    
                    string temp = getUsrDef(input[0]);                
                    //printf("debug %s\n",sheetFunction[input[3] - y][toupper(input[2]) - x]);
                } else if (input[0] == '+') {
                    //cout << "entered '+' sign.\n";
                    char* in=&input[0];
                    sheetFunction[input[3] - y][toupper(input[2]-x)]=(char *)malloc(80);
                    strcpy(sheetFunction[input[3] - y][toupper(input[2]) - x],in);
                    float temp = cal.calcResult('+',
                            getCell((input[3] - y), (toupper(input[2]) - x)),
                            getCell((input[6] - y), (toupper(input[5]) - x)));
                            
                    setCell((input[9] - y), (toupper(input[8]) - x), temp);
                } else if (input[0] == '-') {
                    //cout << "entered '-' sign.\n";
                    char* in=&input[0];
                    sheetFunction[input[3] - y][toupper(input[2]-x)]=(char *)malloc(80);
                    strcpy(sheetFunction[input[3] - y][toupper(input[2]) - x],in);
                    float temp = cal.calcResult('-',
                            getCell((input[3] - y), (toupper(input[2]) - x)),
                            getCell((input[6] - y), (toupper(input[5]) - x)));
                    setCell((input[9] - y), (toupper(input[8]) - x), temp);
                } else if (input[0] == '*') {
                    //cout << "entered '*' sign.\n";                
                    char* in=&input[0];
                    sheetFunction[input[3] - y][toupper(input[2]-x)]=(char *)malloc(80);
                    strcpy(sheetFunction[input[3] - y][toupper(input[2]) - x],in);
                    float temp = cal.calcResult('*',
                            getCell((input[3] - y), (toupper(input[2]) - x)),
                            getCell((input[6] - y), (toupper(input[5]) - x)));
                    setCell((input[9] - y), (toupper(input[8]) - x), temp);
                } else if (input[0] == '/') {
                    //cout << "entered '/' sign.\n";
                    char* in=&input[0];
                    sheetFunction[input[3] - y][toupper(input[2]-x)]=(char *)malloc(80);
                    strcpy(sheetFunction[input[3] - y][toupper(input[2]) - x],in);
                    float temp = cal.calcResult('/',
                            getCell((input[3] - y), (toupper(input[2]) - x)),
                            getCell((input[6] - y), (toupper(input[5]) - x)));
                    setCell((input[9] - y), (toupper(input[8]) - x), temp);
                } else { 
                    cout << "Invalid entry." << endl;
                }
                // Clear printed divide by zero flag.
            //cal.flag = false;
        }
    };
    
    
    
    //UserInput will take the string. It will call input where 
    // in the cell with alpha and number.
    class UserInput { 
    private:
    
        string input;
    
    public:
    
        string getInput(string s) {
            cout << "\n\n\t[" << s << "]: ";
            getline(cin, input);
            return input;
        }
    };
    
    
    // The main will output where is cell with number and aphla for *,-,/ or +,
    // The welcome to help screen and type the word where is the cell. The help call
    // the example "= A0 12.34" and "+ A0 A1 A2".
    int main() {
    
        SpreadSheet ss;
        UserInput ui;
        Calculator cal;
        bool b = false;
        static int x = 65;
        static int y = 48; 
    
        cout << "\n\n";
        ss.initArray();
        //system("clear");
    
        while (!b) { 
            while (!b) { 
                ss.displayHdr();
                ss.displayRows();
                b = true;
            }
    
            while (b) { 
                string input = ui.getInput("entry");
                b=ss.processInput(input);
                  //     b = false; 
            }
        }
        cout << "\n\n";
    
        return (0);
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What is the problem or question that you have about this code?
    Does it work as you expect?
    If not, what does it do wrong that you thought it wouldn't? What behaviour did you expect? Did you give the program any relevant input?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice