为什么我的代码不起作用? (加密三位数字)

编程入门 行业动态 更新时间:2024-10-12 05:51:16
本文介绍了为什么我的代码不起作用? (加密三位数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#include <iostream> //for cin >> and cout << using namespace std; int main() // main algorithm { int n1, n2, n3, key, originalnumber, encryptednumber; cout << ("Enter original number "); cin >> (originalnumber); cout << ("enter encryption number "); cin >> (key); void isolatedigits(); n1 = originalnumber / 100; n2 = (originalnumber % 100) / 100; n3 = originalnumber % 10; void replacedigits(); n1 = n1 + key; n2 = n2 + key; n3 = n3 + key; void swapdigits(); n1 = n3; n3 = n1; void recomposeencryptednumber(); n1 = n1 * 100; n2 = n2 * 10; encryptednumber = n1 + n2 + n3; isolatedigits(); replacedigits(); swapdigits(); recomposeencryptednumber(); cout << (encryptednumber); system("pause"); //to hold the output screen return(0); }

推荐答案

main ,没有任何身体,所以他们什么都不做。它们都应该删除,留下类似的内容: You have a number of redundant function definitions inside main, none of which have any bodies, so they don't do anything. They should all be removed, leaving something like: #include <iostream> //for cin >> and cout << using namespace std; int main() // main algorithm { int n1, n2, n3, key, originalnumber, encryptednumber; cout << ("Enter original number "); cin >> (originalnumber); cout << ("enter encryption number "); cin >> (key); n1 = originalnumber / 100; n2 = (originalnumber % 100) / 100; n3 = originalnumber % 10; n1 = n1 + key; n2 = n2 + key; n3 = n3 + key; n1 = n3; n3 = n1; n1 = n1 * 100; n2 = n2 * 10; encryptednumber = n1 + n2 + n3; cout << (encryptednumber); system("pause"); //to hold the output screen return(0); } </iostream>

理查德在解决方案1中说的是正确的。我假设,你已经找到了某个地方的代码,原作者正在将代码重构为几个独立的函数。你在事情中抓住了它:-) 关于算法,有几件事情是行不通的: What Richard said in Solution 1 is correct. I assume, you have "found" the code somewhere and the original author was in the process of refactoring the code into several independent functions. You caught it right in the middle of things :-) Regarding the algorithm, there are several things that can't work: n1 = originalnumber / 100; n2 = (originalnumber % 100) / 100; n3 = originalnumber % 10;

显然,n1到n3应该收到一个三位十进制数的数字。在这种情况下,第二个陈述必须是:

Obviously, n1 to n3 should receive the digits of a three-digit decimal number. In that case the second statement needs to be:

n2 = (originalnumber % 100) / 10;

在下一部分中,您希望通过键修改每个数字。但是为了使下面的代码工作,这个修改必须保持在0 ... 9的范围内。因此该部分应该是:

In the next section you want to modify each of the digits by the key. But for the following code to work, this modification must remain in the range of 0 ... 9. Hence that section should read:

n1 = (n1 + key) % 10; n2 = (n2 + key) % 10; n3 = (n3 + key) % 10;

最后,交换两个变量的值不能通过以下方式完成:

And lastly, exchanging the values of two variables cannot be done by:

n1 = n3; n3 = n1;

所有这一切都是将n3的值分配给n3和n1。它可能应该是:

All that does is to assign to value of n3 to both, n3 and n1. It should probably read:

int temp = n1; n1 = n3; n3 = temp;

谢谢,这是我的代码从头开始我有一个规范来创建一个程序,其中是 Thanks, this was my code from scratch i have a specification to create a program from which is Quote:

1。规格 。查看以下部分PDL程序,该程序读取表示要加密的值的(三位)整数,表示加密密钥的(一位)整数,加密值并打印加密值。使用的加密方法是将给定数字中的每个数字替换为((该数字加上键的总和)模10)然后交换第一个和最后一个加密数字。 produceEncryptedNumber 输出(输入原来的三位数字:) 输入(originalNumber)//读入(三 - 数字) 输出(输入密钥:) 输入(密钥)//读入(一位数)数字 调用isolateDigits //找出组成数字的3位数字 调用replaceDigits //'加密'三位数中的每一位 调用swapDigit1WithDigit3 //交换第一个和最后一位数字 调用recomposeEncryptedNumber //从'加密'值重新创建加密数字 //输出加密数字 输出(加密数字为,originalNumber,是,encryptedNumber。) 例如,如果输入的数字是216并且给出的密钥是7,则在应用加密程序后描述第一个数字(2)将变为9,中间数字(1)将变为8,最后一位数字(6)将变为3.第一个和最后一个加密数字然后交换。程序显示加密的数字:在这种情况下为389。

1. Specification . Look at the following partial PDL program that reads a (three-digit) integer representing the value to be encrypted, a (one-digit) integer representing the encryption key, encrypt the value and print the encrypted value. The encrypting method used is that each digit in the given number is replaced by ((the sum of that digit plus key) modulo 10) then the first and last "encrypted" digits are swapped. 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, ".") For example, if the number entered was 216 and the key given was 7, after applying the encryption procedure described the first digit (2) would become 9, the middle digit (1) would become 8 and the last digit (6) would become 3. The first and last encrypted digits are then swapped. The program displays the encrypted number: that is 389 in this case.

更多推荐

为什么我的代码不起作用? (加密三位数字)

本文发布于:2023-11-30 07:57:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   代码   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!