Cześć, problem polega na tym, że gdy program natrafia na polską literę w pliku tekstowym, to źle ją interpretuje — kwestia kodowania znaków.
Program ma odszyfrowywać zaszyfrowaną wiadomość, w zależności od pozycji litery (parzysta, nieparzysta) ma być inny klucz do odszyfrowania.
Gdy natrafia na polską literę to liczby ją jako dwie pozycje. I błędnie pobiera wartości do funkcji.
Zależy mi na tym, żeby odszyfrowany tekst trafił do pliku txt, nie chcę go wyświetlać w konsoli.
Już troszkę się z tym bawię, pomocy i z góry dziękuje!
Kod wygląda tak, coś próbowałem z Pythona przemielić na C++ (działający Python dalej poniżej):
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
char Decrypt(char letter, int kay);
int main(int argc, char const *argv[])
{
ifstream file("text.txt");
ofstream file2("text2.txt");
int kay, kay2;
cout << "Podaj klucz dla miejsc parzystych: ";
cin >> kay;
cout << "Podaj klucz dla miejsc nieparzystych: ";
cin >> kay2;
string text, text2;
while (!file.eof())
{
getline(file, text);
text2 = "";
for (int i = 0; i < text.length(); i++)
{
if (i % 2 == 0)
{
text2 += Decrypt(text[i], kay);
}
else
{
text2 += Decrypt(text[i], kay2);
}
}
file2 << text2 << endl;
text2 = "";
}
file2.close();
file.close();
return 0;
}
char Decrypt(char letter, int kay)
{
string lowercaseAlphabetPL = "aąbcćdeęfghijklłmnńoópqrsśtuvwxyzźż";
string uppercaseAlphabetPL = "AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻ";
int index = lowercaseAlphabetPL.find(letter);
if (index >= 0 && index <= 34)
{
if ((index - kay) % 35 < 0)
{
return lowercaseAlphabetPL[35 + (index - kay) % 35];
}
else
{
return lowercaseAlphabetPL[(index - kay) % 35];
}
}
else
{
index = uppercaseAlphabetPL.find(letter);
if (index >= 0 && index <= 34)
{
if ((index - kay) % 35 < 0)
{
return uppercaseAlphabetPL[35 + (index - kay) % 35];
}
else
{
return uppercaseAlphabetPL[(index - kay) % 35];
}
}
else
{
return letter;
}
}
}
Próbowałem też coś takiego lepszy wynik, ale ignoruje polskie znaki:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
const string lowercaseAlphabetPL[35] = {"a", "ą", "b", "c", "ć", "d", "e", "ę", "f", "g", "h", "i", "j", "k", "l", "ł", "m", "n", "ń", "o", "ó", "p", "q", "r", "s", "ś", "t", "u", "v", "w", "x", "y", "z", "ź", "ż"};
const string uppercaseAlphabetPL[35] = {"A", "Ą", "B", "C", "Ć", "D", "E", "Ę", "F", "G", "H", "I", "J", "K", "L", "Ł", "M", "N", "Ń", "O", "Ó", "P", "Q", "R", "S", "Ś", "T", "U", "V", "W", "X", "Y", "Z", "Ź", "Ż"};
string Decrypt(char letter, int kay);
int main(int argc, char const *argv[])
{
ifstream file("text.txt");
ofstream file2("text2.txt");
int kay, kay2;
cout << "Podaj klucz dla miejsc parzystych: ";
cin >> kay;
cout << "Podaj klucz dla miejsc nieparzystych: ";
cin >> kay2;
string text, text2;
while (!file.eof())
{
getline(file, text);
for (int i = 0; i < text.length(); i++)
{
if (i % 2 == 0)
{
cout << text[i] << " "; // rozbija polskie litery na 2 znaki im odpowiadające w innym kodowaniu
text2 += Decrypt(text[i], kay);
}
else
{
cout << text[i] << " ";
text2 += Decrypt(text[i], kay2);
}
}
file2 << text2 << endl;
text2 = "";
}
file2.close();
file.close();
return 0;
}
string Decrypt(char letter, int kay)
{
string decryptedLetter;
decryptedLetter += letter;
for (int i = 0; i < 35; i++)
{
if (decryptedLetter == lowercaseAlphabetPL[i])
{
if ((i - kay) % 35 < 0)
return lowercaseAlphabetPL[(i - kay) % 35 + 35];
else
return lowercaseAlphabetPL[(i - kay) % 35];
}
else if (decryptedLetter == uppercaseAlphabetPL[i])
{
if ((i - kay) % 35 < 0)
return uppercaseAlphabetPL[(i - kay) % 35 + 35];
else
return uppercaseAlphabetPL[(i - kay) % 35];
}
}
return decryptedLetter;
}
W Pythonie działa, ale muszę zrobić w C++ :(
def Decrypt(letter, kay):
lowercaseAlphabetPL = "aąbcćdeęfghijklłmnńoópqrsśtuvwxyzźż"
uppercaseAlphabetPL = "AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻ"
index = lowercaseAlphabetPL.find(letter)
if index >= 0 and index <= 34:
if ((index - kay) % 35 < 0):
return lowercaseAlphabetPL[35 + (index - kay) % 35]
else:
return lowercaseAlphabetPL[(index - kay) % 35]
else:
index = uppercaseAlphabetPL.find(letter)
if index >= 0 and index <= 34:
if ((index - kay) % 35 < 0):
return uppercaseAlphabetPL[35 + (index - kay) % 35]
else:
return uppercaseAlphabetPL[(index - kay) % 35]
else:
return letter
def main():
file = open("text.txt", "r", encoding="utf-8")
file2 = open("text2.txt", "w", encoding="utf-8")
kay = int(input("Podaj klucz dla miejsc parzystych: "))
kay2 = int(input("Podaj klucz dla miejsc nieparzystych: "))
for line in file:
text = line
text2 = ""
for i in range(len(text)):
if i % 2 == 0:
text2 += Decrypt(text[i], kay)
else:
text2 += Decrypt(text[i], kay2)
print("Po rozszyfrowaniu: " + text2.strip())
file2.write(text2)
text2 = ""
file.close()
file2.close()
main()
Wszelkie sugestie i pomysły mile widziane :)