C++ C++;Vigenere和Caesar密码在同一编程中的切换情况

C++ C++;Vigenere和Caesar密码在同一编程中的切换情况,c++,switch-statement,case,C++,Switch Statement,Case,在第一个代码中,我使用了一个凯撒密码,它工作得很好。然而,我不能补充维格纳。有一个类和一些指针,但它们在switch-case语句中不起作用,因此我需要帮助 加密部分为交换机外壳:3 解密部分为开关盒:4 #包括 #包括 #包括 使用名称空间std; char消息[200],ch; int i,键; 字符串k,输出,messageV; int菜单(int应答); int main() { int答案; bool keeploping=true; 同时(继续) { 回答; 如果(答案=1)

在第一个代码中,我使用了一个凯撒密码,它工作得很好。然而,我不能补充维格纳。有一个类和一些指针,但它们在
switch
-
case
语句中不起作用,因此我需要帮助

  • 加密部分为交换机外壳:3
  • 解密部分为开关盒:4

#包括
#包括
#包括
使用名称空间std;
char消息[200],ch;
int i,键;
字符串k,输出,messageV;
int菜单(int应答);
int main()
{
int答案;
bool keeploping=true;
同时(继续)
{
回答;
如果(答案=1)
{
同时(继续)
{
回答;
如果(答案=1)
{
答案=1;
keeploping=false;
}
否则如果(答案=2)
{
答案=2;
keeploping=false;
}
其他的
{
keeploping=true;
}
}
}
否则如果(答案=2)
{
同时(继续)
{
回答;
如果(答案=1)
{
答案=3;
keeploping=false;
}
否则如果(答案=2)
{
答案=4;
keeploping=false;
}
其他的
{
keeploping=true;
}
}
}
其他的
{
keeploping=true;
}
}
菜单(答案);
}
int菜单(int应答)
{
开关(应答)
{
案例1://凯撒密码加密
cout键;
对于(i=0;消息[i]!='\0';++i){
ch=消息[i];
如果(ch>='a'&&ch'z'){
ch=ch-'z'+'a'-1;
}
消息[i]=ch;
}
否则如果(ch>='A'&&ch'Z'){
ch=ch-'Z'+'A'-1;
}
消息[i]=ch;
}
}

cout我的建议是,你应该在两个程序中找到共同的模式。例如,您必须使用加密和解密方法。这可以抽象为两个不同的类,可能有一个共同的基类定义接口(允许多态性之类的东西):

由此,我们可以创建一组使用任何密码的函数(通过指向基
cipher
类的指针)。让我们从选择要使用的密码开始:

Cipher* select_cipher()
{
    std::cout << "Please select cipher to use:\n";
    std::cout << "1) Caesar\n";
    std::cout << "2) Vigenere\n";

    int selection;
    std::cin >> selection;

    if (selection == 1)
    {
        return new Caesar;
    }
    else if (selection == 2)
    {
        return new Vigenere;
    }
    else
    {
        return nullptr;
    }
}
Cipher*选择_Cipher()
{
std::cout解密(输入);
}

我已经把它编码好了,你也能检查一下吗

