Little B has recently had a strong interest in spreadsheets, and she feels that spreadsheets are magical and more powerful than she thinks. She was looking at the cell's coordinate number, and she found that the table cells were usually numbered in columns, the 1th column numbered A, the 2nd columns were B, and so on, and the 26th column was Z. This is followed by a two-character number, the 27th column is numbered AA, the 28th column is AB, and the 52nd columns are numbered AZ. Then there are three-bit, four-bit, five-bit ... The letters are numbered, and the rules are similar.
The row for the table cell is numbered starting at 1, and the table cell name is a combination of its column number and row number, such as cell BB22, which represents the cell that is the 22nd row in the 54 column.
Little B is interested in that the numbering system can sometimes take the rxcy rule, where x and y are numeric values, indicating that the cell is in row x with the nth column. The cell in the example above uses this encoding system with the name r22c54.
Small b want to quickly implement the conversion between the two representations, ask you to help the design program convert the coordinates represented in one way to another.
The first behavior of the input is a positive integer t, which indicates that there is a T group of test data (1<=t<=10^5). Subsequent T-lines, each behavior of a set of test data, represent cell coordinates in one form. Ensure that all coordinates are correct and that all row and column coordinate values do not exceed 10^6.
For each set of test data, a single line is output, another representation of cell coordinates.
2
R23c55
BC23
BC23
R23c55
Analysis: Because all row and column coordinate values are not more than 10^6 XXXXX999999 up to 12 characters (5+6+1) can be saved
Code implementation:
TEST.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include < Assert.h>char * my_itoa (INT&NBSP;VALUE,CHAR&NBSP;*STR)//Analog implementation itoa (integer converted to string) { int i=0; while (value!=0) { str [i++]=value%10+ ' 0 '; value/=10; } int start=0; int end=i-1; while (start<end) { char tmp=str[start]; str[start]=str[end]; str[end]=tmp; start++; end--; } return str;} Int my_atoi (CONST&NBSP;CHAR&NBSP;*STR)//Analog implementation atoi (string converted to digital) { assert (str); int ret=0; while (*str!= ') { ret*=10; ret+=*str-' 0 '; str++; } return ret;} Int my_pow (int x,int y)//return x y-square { int ret=1; while (--y>=0) { ret*=x; } return ret;} Char *coord (Char *a,char *b,int len) { assert (a); int flag=0; if (a[0]== ' R ' &&a[1]>= ' 0 ' &&a[1]<= ' 9 ') {// MAYBE&NBSP;FRLC (r23c55 form) maybe fclr (BC23 form) int i=2; &Nbsp; while (a[i]!=0) { if (a[i]== ' C ' ) { flag=1; break; } i++; } } if (flag==1) {//must be frlc int i=1; int k=0; char row[5]={0}; while (a[i]!= ' C ') { row[k++]=a[i++]; } &Nbsp; while (a[i]<= ' 0 ' | | a[i]>= ' 9 ') { i++; } k=0; char tmp[5]={0}; while (a[i]!=0) { tmp[k++]=a[i++]; } int col=atoi (TMP); int j=0; int ret[5]={0}; while (col>0) { ret[j++]=col%26; col/=26; } int start=0; int end=0; while (ret[end]!=0) { end++; } end-=1; while ( Start<end) { char val=ret[start] ; ret[start]=ret[end]; ret[end]=val; start++; end--; } int t =0; memset (b, ' n ', sizeof (char) *12); while (ret[t]!=0) { b[t]= ' A ' + ret[t]-1; t++; } strcat (B,row); } else{//must is FcLr Int i=0; while (a[i]>= ' a ' &&a[i]<= ' Z ') { i++; } int j=i; int col=0; while (--j>=0) { col+=my_pow (26,i-j-1) * (a[j]-' a ' + 1); } char str[12]={0}; my_itoa (COL,STR); memset (b, ' n ', sizeof (char) * b[0]= ' R '; Int k=1; while (0!= (b[k++]=a[i++)) ; k-=1; b[k++]= ' C '; strcat (b, STR); } return b;} Int main () { char a[12]={0}; char b[12]={0}; while (scanf ("%s", a)!=eof) {&NBSP;&NBSP;&NBSp; coord (a,b,12); int i=0; while (b[i]!=0) { printf ("%c", B[i]); i++; } printf ("\ n"); } return 0;}
Makefile:
bin=testsrc=test.ccc=g++$ (BIN): $ (src) cc-o [email protected] $^-g.phony:cleanclean:rm-f $ (BIN)
Operation Result:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/91/wKiom1ci7LKAS68DAAAYDq39Lmk697.png "title=" 1.PNG " alt= "Wkiom1ci7lkas68daaaydq39lmk697.png"/>
This article is from the "Zero Egg" blog, please be sure to keep this source http://lingdandan.blog.51cto.com/10697032/1768964
Programmatic implementation of the excle of row and column naming and another representation of the mutual conversion