[英]Simple Encryption program array
構建一個簡單的程序,將字符串中字符的 ASCII 值乘以 3 進行加密,然后除以 3 進行解密。 到目前為止,我已經了解了加密部分,但是每當我輸入加密內容並嘗試解密時,它都不起作用。 我認為這與緩沖區 stream 有關,但如果有人可以提供幫助,我可能是錯的。
#include<iostream>
using namespace std;
int main()
{
string message;
int charValue;
int counter;
int encrypt;
char choice;
char quit = 'N';
while (quit == 'N')
{
cout << "Enter E to encrypt or D to Decrypt\n";
cin >> choice;
toupper(choice);
cout << "Enter text no spaces: ";
cin >> message;
int messagelen = message.length();
string stringArray[255];
if (choice == 'E')
{
for (counter = 0; counter < messagelen; counter++) //*3 to ascii val
{
stringArray[counter] = message[counter] * 3;
}
for (counter = 0; counter < messagelen; counter++)
{
cout << stringArray[counter];
}
}
else
{
for (counter = 0; counter < messagelen; counter++) // divide 3 to ascii val
{
stringArray[counter] = message[counter] / 3;
}
for (counter = 0; counter < messagelen; counter++)
{
cout << stringArray[counter];
}
}
cout << "\nY to go again N to quit";
cin >> quit;
}
return 0;
}
它將真正幫助您將其分解為更小的問題。 讓我們將std::string
“加密”為std::vector<int>
:
std::vector<int> encrypt_msg(std::string s) {
std::vector<int> v;
for (auto ch = s.begin(); ch != s.end(); ch++) {
v.push_back(static_cast<int>(*ch) * 3);
}
return v;
}
然后讓我們“解密”一條消息,反向執行轉換。
std::string decrypt_msg(std::vector<int> v) {
std::string s;
for (auto i : v) {
s += static_cast<char>(i / 3);
}
return s;
}
現在您可以測試並查看您的單個功能是否可以完成一件事,並且將整個程序組合在一起應該會容易得多。
這是一個可行的實現,盡管我同意您應該使用encrypt
和decrypt
功能的其他答案。 我在您的代碼中發現了很多其他錯誤。 您應該使用-Wall -Werror
啟用所有警告並修復它們:
#include <iostream>
#include <vector>
#include <sstream>
int main()
{
// removed some unused variables
std::string message;
size_t counter;
char choice;
char quit;
// use vector of int instead of array of strings
std::vector<int> encryptArray;
// change to do while loop. Not particularly necessary, but I think
// it makes more sense in this case. Your condition is broken. If the
// user enters 'Y' at the end to go again, then the quit == 'N'
// condition is false and the program terminates.
do
{
std::cout << "Enter E to encrypt or D to Decrypt\n";
std::cin >> choice;
// toupper returns a value, you need to assign it to choice.
// Not capturing the return value makes this a noop.
choice = toupper(choice);
if (choice == 'E')
{
std::cout << "Enter text no spaces: ";
std::cin >> message;
size_t messagelen = message.length();
// initialize vector to input message length size
encryptArray = std::vector<int>(messagelen);
for (counter = 0; counter < messagelen; counter++) //*3 to ascii val
{
encryptArray[counter] = message[counter] * 3;
}
// Note, this 2nd loop is more work than you need, you could
// simply put the std::cout line in the loop above below the
// assignment
for (counter = 0; counter < messagelen; counter++)
{
// added the separator just for clarity. You could also print
// hex bytes
std::cout << encryptArray[counter] << "|";
}
}
else
{
// all the data we care about is in the vector now
for (counter = 0; counter < encryptArray.size(); counter++) // divide 3 to ascii val
{
// you don't want to /3 what's in the message here, you want
// to /3 the encrypted values, which are in the vector
encryptArray[counter] = encryptArray[counter] / 3;
}
// plenty of ways to convert the vector to a string, this is not
// a "modern" way.
// Note, you could skip this loop entirely, and in the one
// above, simply do ss << (char)(encryptArray[i] / 3);
// Not necessary to write the data back to encryptArray first.
std::stringstream ss;
for (size_t i=0; i<encryptArray.size(); ++i)
{
ss << (char)encryptArray[i];
}
std::cout << "decrypted string: " << ss.str() << std::endl;
}
std::cout << "\nY to go again N to quit: ";
std::cin >> quit;
} while(quit != 'N'); // loop until quit == N
return 0;
}
最后,我刪除了using namespace std;
, 這就是為什么
在 godbolt 上使用stdin
時事情變得很奇怪,但這是一個有效的演示,至少在最初是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.