#include <iostream>
#include <string>
using namespace std;
class Vig {
   public:
      string k;
   Vig(string k) {
      for (int i = 0; i < k.size(); ++i) {
         if (k[i] >= 'A' && k[i] <= 'Z')
            this->k += k[i];
         else if (k[i] >= 'a' && k[i] <= 'z')
            this->k += k[i] + 'A' - 'a';
      }
   }
   string Vegencryption(string t) {
      string output;
      for (int i = 0, j = 0; i < t.length(); ++i) {
         char c = t[i];
         if (c >= 'a' && c <= 'z')
            c += 'A' - 'a';
         else if (c < 'A' || c > 'Z')
            continue;
         output += (c + k[j] - 2 * 'A') % 26 + 'A'; //added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
         j = (j + 1) % k.length();
      }
      return output;
   }
   string Vegdecryption(string t) {
      string output;
      for (int i = 0, j = 0; i < t.length(); ++i) {
         char c = t[i];
         if (c >= 'a' && c <= 'z')
            c += 'A' - 'a';
         else if (c < 'A' || c > 'Z')
            continue;
         output += (c - k[j] + 26) % 26 + 'A';//added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
         j = (j + 1) % k.length();
      }
      return output;
   }
   string Cesencryption(string t, int key){
      string output;
      char ch;
      for(int i = 0; t[i] != '\0'; ++i){
                ch = t[i];
                
                if(ch >= 'a' && ch <= 'z'){
                    ch = ch + key;
                    
                    if(ch > 'z'){
                        ch = ch - 'z' + 'a' - 1;
                    }
                    
                    t[i] = ch;
                }
                else if(ch >= 'A' && ch <= 'Z'){
                    ch = ch + key;
                    
                    if(ch > 'Z'){
                        ch = ch - 'Z' + 'A' - 1;
                    }
                    
                    t[i] = ch;
                }
            }
           output = t;
      return output;
   }
   string Cesdencryption(string t, int key){
      string output;
      char ch;
      for(int i = 0; t[i] != '\0'; ++i){
        ch = t[i];
                if(ch >= 'a' && ch <= 'z'){
                    ch = ch - key;
                    
                    if(ch < 'a'){
                        ch = ch + 'z' - 'a' + 1;
                    }
                    
                    t[i] = ch;
                }
                else if(ch >= 'A' && ch <= 'Z'){
                    ch = ch - key;
                    
                    if(ch > 'a'){
                        ch = ch + 'Z' - 'A' + 1;
                    }
                    
                    t[i] = ch;
                }
            }
           output = t;
      return output;
   }
};
int main() {
    char message[200];
    string key;
   int choice;

    cout << "Pick a Choice from the List:  \n(1)Caesar \n(2)Vigenere \n ";
    cin >> choice;
    if(choice == 2) //Vigenere
    {
        cout << "Pick a Choice from the List:  \n(1)Encryption\n(2)Decryption \n ";
        cin >> choice;
            if (choice == 1) 
            {
                cout << "Key: ";
                cin >> key;
                cin.ignore();
                Vig v(key);
                cout << "Enter a message to encrypt: ";
                cin.getline(message, 200);
                string ori = message;
                string encrypt = v.Vegencryption(ori);
                cout << "Encrypted Message: " << encrypt << endl;

            }
            else if (choice == 2)
            {
                cout << "Key: ";
                cin >> key;
                cin.ignore();
                Vig v(key);
                cout << "Enter a message to encrypt: ";
                cin.getline(message, 200);
                string ori = message;
                string decrypt = v.Vegdecryption(ori);
                cout << "Decrypted Message: " << decrypt << endl;
            }
            else
            {
                cout << "You Exit the Program.";
            }
    }
    else if (choice == 1) //Ceaser
    {
         cout << "Pick a Choice from the List:  \n(1)Encryption\n(2)Decryption \n ";
         cin >> choice;
            if (choice == 1)
            {
               int key;
               cout << "Enter a message to encrypt: ";
               cin.ignore();
               cin.getline(message, 200);
               cout << "Enter key: ";
               cin >> key;
               Vig v(message);
               string ori = message;
               string decrypt = v.Cesencryption(ori,key);
               cout << "Decrypted Message: " << decrypt << endl;

            }
            else if (choice == 2)
            {
               int key;
               cout << "Enter a message to dencrypt: ";
               cin.ignore();
               cin.getline(message, 200);
               cout << "Enter key: ";
               cin >> key;
               Vig v(message);
               string ori = message;
               string encryption = v.Cesdencryption(ori,key);
               cout << "Decrypted Message: " << encryption << endl;
            }
            else
            {
                cout << "You Exit the Program.";
            }
    }
    else
    {
       cout << "You Exit the Program.";
    }
}
#包括
#包括
使用名称空间std;
维格班{
公众:
串k;
Vig(字符串k){
对于(int i=0;i='A'&&k[i]k+=k[i];
如果(k[i]>='a'&&k[i]k+=k[i]+'a'-'a';
}
}
字符串加密(字符串t){
字符串输出;
对于(int i=0,j=0;i='a'和&c'Z')
继续;
输出+=(c+k[j]-2*'A')%26+'A';//添加了'A'以使其位于ASCII字母[65-90 | A-Z]的范围内
j=(j+1)%k.长度();
}
返回输出;
}
字符串解密(字符串t){
字符串输出;
对于(int i=0,j=0;i='a'和&c'Z')
继续;
输出+=(c-k[j]+26)%26+'A';//添加'A'以使其位于ASCII字母[65-90 | A-Z]的范围内
j=(j+1)%k.长度();
}
返回输出;
}
字符串加密(字符串t,整数密钥){
字符串输出;
char ch;
对于(int i=0;t[i]!='\0';++i){
ch=t[i];
如果(ch>='a'&&ch'z'){
ch=ch-'z'+'a'-1;
}
t[i]=ch;
}
否则如果(ch>='A'&&ch'Z'){
ch=ch-'Z'+'A'-1;
}
t[i]=ch;
}
}
输出=t;
返回输出;
}
字符串加密(字符串t,int键){
字符串输出;
char ch;
对于(int i=0;t[i]!='\0';++i){
ch=t[i];
如果(ch>='a'&&ch='a'&&ch'a'){
ch=ch+Z'-'A'+1;
}
t[i]=ch;
}
}
输出=t;
返回输出;
}
};
int main(){
字符消息[200];
字符串键;
智力选择;
选择;
if(choice==2)//Vigenere
{
选择;
如果(选项==1)
{
cout>键;
cin.ignore();
Vig v(键);

你能不能有一个
Vig
类,为什么不也有一个
Caesar
类呢?一旦你有了
Caesar
类,并且可以在你的Caesar密码程序中使用它,将
Vig
类添加到该程序中并使用它应该很容易。还有一些看起来很奇怪的事情,比如
if(答案==1){答案=1;…}
?为什么要分配任务?@Someprogrammerdude没有必要为caesar使用类,所以请保持这样。如果部分原因迫使您选择1或2。请您澄清这两段代码之间的关系以及您遇到的具体问题。据我所知,您在第一段代码中没有使用
Vic
。请使用c了解代码的错误。@churill我想把使用类的程序放在第一个程序中。这意味着两个不同的chiper方法将在同一个地方使用菜单。为什么我必须使用Vic c
struct Cipher
{
    virtual std::string encryption(std::string const&) = 0;
    virtual std::string decryption(std::string const&) = 0;
};

class Caesar : public Cipher
{
public:
    std::string encryption(std::string const& s) override
    {
        // TODO: Implement Caesar cipher encryption
    }

    std::string decryption(std::string const& s) override
    {
        // TODO: Implement Caesar cipher decryption
    }
};

class Vigenere : public Cipher
{
public:
    std::string encryption(std::string const& s) override
    {
        // TODO: Implement Vigenere cipher encryption
    }

    std::string decryption(std::string const& s) override
    {
        // TODO: Implement Vigenere cipher decryption
    }
};
Cipher* select_cipher()
{
    std::cout << "Please select cipher to use:\n";
    std::cout << "1) Caesar\n";
    std::cout << "2) Vigenere\n";

    int selection;
    std::cin >> selection;

    if (selection == 1)
    {
        return new Caesar;
    }
    else if (selection == 2)
    {
        return new Vigenere;
    }
    else
    {
        return nullptr;
    }
}
int select_method()
{
    std::cout << "Do you want to encrypt or decrypt?\n";
    std::cout << "1) Encrypt\n";
    std::cout << "2) Decrypt\n";

    int method;
    std::cin >> method;

    return method;
}
int main()
{
    Cipher* cipher = select_cipher();
    int method = select_method();

    std::string input;
    std::string result;

    // TODO: Read input string to encrypt or decrypt

    if (method == 1)
    {
        result = cipher->encryption(input);
    }
    else if (method == 2)
    {
        result = cipher->decryption(input);
    }

    std::cout << "Result = " << result << '\n';
}
#include <iostream>
#include <string>
using namespace std;
class Vig {
   public:
      string k;
   Vig(string k) {
      for (int i = 0; i < k.size(); ++i) {
         if (k[i] >= 'A' && k[i] <= 'Z')
            this->k += k[i];
         else if (k[i] >= 'a' && k[i] <= 'z')
            this->k += k[i] + 'A' - 'a';
      }
   }
   string Vegencryption(string t) {
      string output;
      for (int i = 0, j = 0; i < t.length(); ++i) {
         char c = t[i];
         if (c >= 'a' && c <= 'z')
            c += 'A' - 'a';
         else if (c < 'A' || c > 'Z')
            continue;
         output += (c + k[j] - 2 * 'A') % 26 + 'A'; //added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
         j = (j + 1) % k.length();
      }
      return output;
   }
   string Vegdecryption(string t) {
      string output;
      for (int i = 0, j = 0; i < t.length(); ++i) {
         char c = t[i];
         if (c >= 'a' && c <= 'z')
            c += 'A' - 'a';
         else if (c < 'A' || c > 'Z')
            continue;
         output += (c - k[j] + 26) % 26 + 'A';//added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
         j = (j + 1) % k.length();
      }
      return output;
   }
   string Cesencryption(string t, int key){
      string output;
      char ch;
      for(int i = 0; t[i] != '\0'; ++i){
                ch = t[i];
                
                if(ch >= 'a' && ch <= 'z'){
                    ch = ch + key;
                    
                    if(ch > 'z'){
                        ch = ch - 'z' + 'a' - 1;
                    }
                    
                    t[i] = ch;
                }
                else if(ch >= 'A' && ch <= 'Z'){
                    ch = ch + key;
                    
                    if(ch > 'Z'){
                        ch = ch - 'Z' + 'A' - 1;
                    }
                    
                    t[i] = ch;
                }
            }
           output = t;
      return output;
   }
   string Cesdencryption(string t, int key){
      string output;
      char ch;
      for(int i = 0; t[i] != '\0'; ++i){
        ch = t[i];
                if(ch >= 'a' && ch <= 'z'){
                    ch = ch - key;
                    
                    if(ch < 'a'){
                        ch = ch + 'z' - 'a' + 1;
                    }
                    
                    t[i] = ch;
                }
                else if(ch >= 'A' && ch <= 'Z'){
                    ch = ch - key;
                    
                    if(ch > 'a'){
                        ch = ch + 'Z' - 'A' + 1;
                    }
                    
                    t[i] = ch;
                }
            }
           output = t;
      return output;
   }
};
int main() {
    char message[200];
    string key;
   int choice;

    cout << "Pick a Choice from the List:  \n(1)Caesar \n(2)Vigenere \n ";
    cin >> choice;
    if(choice == 2) //Vigenere
    {
        cout << "Pick a Choice from the List:  \n(1)Encryption\n(2)Decryption \n ";
        cin >> choice;
            if (choice == 1) 
            {
                cout << "Key: ";
                cin >> key;
                cin.ignore();
                Vig v(key);
                cout << "Enter a message to encrypt: ";
                cin.getline(message, 200);
                string ori = message;
                string encrypt = v.Vegencryption(ori);
                cout << "Encrypted Message: " << encrypt << endl;

            }
            else if (choice == 2)
            {
                cout << "Key: ";
                cin >> key;
                cin.ignore();
                Vig v(key);
                cout << "Enter a message to encrypt: ";
                cin.getline(message, 200);
                string ori = message;
                string decrypt = v.Vegdecryption(ori);
                cout << "Decrypted Message: " << decrypt << endl;
            }
            else
            {
                cout << "You Exit the Program.";
            }
    }
    else if (choice == 1) //Ceaser
    {
         cout << "Pick a Choice from the List:  \n(1)Encryption\n(2)Decryption \n ";
         cin >> choice;
            if (choice == 1)
            {
               int key;
               cout << "Enter a message to encrypt: ";
               cin.ignore();
               cin.getline(message, 200);
               cout << "Enter key: ";
               cin >> key;
               Vig v(message);
               string ori = message;
               string decrypt = v.Cesencryption(ori,key);
               cout << "Decrypted Message: " << decrypt << endl;

            }
            else if (choice == 2)
            {
               int key;
               cout << "Enter a message to dencrypt: ";
               cin.ignore();
               cin.getline(message, 200);
               cout << "Enter key: ";
               cin >> key;
               Vig v(message);
               string ori = message;
               string encryption = v.Cesdencryption(ori,key);
               cout << "Decrypted Message: " << encryption << endl;
            }
            else
            {
                cout << "You Exit the Program.";
            }
    }
    else
    {
       cout << "You Exit the Program.";
    }
}