C程序根据给定的列号打印Excel列标题
问题
用于打印与给定列号(整数值)相对应的Excel列标题的程序。用户必须根据给定的数字输入整数才能打印excel列号。
解决方案
下面解释了打印与C编程语言中给定列号相对应的Excel列标题的解决方案-
示例1
让我们看一个例子。
1 -> A 2 -> B ... 26 -> Z 27 -> AA 28 -> AB ...
示例2
的输入是如下-
number = 3 number = 27 number = 151
该输出是如下-
Excel column title: C Excel column title: AA Excel column title: EU
示例
以下是打印与给定列号对应的Excel列标题的C程序-
#include输出结果static char *convert_To_Excel_Title(int column_no){ if (column_no <= 0) { return ""; } char *result = malloc(1024); int len = 0; do { result[len++] = ((column_no - 1) % 26) + 'A'; column_no = (column_no - 1) / 26; } while (column_no > 0); result[len] = '\0'; int i, j; for (i = 0, j = len - 1; i < j; i++, j--) { char c = result[i]; result[i] = result[j]; result[j] = c; } return result; } int main(void){ int n; printf("输入整数1:"); scanf("%d",&n); printf("Column Number n = %d", n); printf("\nExcel column title: %s\n\n ",convert_To_Excel_Title(n)); printf("输入整数2:"); scanf("%d",&n); printf("\nColumn Number n = %d", n); printf("\nExcel column title: %s\n\n ",convert_To_Excel_Title(n)); printf("输入整数3:"); scanf("%d",&n); printf("\n\nColumn Number n = %d", n); printf("\nExcel column title: %s ",convert_To_Excel_Title(n)); return 0; }
执行上述程序时,会产生以下结果-
输入整数1:23 Column Number n = 23 Excel column title: W 输入整数2:12 Column Number n = 12 Excel column title: L 输入整数3:69 Column Number n = 69 Excel column title: BQ