如何增加c ++中的字母组合超出'z'? [英] How to increment letter combinations in c++ beyond 'z'?

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

问题描述

我正在使用一个Excel电子表格,我只能使用一种类型的公式来获取大量的数据。因为在公式中,只有必要的变化涉及字母,我想知道是否有一种方法使一个程序增加他们遵循Excel列顺序(A,B,C ... Z; AA,AB,AC ... AZ; BA,BB,BC ... BZ)。



在我的例子中,我需要每次递增5个字母,我想要获得:

  #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< :<<< x<<100)-ROW(<< x<1),)))& endl;
x = x + 5;
y = y + 5;
}
return 0;
}

当然不会工作,因为它超过了'z'

解决方案

一般解决方案说明 b
$ b

解决方案1:创建base-26系统本身:



假设您有26个字母。所以首先让26号码系统。我们对每个数字使用1个字节。我们创建一个数字数组,然后当添加超过26时,我们需要调整。



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

计算溢出阈值(上面的值为1)后,我们可以将其添加到下一个数字,



最后,为了显示,我们只需为每个字节添加65('A')。

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



在这种情况下,

  number / 26 = x 

 其余r1 =(数字%26)



r1到字节。

  x / 26 = x1 

 其余r2 =(x%26)

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

  x1 / 26 = x2 

 其余r3 =(x%26)

我们将r3存储到下一个字节。我们得到一个字符串 r3 r2 r1'\0',然后为每个字节添加65'A'。


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;
}

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

解决方案

General solution descriptions

Solution 1: Create base-26 system itself:

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.

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.)

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.

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

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

In this case,

number/26 = x

and

remainder r1 = (number%26)

We store r1 to a byte.

x/26 = x1

and

remainder r2 = (x%26)

We store r2 to the next byte.

x1/26 = x2

and

remainder r3 = (x%26)

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

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

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