• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

spreadsheet program in c++

Is there a way of writing a spreadsheet application in c++ with the various diplays. if possible can you give a sample code coz I'm trying to come up with one.
  • 2
1 Solution
Not sure what you mean by 'sample code'. If 'sample code' is to be of any value in situations like this it will quickly tend to be very close to a regular full program and many lines of code.

If you want me to write a spreadsheet program for you I can do so but then you have to pay me.

A spreadsheet program is in principle simple enough.

The main thing is that you have a collection of cells and a cell can hold a value. Some of these values are actually formulae and whenever any of the cells they depend on their value will be automatically updated. There are several ways to organize this in a data structure but whatever way is chosen will quickly be unusable by anyone who has chosen a different data structure so there's little point of me to give you much details around this.

If you just want to handle standard data types (for example all simple values are 'double') it is very simple indeed, if you want to handle several data types perhaps multiple precision etc it can get more complicated but the principles are still very simple. Personally I would go for the multiple types scenario if I were you because then you can simply a 'formula' be just another type so a cell hold a value and the type can be a double, int, bool, string, formula etc etc. and 'formula' is simply just another data type and the value is some data structure that describes how to compute the formula.

Do you want to write this for Windows or X-windows or what?

nw_khaembaAuthor Commented:
Thanks I would like to write it for windows but how to get the features to help come up with the cells is the problem
Well, a cell is simply something like this.

Assuming a cell can hold values and formulae, in addition it might have other properties as well.

A formula is simply just another value but which is such that when evaluated it will execute various functions instead of simply returning a stored value.

class Value; // includes formulae

class Cell {
  Value  * value() const;

  void Parse(const string & str);

  int row() const;
  int column() const;

   .. other properties....
  for example you could have a collection of references to
  all cells that depends on this cell for their value.
  I.e. if this cell is modified, those cells should be
  modified as well.

   Value * m_value;
   int m_row;
   int m_column;

   ...other data...

enum value_type {
   no_value = 0, // empty cell

class Value {
   virtual ~Value() {}

   virtual value_type type() const = 0;

   // formulae are displayed as themselves.
   virtual void just_display(ostream & os) const = 0;

   // formulae are evaluated and the result is
   // displayed.
   virtual void eval_and_display(ostream & os) const = 0;

class dblValue : public Value {
   dblValue(double val = 0.0) : m_value(val) {}

   virtual value_type type() const { return double_value; }

   virtual void just_display(ostream & os) const
   { os << m_value; }

   virtual void eval_and_display(ostream & os) const
   { os << m_value; }

   double m_value;

class formulaValue : public Value {
    virtual value_type type() const { return formula_value; }


Then make subclasses of formula depending on the what the formula is supposed to do.

Another way would be that FormulaValue would be a class containing a pointer to an Formula class object that holds the actual formula to compute.

One thing might need a comment and it is the storing of the row and column. A spreadsheet has lots of cells and if they come nicely in a row you might just store them as a matrix.

However, many of those cells will be empty and you will spend a lot of storage storing those empty cells so I would believe it is a better approach to have the cells that are non-empty remember what their row and column are.

Of course, you then also need some data structure to find a cell given row and column but that is a simple std::map using the combination of row and column as key and a Cell * as the value.

Hope this is enough hint to get you started.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now