找到解决方案最有效的方法 - C ++

时间:2014-11-03 17:37:46

标签: c++ performance encryption

过去几周我一直在学习C ++,并且已经完成了一项任务。我已经完成了这项任务,并想知道是否有人能够指出我在使我的代码更有效和更直接方面的正确方向。

以下是该任务的简要说明:

查看以下部分PDL程序,该程序读取表示要加密的值的(三位)整数,表示加密密钥的(一位)整数,加密值并打印加密值。使用的加密方法是将给定数字中的每个数字替换为((该数字加上键的总和)模10)然后交换第一个和最后一个“加密”数字。

produceEncryptedNumber output( "Enter the original three-digit number: ") input( originalNumber) //read in a (three-digit) number output( "Enter the key: ") input( key) //read in a (one-digit) number call isolateDigits //find out the 3 digits that make up the number call replaceDigits //’encrypt’ each of the three digits call swapDigit1WithDigit3 //swap first and last digit call recomposeEncryptedNumber //recreate encrypted number from ‘encrypted’ values //output encrypted number output( "The encrypted number for ", originalNumber, " is ", encryptedNumber, ".")

例如,如果输入的数字是216并且给出的密钥是7,则在应用所描述的加密程序后,第一个数字(2)将变为9,中间数字(1)将变为8并且最后一个数字(6) )将变为3.然后交换第一个和最后一个加密数字。程序显示加密的数字:在这种情况下为389。

程序应如何出现在命令窗口中的示例(具有预期的返回值):

Enter the original three-digit number: 216 Enter the key: 7 The encrypted number for 216 is 389.

Enter the original three-digit number: 123 Enter the key: 7 The encrypted number for 123 is 098

这是我的解决方案代码:

`
    #include

using namespace std;

// Global Variables 
int originalNumber, key, a, b, c, number, valA, valB, valC, encryptedNumber;

// Main Section of the Program
int main()

{
    // Get original Number
    cout << ("\nEnter the original three-digit number: ");
    cin >> originalNumber;

    // Get Key
    cout << ("\nEnter the key: ");
    cin >> key;

    // Call isolateDigits
    void isolateDigits();
    isolateDigits();

    // Call replaceDigits
    void replaceDigits();
    replaceDigits();

    // Call swapDigit1WithDigit3
    void swapDigit1WithDigit3();
    swapDigit1WithDigit3();

    // Call recomposeEncryptedNumber
    void recomposeEncryptedNumber();
    recomposeEncryptedNumber();

    // Check for a 3-digit outcome and print out the original + final values

    // If the value of the outcome is more than 2-digits and less than 3-digits, then add a single 0 at the front of the output.
    if (encryptedNumber >= 9 && encryptedNumber <= 99)
        cout << "\nThe encrypted number for " << originalNumber << " is 0" << encryptedNumber << ".\n\n";

    // If the value of the outcome is less than 2-digits, then add two 00's at the front of the output.
    else if (encryptedNumber <= 9)
        cout << "\nThe encrypted number for " << originalNumber << " is 00" << encryptedNumber << ".\n\n";

    // If the value is 3-digits exactly then simply output the value in the normal order. 
    else
        cout << "\nThe encrypted number for " << originalNumber << " is " << encryptedNumber << ".\n\n";

    // Pause at end
    system("pause");
    return(0);
}

// isolateDigits procedure
void isolateDigits()
{
    a = originalNumber / 100 % 10;
    b = originalNumber / 10 % 10;
    c = originalNumber % 10;
}

// replaceDigits procedure for Encryption
void replaceDigits()
{
    valA = a + key;
    valB = b + key;
    valC = c + key;
}

// swapDigit1WithDigit3 procedure
void swapDigit1WithDigit3()
{
    valC = a + key;
    valB = b + key;
    valA = c + key;
}

// recomposeEncryptedNumber procedure
void recomposeEncryptedNumber()
{
    // Check for values above 2-digits
    if (valA >= 10)
        valA = valA - 10;
    if (valB >= 10)
        valB = valB - 10;
    if (valC >= 10)
        valC = valC - 10;

    // Put the values together
    encryptedNumber = 100 * valA + 10 * valB + valC;
}

`

我非常感谢有关如何改进此解决方案的任何提示,我总是热衷于学习新事物,请放轻松地对待我,我仍然很擅长使用C ++进行编程。感谢。

1 个答案:

答案 0 :(得分:0)

Alright Boris,

  • 将加密代码移入其自己的班级

  • 将步骤集中到单个加密函数

  • 替换数字在您当前的解决方案中实际上没有做任何有用的工作

我可能建议你从以下的Encryptor类声明开始,然后从那里开始:

class Encryptor
{
 Public:
  Encryptor();
  ~Encryptor();

  void setKey(int key);
  unsigned int encryptNumber(unsigned int number);

 Private:
  void isolateDigits();
  void replaceDigits();
  void swapDigits();
  void recomposeNumber();

  unsigned int a,b,c;
  unsigned int number;
  unsigned int key;
};

我建议使用无符号整数,因为你的打印语句实际上不适用于负整数。

作为提示,replaceDigits看起来像:

void Encryptor::replaceDigits()
{
  a += key;
  b += key;
  c += key;
}