关闭。这个问题是opinion-based .它目前不接受答案。
想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它.
7年前关闭。
Improve this question
在过去的几周里,我一直在学习 C++,并被分配了一项任务。我已经完成了这项任务,并想知道是否有人能够为我指出正确的方向,以使我的代码更加高效和直接。
以下是任务简介:
请看下面的部分 PDL 程序,它读取一个表示要加密的值的(三位)整数,一个表示加密 key 的(一位)整数,加密该值并打印加密的值。使用的加密方法是将给定数字中的每个数字替换为((该数字加 key 的总和)模 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,给定的 key 是 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
这是我的解决方案代码:
`
#包括
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++ 编程还是很陌生。谢谢。
最佳答案
好吧,鲍里斯,
我是否可以建议您从 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;
}
关于c++ - 找到解决方案的最有效方法 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26719759/