c++ - 如何在'z'之外增加C++中的字母组合? [英] How to increment letter combinations in c++ beyond 'z'?

查看:38
本文介绍了c++ - 如何在'z'之外增加C++中的字母组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Excel 电子表格,并且我必须只使用一种类型的公式来处理大量数据.由于在公式中唯一必要的更改涉及字母,我想知道是否有一种方法可以制作一个程序,按照 Excel 列顺序(A、B、C...Z;AA、AB、AC...AZ;BA、BB、BC...BZ).

I'm working on an Excel spreadsheet, and I have to use only one type of formula for a huge amount of data. Since in the formula the only necessary changes concern letters, I was wondering if there is a way to make a program that increments them following the Excel columns order (A, B, C...Z; AA, AB, AC...AZ; BA, BB, BC...BZ).

在我的情况下,我需要每次将字母增加五个,所以这是我想要获得的代码类型:

In my case, I would need to increment letters each time by five, so here is the kind of code I'm trying to obtain:

#include <iostream>

using namespace std;

int main() {

 char x = 'B';
 char y = 'E';
 for (int z = 1; z < 2255; z++) {
   cout << "=SUMPRODUCT(SUBTOTAL(4,OFFSET(" << x << "1:" << y << "1,ROW(" << x << "1:" << x << "100)-ROW(" << x << "1),)))" << endl;
   x = x + 5;
   y= y + 5;
  }
  return 0;
}

当然它不会工作,因为它超过了z",但是,有没有办法做到这一点?

Of course it won't work because it goes over 'z', but still, is there a way to do this?

推荐答案

一般解决方案说明

解决方案 1:自行创建 base-26 系统:

Solution 1: Create base-26 system itself:

假设您有 26 个字母.所以首先让我们制作26个数字系统.我们为每个数字使用 1 个字节.我们创建了一个数字数组,然后有时候加起来超过26就需要调整一下.

Assume that you have 26 letters. So first lets make 26 number system. We use 1 byte for each digit. We create an array of digits, and then we need to adjust sometimes when adding exceeds 26.

假设你当前的位数是 25.我们加 7,然后我们需要处理溢出,假设最大为 256(1 个字节),我们的位数限制是 26.因此调整将是 256-26=230.(我们在short(16位)上进行计算,所以我们在26+7=33 -> 33+230=263处溢出.因此,高字节为1,低字节将是 7.)

Let's assume your current digit is 25. We add 7 to it, and then we need to handle the overflow, assuming 256 (1 byte) as maximum, and our digit limit is 26. Hence the adjustment will be 256-26=230. (We do this calculation on short (16 bits), so we get overflow at 26+7=33 -> 33+230=263. Therefore, the higher byte will be 1, the lower byte will be 7.)

已经计算出溢出阈值(高于 1),然后我们可以将它添加到下一个数字,如果发生溢出,也可以这样做.

Having calculated the overflow threshold (above it was 1), we can then add it to next coming digit and do the same if overflow occur.

最后,为了显示,我们只需将 65 ('A') 添加到两个字节中的每一个.我们的最后一个字节将以 '' null 结尾,因此我们可以将其转换为字符串.

Finally, for display we just add 65 ('A') to each of the two bytes. Our last byte will be '' null terminating so we could turn it into a string.

解决方案 2 执行所有计算,然后将其转换为 26 基数:

Solution 2 Perform all calculations and then convert it to a 26-base number:

在这种情况下,

number/26 = x

remainder r1 = (number%26)

我们将 r1 存储到一个字节中.

We store r1 to a byte.

x/26 = x1

remainder r2 = (x%26)

我们将 r2 存储到下一个字节.

We store r2 to the next byte.

x1/26 = x2

remainder r3 = (x%26)

我们将 r3 存储到下一个字节.我们得到一个字符串 r3 r2 r1 '',然后在每个字节上加上 65 'A'.

We store r3 to the next byte. We get a string r3 r2 r1 '' and then add 65 'A' to each byte.

这篇关于c++ - 如何在'z'之外增加C++中的字母组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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