【UVaOJ】196 - Spreadsheet

1979年,Dan Bricklin和Bob Frankston开发了首款电子表格应用VisiCalc,该应用成为了Apple II计算机上的杀手级应用。本文介绍了一个简单的电子表格程序的设计思路,该程序能够处理数值和求和公式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



 Spreadsheet 

In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet application. It became a huge success and, at that time, was the killer application for the Apple II computers. Today, spreadsheets are found on most desktop computers.

The idea behind spreadsheets is very simple, though powerful. A spreadsheet consists of a table where each cell contains either a number or a formula. A formula can compute an expression that depends on the values of other cells. Text and graphics can be added for presentation purposes.

You are to write a very simple spreadsheet application. Your program should accept several spreadsheets. Each cell of the spreadsheet contains either a numeric value (integers only) or a formula, which only support sums. After having computed the values of all formulas, your program should output the resulting spreadsheet where all formulas have been replaced by their value.

  figure22 
Figure: Naming of the top left cells

Input

The first line of the input file contains the number of spreadsheets to follow. A spreadsheet starts with a line consisting of two integer numbers, separated by a space, giving the number of columns and rows. The following lines of the spreadsheet each contain a row. A row consists of the cells of that row, separated by a single space.

A cell consists either of a numeric integer value or of a formula. A formula starts with an equal sign (=). After that, one or more cell names follow, separated by plus signs (+). The value of such a formula is the sum of all values found in the referenced cells. These cells may again contain a formula. There are no spaces within a formula.

You may safely assume that there are no cyclic dependencies between cells. So each spreadsheet can be fully computed.

The name of a cell consists of one to three letters for the column followed by a number between 1 and 999 (including) for the row. The letters for the column form the following series: A, B, C, ..., Z, AA, AB, AC, ..., AZ, BA, ..., BZ, CA, ..., ZZ, AAA, AAB, ..., AAZ, ABA, ..., ABZ, ACA, ..., ZZZ. These letters correspond to the number from 1 to 18278. The top left cell has the name A1. See figure 1.

Output

The output of your program should have the same format as the input, except that the number of spreadsheets and the number of columns and rows are not repeated. Furthermore, all formulas should be replaced by their value.

Sample Input

1
4 3
10 34 37 =A1+B1+C1
40 17 34 =A2+B2+C2
=A1+A2 =B1+B2 =C1+C2 =D1+D2

Sample Output

10 34 37 81
40 17 34 91
50 51 71 172

====================================================================================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LOCAL 

#define MAX_ROW      1000+10
#define MAX_COLUMN   1000+10
#define MAX_STRING_LEN  1000

#define TRANS(i,j)   ((i)*MAX_COLUMN+(j))

int invalid = 1<<31;

int cell[MAX_ROW][MAX_COLUMN];
char formula[(MAX_ROW+1)*MAX_COLUMN][MAX_STRING_LEN];

int getValue(int , int);

int main(int argc, char *argv[])
{
    int index,i, j, n, row, column;
#ifdef LOCAL
       freopen("in.txt", "r",stdin);
       freopen("ou.txt", "w+",stdout);
#endif
    scanf("%d\n", &n);
    while(n--){
        scanf("%d%d\n", &column, &row);
        for (i = 1; i <= row; ++i){
           for (j = 1; j <= column; ++j){
               scanf("%s", formula[index=TRANS(i,j)]); 
               cell[i][j] = *formula[index]!='=' ? atoi(formula[index]):invalid;
           }
        }
        /*输出*/
        for (i = 1; i <= row; ++i){
            printf("%d", getValue(i,1));
            for (j = 2; j <= column; ++j)   
                printf(" %d", cell[i][j]!=invalid ? cell[i][j]:getValue(i,j));
            printf("\n");
        }
    }

    return 0;    
} 

int getValue(int r, int c)
{
    char *sp;
    int _row, _column, _res = 0;
    if(cell[r][c] != invalid)
        return cell[r][c];
    sp = formula[TRANS(r,c)]+1;
    do{
         _row = 0; _column = 0;
         while(isalpha(*sp)){
             _column = _column*26 + (*sp-'A')+1;
             sp++;
         }
         while(isdigit(*sp)){  /* 跳出循环时,sp指向'+'或者'\0' */
             _row = _row*10 + *sp-'0';
             sp++;
         }
         _res += getValue(_row, _column);
    }while(*sp++ != '\0');
    cell[r][c] = _res;       
    return _res;
}


x-spreadsheet是一个开源的JavaScript电子表格库,允许开发者轻松地在网页应用中集成电子表格功能。它提供了类似Microsoft Excel或Google Sheets的界面和功能,使得用户可以在网页上进行数据输入、编辑和计算。 以下是x-spreadsheet的一些主要特点: 1. **易于集成**:通过简单的JavaScript代码即可将x-spreadsheet集成到任何网页应用中。 2. **功能丰富**:支持单元格编辑、公式计算、格式设置、数据验证、图表生成等。 3. **高度可定制**:开发者可以根据需求自定义样式、行为和功能。 4. **跨平台**:由于是基于JavaScript的库,x-spreadsheet可以在任何支持现代浏览器的平台上运行。 5. **开源**:x-spreadsheet是开源项目,开发者可以自由查看、修改和分发代码。 使用x-spreadsheet的基本步骤如下: 1. **引入库文件**:在HTML文件中引入x-spreadsheet的CSS和JavaScript文件。 2. **创建容器**:在HTML中创建一个容器元素,用于放置电子表格。 3. **初始化**:使用JavaScript初始化x-spreadsheet,并将其绑定到容器元素上。 以下是一个简单的示例代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>x-spreadsheet Example</title> <link rel="stylesheet" href="https://unpkg.com/x-data-spreadsheet/dist/xspreadsheet.css"> </head> <body> <div id="x-spreadsheet-demo"></div> <script src="https://unpkg.com/x-data-spreadsheet/dist/xspreadsheet.js"></script> <script> const data = [ ["Name", "Age", "City"], ["Alice", 25, "New York"], ["Bob", 30, "San Francisco"] ]; const options = {}; const spreadsheet = x.spreadsheet('#x-spreadsheet-demo', data, options); </script> </body> </html> ``` 通过以上代码,你可以在网页中创建一个简单的电子表格,并进行数据输入和编辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值