Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从ASCII字符中减去一个数字_C++_Ascii - Fatal编程技术网

C++ 从ASCII字符中减去一个数字

C++ 从ASCII字符中减去一个数字,c++,ascii,C++,Ascii,所以,我试着去做那个线程中的事情,除了我想返回ascii转换(仅字母)aka,如果我给了数字1 b+1 = a B+1 = A (capital becomes capital) c+2 = a z+1 = y a+1 = z int lower_add=((字母-'a'-input_int)%26)+'a'; 如果((下加-'a'-输入值)

所以,我试着去做那个线程中的事情,除了我想返回ascii转换(仅字母)aka,如果我给了数字1

b+1 = a

B+1 = A (capital becomes capital)

c+2 = a

z+1 = y

a+1 = z
int lower_add=((字母-'a'-input_int)%26)+'a';
如果((下加-'a'-输入值)<0)
下加=下加+26;

这几乎是正确的,除了有些情况下,b+1将转到其他非字母ascii字符。

lower\u add
已经减去了
input\u int
,不要再这样做了。将您的
if
更改为:

if (lower_add < 'a')
    lower_add += 26;
if(下加<'a')
下加+=26;

这仍然不能正确处理大写字母,您可能需要一个范围测试来决定是减去
'a'
还是减去
'a'

我会这样做:

#include <iostream>
#include <ctype.h>

class letter { 
    char current;
public:
    letter(char x) : current(x) {}
    letter &operator+=(int v) { 
        if (islower(current)) {
            int pos = current - 'a' - v;
            if (pos < 0) pos += 26;
            current = pos + 'a';
        }
        else if (isupper(current)) {
            int pos = current - 'A' - v;
            if (pos < 0) pos += 26; 
            current = pos + 'A';
        }
        return *this;
    }
    friend std::ostream &operator<<(std::ostream &os, letter const &l) { 
        return os << l.current;
    }   
};

int main() { 
    letter a('c');
    a += 3;
    std::cout << a;
}
#包括
#包括
类字母{
炭流;
公众:
字母(字符x):当前(x){}
字母和运算符+=(int v){
如果(孤岛(当前)){
int pos=电流-‘a’-v;
如果(pos<0)pos+=26;
电流=位置+‘a’;
}
否则如果(isupper(当前)){
int pos=电流-‘A’-v;
如果(pos<0)pos+=26;
电流=位置+‘A’;
}
归还*这个;
}

friend std::ostream&Operator可能与您之前所问的完全相同。这个问题怎么还没有回答?在您的示例中,
+
的真正意思是
-
。这就是您的意思吗?
#include <iostream>
#include <ctype.h>

class letter { 
    char current;
public:
    letter(char x) : current(x) {}
    letter &operator+=(int v) { 
        if (islower(current)) {
            int pos = current - 'a' - v;
            if (pos < 0) pos += 26;
            current = pos + 'a';
        }
        else if (isupper(current)) {
            int pos = current - 'A' - v;
            if (pos < 0) pos += 26; 
            current = pos + 'A';
        }
        return *this;
    }
    friend std::ostream &operator<<(std::ostream &os, letter const &l) { 
        return os << l.current;
    }   
};

int main() { 
    letter a('c');
    a += 3;
    std::cout << a;
}