【问题标题】:rewrite c# Stopwatch to c++将c#秒表重写为c++
【发布时间】:2019-02-07 05:33:10
【问题描述】:

我正在将此秒表代码从 C# 重写为 C++。

我已经重写了一些代码(如果您认为有帮助,我可以附上它)但对var watch = Stopwatch.StartNew() 之后的行感到困惑。 C++有类似的东西吗?我应该在 C++ 中为 watch 放置什么样的变量类型?

namespace BruteForcePasswordGeneration
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

            int passwordLength=0;

            Console.WriteLine("Enter the password length");

            passwordLength = Convert.ToInt32(Console.ReadLine());

            BigInteger iPossibilities = (BigInteger)Math.Pow((double)chars.Length, (double)passwordLength);
            Console.WriteLine("{0} words total. Press enter to continue;", iPossibilities);
            Console.ReadLine();

            var watch = Stopwatch.StartNew();
            for (BigInteger i = 0; i < iPossibilities; i++)
            {
                string theword = "";
                BigInteger val = i;
                for (int j = 0; j < passwordLength; j++)
                {
                    BigInteger ch = val % chars.Length;
                    theword = chars[(int)ch] + theword;
                    val = val / chars.Length;
                }
                Console.WriteLine(theword);
            }
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("It took {0} seconds to generate {1} possible combinations", elapsedMs / 1000, iPossibilities);
            Console.ReadLine();

        }
    }
}

【问题讨论】:

标签: c# c++


【解决方案1】:

虽然您可以根据需要在 C++ 中编写“秒表”类,但通常只使用 high_resolution_clock::now() 来获取开始和停止时间。然后,您可以使用duration_cast 来获得所需形式的差异。

请原谅我,我认为要求用户在启动程序后输入密码长度没有任何实际意义。至少对我来说,使用“gen_passwords 4”之类的东西来生成所有长度为 4 的密码似乎更容易。

这为代码提供了这样的一般顺序:

#include <iostream>
#include <chrono>
#include <string>

int main(int argc, char **argv) { 
    static const std::string chars{ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };

    if (argc != 2) {
        std::cerr << "Usage: generate <password length>\n";
        return EXIT_FAILURE;
    }

    int passwordLength = std::stoi(argv[1]);

    unsigned long long iPossibilities = std::pow(chars.size(), passwordLength);

    using namespace std::chrono;

    auto start = high_resolution_clock::now();
    for (unsigned long long i = 0; i < iPossibilities; i++) {
        std::string theword;
        unsigned long long val = i;
        for (int j = 0; j < passwordLength; j++) {
            size_t ch = val % chars.size();
            theword = chars[ch] + theword;
            val /= chars.size();
        }
        std::cout << theword << '\n';
    }
    auto stop = high_resolution_clock::now();

    double elapsed = duration_cast<milliseconds>(stop - start).count() / 1000.0;
    std::cerr << "It took " << elapsed << " seconds to generate " << iPossibilities << " combinations\n";
}

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2011-01-05
    • 2016-01-21
    相关资源
    最近更新 更多