给定两个相反的坐标时如何打印正方形内的所有单元格

How to print all the cells inside a square when given the two opposite coordinates

我有一个 10x10 矩阵。当给定正方形的相反坐标时,我必须形成一个正方形并找到其中的所有单元格。 例子 给定 1B,3D 作为坐标,形成一个正方形。 我需要打印 1B,1C, 1D, 2B, 2C, 2D, 3B, 3C, 3D

因为值是字符串,我该如何开始使用这个

void printSquare(string a, string b){

    int start = stoi(a.substr (0, a.length()-1));   
    int end =  stoi(b.substr (0, b.length()-1));   
    char s = a[a.length()-1];
    for(int i=0;i<=end-start;i++){
        for(int j=0;j<=end-start;j++){
            cout<<1+i << char(s+j);
        }
        cout<<endl;
    }

}

使用 atoisubstr 方法将单元格转换为整数。假设只有最后一个字符可以是字符串。否则,我们需要遍历每个字符并检查字符是否在 0-9 之间。

一旦我们有了正方形的角,就可以遍历长度并简单地打印出来。

https://ideone.com/6OFHM1

大多数系统都使用 ASCII。 ASCII有属性即'A'+1 == 'B'、'B'+1 == 'C'等。 C++ 还保证 '0'+1 == '1'、'1'+1 == '2' 等等。因此你可以简单地

void f(const std::string &lhs, const std::string &rhs) {
    bool first = true;
    for(char i = lhs[0]; i <= rhs[0]; ++i) {
        for(char j = lhs[1] ; j <= rhs[1]; ++j) {
            std::cout << (first ? "" : ", ") << i << j;
            first = false;
        }
    }
}