0

我的程序一直有一个小问题,我想做的是开发一种方法让用户模拟密码的可能强度。这是假设所有密码都是排列(我知道很奇怪,但我认为这是为了阻止数据变得更加笨拙。)使用等式......

//n!/(nr)! 当n!= (e^-n)*(n^n) sqrt(2(pi)n)。当 n 是正在使用的字符数并且 r 是密码长度时

无论我说什么,我都会收到 nan 作为答案。我想也许我的方程不正确(也许不知何故我被零除)所以我重新设计了它并大大简化了它。但这似乎不是问题,尽管我觉得这让我更接近正确。但我认为数字溢出可能在这里产生影响?但我真的不知道如何解决这样的问题。我尝试从不同的数据类型跳转,但似乎没有任何效果。

我的模数也有问题。它返回小于零的数字时间,所以以我的笨拙知识告诉我,也许我再次溢出它但是我将如何使用 % 而不将其定义为 int 呢?也许解决上述问题会解决这个问题?

对于给予我的任何帮助,我将不胜感激。如何处理 nan 的返回值?有没有一步步解决的现状?它几乎总是溢出还是可能是别的东西?

代码本身。

#include <iostream>
#include <cmath>

using namespace std;

const int SECONDS_IN_YEAR = 31556926;
const int SECONDS_IN_DAY  = 86400;
const int SECONDS_IN_HOUR = 3600;
const int SECONDS_IN_MIN  = 60;

int main()
{

    int passwordLength ,characterSymbols;
    double instructionsPerSecond, instructionSuccess;

    ////////////////////////////////////////////////////////////////////////////////
    //Equations needed
    // n!/(n-r)!
    //n is the number of letters in the alphabet
    //and r is the number of letters in the password

    // n! = (e^-n)*(n^n) sqrt(2(pi)n)
    double numeratorFactorial  = (pow(M_E,-characterSymbols))
                                *(pow(characterSymbols,characterSymbols))
                                *(sqrt(2*M_PI*characterSymbols));
    // (n-r)
    double characterMinusLength= (characterSymbols-passwordLength);

    // (n-r)! = (e^-(n-r)) * ((n-r)^(n-r)) * sqrt(2(pi)(n-r))
    double denominatorFactorial = ((pow(M_E, -(characterMinusLength)))*
                                (pow((characterMinusLength),(characterMinusLength)))
                                * (sqrt(2*M_PI*(characterMinusLength))));

    // n!/(n-r)!
    long double passwordPermutation = (numeratorFactorial / denominatorFactorial);

    // (passwords)* (instructions/Password) * (seconds/instruction) = sec
    int passwordSeconds = (passwordPermutation * instructionSuccess)
                         *(1/instructionsPerSecond);



    int passwordMin  = passwordSeconds / SECONDS_IN_MIN ;
    int passwordHour = passwordSeconds / SECONDS_IN_HOUR;
    int passwordDay  = passwordSeconds / SECONDS_IN_DAY ;
    int passwordYear = passwordSeconds / SECONDS_IN_YEAR;


    ////////////////////////////////////////////////////////////////////////////////
    //Explain purpose of program
    cout << "This program is designed to simulate the strength of passwords." << endl;

    //Ask for alphabet
    cout << "But first, share with me the max number of characters you'd be using."
         << endl;
    cin >> characterSymbols;
    //Reflect information
    cout << "We will be using " << characterSymbols << " character symbols to "
         << " construct the password.\n" << endl;



    ///////////////////////////////////////////////////////////////////////////////
    //Input length of password
    cout << "\n\nWill you give me the length of proposed password?" << endl;
    cin >> passwordLength;

    //Repeat information
    cout << "The password length will be " << passwordLength << "." <<endl;



    //cout permutations
    cout << "This would lead to " << passwordPermutation << " unique password\n"
         << endl;



    ////////////////////////////////////////////////////////////////////////////////
    //Ask for computer strength
    cout << "How powerful is this computer? How many instructions per second " << endl;
    cout << "can it accomplish?" << endl;
    cin >> instructionsPerSecond;

    //Read out computer strength
    cout << "The computer can do " << instructionsPerSecond << " instructions/second"
         << endl << endl;



    ////////////////////////////////////////////////////////////////////////////////

    //Ask for instructions/password
    cout << "The number of instructions needed to test your password is." << endl
         << endl;
    cin >> instructionSuccess;

    //reflect
    cout << "This computer can do " << instructionSuccess
     << " instructions/password" << endl;


    ////////////////////////////////////////////////////////////////////////////////
    cout << "\n\nThe amount of seconds it'll take to crack this passcode is... "
         << endl << passwordSeconds << " seconds.\n\n\n\n\n" << endl;

    ////////////////////////////////////////////////////////////////////////////////
    //Reflect all information in an easily readable table
    cout << "Number of character symbols using... " << characterSymbols      << endl;
    cout << "Length of password... "                << passwordLength        << endl;
    cout << "Number of permutations... "            << passwordPermutation   << endl;
    cout << "Instructions per second... "           << instructionsPerSecond << endl;
    cout << "Instructions per password..."          << instructionSuccess    << endl;

    cout << endl << endl << endl;

    ////////////////////////////////////////////////////////////////////////////////
    //Add in conversions for min, hour, day, years
    cout << "Number of seconds to break..." << passwordSeconds << endl;


    cout << "Converted to minutes..."       << passwordMin     << endl;
    passwordMin = passwordSeconds / SECONDS_IN_MIN;
    passwordSeconds = passwordSeconds % SECONDS_IN_MIN;

    cout << "Converted to hours..."         << passwordHour    << endl;
    passwordHour = passwordSeconds / SECONDS_IN_HOUR;
    passwordSeconds = passwordSeconds % SECONDS_IN_MIN;

    cout << "Converted to days..."          << passwordDay     << endl;
    passwordDay = passwordSeconds / SECONDS_IN_DAY;
    passwordSeconds = passwordSeconds % SECONDS_IN_DAY;

    cout << "Converted to years..."         << passwordYear    << endl;
    passwordYear = passwordSeconds / SECONDS_IN_YEAR;
    passwordSeconds = passwordSeconds % SECONDS_IN_YEAR;

    return (0);
}
4

