R
Rendall
<div class="sp-wrap"><div class="sp-head-wrap"><div class="sp-head folded clickable">код</div></div><div class="sp-body"><div class="sp-content">
программа шифрует текст введенный пользователем по заданному им ключу, надо переделать Encode и Decode потому что выдает чушь, сам найти ошибку не могу, помогите пожалуйста. П.С если что делал в 12 студии
C++:
#include "stdafx.h"
#include <math.h>
#include <iostream>
#include <clocale>
#include <stdio.h>
//Кодировка строки
//char* s - исходная строка
//int* key - ключ
//int sz_key - количество элементов в ключе
using namespace std;
char* Encode(char* s, int* key, int sz_key);
//Декодировка строки
//char* s - закодированная строка
//int* key - ключ
//int sz_key - количество элементов в ключе
char* Decode(char * s, int * key, int sz_key);
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(0, "Rus");
//ключ
cout<<"Введите ключ без повторений букв \n";
char *s_key;
s_key= new char[11];
fflush(stdin);
fgets(s_key,11, stdin);
int t = 0;
while (s_key[t] != '\0') t++;
int *key = new int[t];
for(int p=0;p<t-1;p++)
{
key[p]=(int)s_key[p]-96;
cout<<key[p]<<"\n";
}
cout<<"\n";
int i=0;
char *s= new char[100];
cout<<"введите сообщение для зашифровки"<<" \n";
fflush(stdin);
fgets(s,100, stdin);
cout<<"\n";
int sz_key = sizeof(key)/sizeof(int);
char *s_code = Encode(s, key, sz_key);
char *s_decode = Decode(s_code, key, sz_key);
printf("Исходное сообщение: %s\n", s);
printf("Зашифрованное сообщение: %s\n", s_code);
printf("Сообщение после дешифрования: %s\n", s_decode);
system("pause");
return 0;
}
char* Encode(char* s, int* key, int sz_key)
{
//количество элементов в исходной строке s
int sz_s = 0;
while (s[sz_s] != '\0')
sz_s++;
//результируюшая строка
char *str = new char[sz_s];
//кодировка строки s
//количество блоков
int rounds = ceil((double) sz_s / sz_key);
int j = 0;
for (int i = 0; i < rounds; i++)
for (int k = 0; k < sz_key; k++)
if (i*sz_key + key[k] < sz_s)
{
str[j] = s[i*sz_key + key[k]];
j++;
}
str[sz_s] = '\0';
return str;
}
char* Decode(char * s, int * key, int sz_key)
{
//количество элементов в закодированной строке s
int sz_s = 0;
while (s[sz_s] != '\0')
sz_s++;
//результируюшая строка
char *str = new char[sz_s];
//декодировка строки s
//количество блоков
int rounds = ceil((double) sz_s / sz_key);
int j = 0;
for (int i = 0; i < rounds; i++)
for (int k = 0; k < sz_key; k++)
if (i*sz_key + key[k] < sz_s)
{
str[i*sz_key + key[k]] = s[j];
j++;
}
str[sz_s] = '\0';
return str;
return 0;
}
программа шифрует текст введенный пользователем по заданному им ключу, надо переделать Encode и Decode потому что выдает чушь, сам найти ошибку не могу, помогите пожалуйста. П.С если что делал в 12 студии