spreadsheet program in c++

Posted on 2003-03-28
Medium Priority
Last Modified: 2008-03-10
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.
Question by:nw_khaemba
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 12

Expert Comment

ID: 8225325
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?


Author Comment

ID: 8229853
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
LVL 12

Accepted Solution

Salte earned 135 total points
ID: 8229987
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.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question