Задачка по программированию на c++ - Вопросы по С+

Вопрос Задачка по программированию на c++

Регистрация
15 Дек 2013
Сообщения
89
Репутация
0
Спасибо
0
Монет
0
279819996_4a6506f3ffaf34cbc7bb5b0289e78bfa_800.png

 
Регистрация
1 Ноя 2013
Сообщения
83
Репутация
0
Спасибо
0
Монет
0
#include

int main()
{
char ch, base; int k = 3;
while (std::cin.get(ch),ch!='\n')
{
((ch >= 'A' && ch = 'a' && ch
 
Регистрация
22 Ноя 2012
Сообщения
78
Репутация
0
Спасибо
0
Монет
0
Легко и просто: #include
#include
#include

using namespace std;

int main() {
const char k = 3;
string s;
getline(cin, s);
for (size_t i = 0; i < s.size(); i++) {
if (isalpha(s)) {
char c = s + k;
if (!isalpha(c)) c -= 26;
s = c;
}
}
cout
 
Регистрация
25 Дек 2012
Сообщения
78
Репутация
0
Спасибо
0
Монет
0
#include
#include
#include

using namespace std;

struct CaesarCipher {
static string encode(const string& line, int k) {
k %= length;
if (k 0 && isalpha(ch)) {
if (ch > Z) {
ch += k;
if (ch > z) {
ch -= length;
}
} else {
ch += k;
if (ch > Z) {
ch -= length;
}
}
}
}
return text;
}
private:
static const int length = 26;
static const int Z = 'Z';
static const int z = 'z';
};

int main() {
int k = 3;
string line;
getline(cin, line);
auto cipher = CaesarCipher::encode(line, k);
cout
 
Регистрация
15 Сен 2013
Сообщения
90
Репутация
0
Спасибо
0
Монет
0
Контроля за количеством символов и их кодами нет. Если надо, скажи, добавлю, если буду на связи.

#include
#include

const std::string az = "abcdefghijklmnopqrstuvwxyz";
const std::string AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int is_az(const char c)
{
for(int x = 0; x < 26; x++)
if(az[x] == c)
return x+1;
return 0;
}

int is_AZ(const char c)
{
for(int x = 0; x < 26; x++)
if(AZ[x] == c)
return x+1;
return 0;
}

std::string str_forvard(std::string str, int k)
{
std::string rc = "";
int pos;

for(int x = 0; x < str.length(); x++)
{
pos = is_az(str.at(x));
if(pos)
{
pos += (k - 1);
if(pos < 0)
rc += az.at(pos+26);
else if(pos >= 26)
rc += az.at(pos - 26);
else
rc += az.at(pos);
}
else
{
pos = is_AZ(str.at(x));
if(pos)
{
pos += (k - 1);
if(pos < 0)
rc += AZ.at(pos+26);
else if(pos >= 26)
rc += AZ.at(pos - 26);
else
rc += AZ.at(pos);
}
else
rc += str.at(x);
}
}

return rc;
}

std::string str_back(std::string str, int k)
{ // эту функцию можно удалить. Она для наглядности, если надо мотать алфавит назад.
std::string rc = "";
int pos;

for(int x = 0; x < str.length(); x++)
{
pos = is_az(str.at(x));
if(pos)
{
pos += (-k - 1);
if(pos < 0)
rc += az.at(pos+26);
else if(pos >= 26)
rc += az.at(pos - 26);
else
rc += az.at(pos);
}
else
{
pos = is_AZ(str.at(x));
if(pos)
{
pos += (k - 1);
if(pos < 0)
rc += AZ.at(pos+26);
else if(pos >= 26)
rc += AZ.at(pos - 26);
else
rc += AZ.at(pos);
}
else
rc += str.at(x);
}
}

return rc;
}

int main()
{
std::string str1,str2;
int k = 3;

getline(std::cin,str1);
str2 = str_forvard(str1, k);

std::cout
 
Сверху Снизу