转换R1C1格式A1幅面 [英] Converting R1C1 format to A1 format

查看:176
本文介绍了转换R1C1格式A1幅面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Excel US preadsheet我们每个小区由其无论是在R1C1格式或A1幅面的行和列表示。

R1C1格式表示的行数和列数。例如,R23C55意味着第23行和列55。

在A1幅面,每列被赋予一个code,根据字母permutatons。考虑第26列,每从A到Z的下一列命名(即第27列)。将AA和AB,AC和等。例如,BC23将意味着列BC是第55列和行23这是ofcourse 23行。

我需要的形式R1C1的输入转换为形式A1的输出。例如,R23C55是BC23,其中BC是55相应的输出。

我已经做了code到给​​定的列数转换成它的A1格式。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
    INT列,温度,I = 0;
    炭COL [5];    scanf函数(%d个,&安培;列);
    TEMP =列;    而(温度!= 0)
    {
        如果(TEMP%26 == 0)
            {
                COL [I] ='Z';
                我++;
        }
        其他
        {
            COL [I] =(TEMP%26)-1 +'A';
            我++;
        }
        TEMP =(TEMP-1)/ 26;
    }
    COL [I] ='\\ 0';
    strrev(COL);
    看跌期权(COL);    返回0;
}

我可以输入数字55转换为输出BC。但我不能用code继续开行R23C55转换为BC23。如何参加输入R23C55,提取号码23和55,并给出了形式BC23的输出?


解决方案

 #包括LT&;&cstdio GT;
无效克(INT T){如果(T){克((T-1)/ 26);的putchar(65+(T-1)%26);}}
诠释主(){
  INT N,X,Y;
  个char [64];    与fgets(S,64,标准输入);
    如果(sscanf的(S,R%的直流%D,&安培; X,&安培; Y)== 2){
      克(Y);
      的printf(%d个\\ N,X);  }
  返回0;
}

此程序precisely你想要做什么。其中的逻辑是从code清楚。


  

勾选此链接,了解的sscanf() sscanf的


  
  

下面为您的使用scanf函数(R%的直流%D,..)也可以工作。但是我用的sscanf()为您提供的路要走,如果再需要进行一些处理,这将是有益的。


假设输入格式为:

  4
R23C45
R24C55
R2C4
R12C45

,那么你可以把他们这种方式 -

 与fgets(S,64,标准输入);
sscanf的(S,%D,&安培; N);
对于(i = 0; I< N;我++)
{与fgets(S,64,标准输入);
   ...
}

In an excel spreadsheet we have each cell denoted by its row and column either in R1C1 format or the A1 format.

R1C1 format indicates the row number and the column number. For example, R23C55 means row 23 and column 55.

In the A1 format, each column is given a code, based on alphabet permutatons. Consider the first 26 columns, each are named from A to Z. The next column (i.e 27th column) would be AA, and AB,AC and so on. For example, BC23 would mean column BC which is the 55th column and row 23 which is ofcourse the 23rd row.

I need to convert an input of the form R1C1 to an output of the form A1. For example, R23C55 would be BC23 where BC is the corresponding output for 55.

I have made a code to convert a given column number to its A1 format.

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

int main()
{
    int column,temp,i=0;
    char col[5];

    scanf("%d",&column);
    temp=column;

    while(temp!=0)
    {
        if(temp%26==0)
            {
                col[i]='Z';
                i++;
        }
        else
        {
            col[i]=(temp%26)-1+'A';
            i++;
        }
        temp=(temp-1)/26;
    }
    col[i]='\0';
    strrev(col);
    puts(col);

    return 0;
}

I can convert an input number 55 to its output BC. But I cannot proceed with a code for converting R23C55 to BC23. How do I take the input R23C55, extract numbers 23 and 55, and give output of the form BC23?

解决方案

#include<cstdio>
void g(int t){if(t){g((t-1)/26);putchar(65+(t-1)%26);}}
int main(){
  int n,x,y;
  char s[64];

    fgets(s,64,stdin);
    if(sscanf(s,"R%dC%d",&x,&y)==2){
      g(y);
      printf("%d\n",x);

  }
  return 0;
}

This program precisely does what you want. The logic is clear from the code.

Check this link to learn about sscanf() sscanf

Here for your use scanf("R%dC%d",..) will also work. But I have used sscanf() to provide you with a way that will be helpful if further some processing is required.

Say the input format is

4
R23C45
R24C55
R2C4
R12C45

then you could take them this way-

fgets(s,64,stdin);
sscanf(s,"%d",&n);
for(i=0;i<n;i++)
{   fgets(s,64,stdin);
   ...
}

这篇关于转换R1C1格式A1幅面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