为什么我的代码不起作用? (加密三位数字) [英] Why wont my code work? (encrypting a three digit number)

查看:68
本文介绍了为什么我的代码不起作用? (加密三位数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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.


这篇关于为什么我的代码不起作用? (加密三位数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