2 回答 2

2

“nan”代表“不是数字”。发生这种情况是因为您已经声明了变量 characterSymbols 和 passwordLength 而没有给它们一个初始值。

您必须在使用任何变量之前对其进行初始化 - 如果您不这样做,那么您将有未确定的行为。例如:

int x;
int y;
int z = x + y;

在这里无法预测 z 将等于什么,因为我们不知道 x 或 y 等于什么。同样,您的代码应类似于:

int characterSymbols = 10;  //or whatever you want the initial value to be

...

double numeratorFactorial  = (pow(M_E,-characterSymbols))
                            *(pow(characterSymbols,characterSymbols))
                            *(sqrt(2*M_PI*characterSymbols));

这样, numeratorFactorial 将具有有效值。

于 2013-10-10T19:12:41.547 回答
0

当您实际声明变量时,您似乎认为您正在声明“方程式”。你写:

double numeratorFactorial  = (pow(M_E,-characterSymbols))
                            *(pow(characterSymbols,characterSymbols))
                            *(sqrt(2*M_PI*characterSymbols));

characterSymbols没有定义,只是“声明”。 characterSymbols在它上面声明了,但它没有值……还没有。稍后您使用cin它来获取一个值,但是当您第一次声明时,numeratorFactorial您不能简单地期望程序在更改numeratorFactorial时将值插入characterSymbols

一些定义可能是有序的: 该语句double numeratorFactorial = some_value;创建了一个名为的变量numeratorFactorial,并用于some_value立即填充该变量。你想要的是一个函数,一个可以“传递值”的逻辑语句,以便在需要时生成值。例如,对于您的分子阶乘:

double numeratorFactorial(double characterSymbols) {
    return  (pow(M_E,-characterSymbols))
           *(pow(characterSymbols,characterSymbols))
           *(sqrt(2*M_PI*characterSymbols));
}

int main() {
    std::cout << "Numerator Factorial test: " << numeratorFactorial(5.0) << std::endl;
}

请注意,您不能主函数中声明函数。

这种东西是编程基础,看起来你在学会走路之前就在尝试跑步。找一本好书,比如C++ Primer,然后自己调整节奏。

于 2013-10-10T19:00:28.493 回答