uva 196 spreadsheet

来源:互联网 发布:php网络编程 编辑:程序博客网 时间:2024/05/17 01:51
//uva 196 spreadsheet//题目不是很难  但是对于输入数据的处理比较困难//题目大意为 网格中每个要么为整数 要么为公式//最后要把公式都替成数字#include <iostream>#include <string.h>#include <cstdio>using namespace std;typedef struct{    bool type;//标记是数字还是公式    int count;//记录公式中数的个数    int column[50];//当为公式时记录列的编号    int row[50];//当为公式时记录行的编号    int value;//当为值时记录值的大小 当为公式时记录公式中所出现的值的和}cell;const int maxn=1010;cell sheet[maxn][maxn];int n,m;void translate(int x,int y,char *s){    sheet[x][y].count=0;    int col_num,row_num,len;    bool flag=false;    col_num=row_num=0;    len=strlen(s);    sheet[x][y].value=0;    for(int i=1;i<=len;i++)    {        if(s[i]>='A'&&s[i]<='Z')//计算列号        {            col_num=col_num*26+(s[i]-'A');            flag=true;        }        else if(s[i]>='0'&&s[i]<='9')//计算行号 或者直接计算数字            row_num=row_num*10+(s[i]-'1');        else//遇到了“+”号说明一个输入完成        {            if(flag)//如果输入了列号则说明不是输入的数字            {                sheet[x][y].column[sheet[x][y].count]=col_num;                sheet[x][y].row[(sheet[x][y].count)++]=row_num;                row_num=col_num=0;                flag=false;            }            else //在公式中直接输入数字            {                sheet[x][y].value+=row_num;                row_num=col_num=0;                flag=false;            }        }    }}void computer(int x,int y){    int newcol,newrow;    for(int i=0;i<sheet[x][y].count;i++)    {        newcol=sheet[x][y].column[i];        newrow=sheet[x][y].row[i];        if(!sheet[newrow][newcol].type)//如果需要叠加的是公式则计算出它的值            computer(newrow,newcol);        if(sheet[newrow][newcol].type)//如果需要叠加的是数字则直接加 或者计算完公式后再加            sheet[x][y].value+=sheet[newrow][newcol].value;    }    sheet[x][y].type=true;}void readsheet(){    char s[60];    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        {            scanf("%s",s);            if(s[0]!='=')//为数字            {                sscanf(s,"%d",&sheet[i][j].value);                sheet[i][j].type=true;            }            else //为公式            {                translate(i,j,s);                sheet[i][j].type=false;            }        }   }int main(int argc,char *argv[]){    int times;    scanf("%d",&times);//输入表格的个数    while(times--)    {        scanf("%d%d",&m,&n);//输入列数和行数        readsheet();        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)                if(!sheet[i][j].type)//如果是公式则计算公式的值                    computer(i,j);        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)                if(j==m-1)printf("%d\n",sheet[i][j].value);                else printf("%d ",sheet[i][j].value);    }    return 0;}
0 0
原创粉丝点击