0

i am looking to create a simple spreadsheet application where users are allowed to customize the rows and columns based on their input.

for example user inputs, newSS 10 9

which means user wants to create a newSpreadSheet of 10 columns and 9 rows.

an empty spreadsheet should be created and displayed in the console window.

the catch here is that there will always be an extra row and column for which the user has entered as the extra rows and columns are for the headers.

so 10 columns and 9 rows will have 11 columns and 10 rows stored in a 3D array because the first index of every column and row are the headers. it should look something like this.

first row would show: A B C D E F G H I J

first column would show: 1 2 3 4 5 6 7 8 9

as their labels

the spaces in between are their paticular index say A1, A2, B4 etc.. which the user can then edit the values inside.

can anyone teach me how to get started? am a very new c programmer here. am facing some issues with the codes. am able to create a 2d array but i need to alter the first row and first column to print it as the header according to how many rows and columns the users wants. so if 5 columns it will be (A-E)

WORKSHEET *ws_new(int cols, int rows) {

//return NULL;

int r;
int c;
int n[rows][cols];

for (r=1;r<=rows;r++)
{
    for(c=1;c<=cols;c++)
    {
        //printf("%d",cols);
        n[r][c]=0;
        printf("%d", n[r][c]);
    }
    printf("\n");
}

}
notknow
  • 1
  • 2
  • You've mentioned that you've written some code and that it's not working however I failed see any code snippets in your post. Please provide what you've done so far. – rbaleksandar Oct 19 '18 at 05:29
  • added the codes – notknow Oct 19 '18 at 05:37
  • If you're just looking for a text based spreadsheet, there's a number of options (like emacs' [SES mode](https://www.gnu.org/software/emacs/manual/html_mono/ses.html) or [sc-im](https://github.com/andmarti1424/sc-im)). – Shawn Oct 19 '18 at 06:10

1 Answers1

2

I am looking to make a simple spreadsheet application

Spreadsheets cannot be a simple application. They have two related components which are complex:

  • a nice GUI presenting a table of cells (you could also consider a terminal interface, using something like ncurses, but that won't be simpler than providing a simple GUI). If your program don't provide some tabular interface, don't call it a spreadsheet.

  • a lazy "functional" interpreter, running in each spreadsheet cell (or interpreting a 2D array of formulas); you have some scripting language involved, and your spreadsheet has conceptually some matrix of formulae.

You could look (for inspiration) into the source code of existing free software spreadsheets, e.g. Gnumeric

For the GUI part, use some existing GUI toolkit. Since you want to code that in C, consider GTK.

For the interpreter part, read first the Dragon Book (after having read SICP), something like Programming Languages Pragmatics and probably Lisp In Small Pieces. If you want to parse your own formula language, read more about parsing techniques, recursive descent parsing, and look into bison infix calc example.

You need at least several months, and perhaps several years, of work.

You might embed some existing interpreter (instead of designing and implementing your own one). Consider using Lua or Guile.

Your incomplete code is conceptually wrong: what a spreadsheet needs to have is some matrix of formulae, not just of numbers (like your n array). Each cell contains a formula (or a tagged union of formula and plain number), and you want to keep the AST of that formula. A cell apparently containing a number is a degenerate case of a formula reduced to a constant number.

This answer shows how to implement a numerical matrix as some abstract data type. It could inspire you to represent a spreadsheet as some matrix of formulae. Of course you need to also have a type for the AST of your formulae.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I have had Aho's book for about 30 years: didn't know it was called the Dragon book. Learn something new everyday. – cup Oct 19 '18 at 05:34
  • i dont need a GUI. i just need to be able to let users edit in the console window and save it into a .csv file thats all. but i would like to get started by knowing how to create the table first. – notknow Oct 19 '18 at 05:37
  • I don't understand how you would let users edit in the console window? Are you considering using some terminal related library like [ncurses](https://www.gnu.org/software/ncurses/) – Basile Starynkevitch Oct 19 '18 at 05:38
  • there will be a specific function for that. like for example.. 'change A6 139' it means change value in the cell/pointer of column A and row 6 to number 139 – notknow Oct 19 '18 at 05:40
  • That is not a function, and what you are making is not a spreadsheet (because the user don't see at once the table of cells every spreadsheet has) – Basile Starynkevitch Oct 19 '18 at 05:41
  • Why the downvotes? I improved my answer to follow the evolution of OP's question! – Basile Starynkevitch Oct 19 '18 at 05:52
  • 3
    I disagree with the premise of the answer. Of course one can make a simple spreadsheet application. Telling a beginner that it's all or nothing – that they need all the sophistication of Gnumeric and that this has to take months or years of work – is ridiculous. There's nothing wrong with a matrix of `double`s to start out. – Maxpm Oct 19 '18 at 06:04
  • And then you find out that the matrix is that of integers... – Ruks Oct 19 '18 at 06:26
  • If they are no lazily evaluated formulae, you don't have a spreadsheet anymore – Basile Starynkevitch Oct 19 '18 at 07:21