移动设备上的现代C ++挑战–最大公约数

Hello ! I’m Xavier Jouvenot and here is the second part of a long series on The Modern C++ Challenge.In this article, I am going to explain how I solved the second problem in C++, and how I integrated the solution in an Android project.

第二个问题的目的很简单,我们必须计算用户给定的两个正整数的最大公约数,然后将其打印给用户。解决方案将使用C ++和接口进行计算以获取用户输入并显示 结果将通过Android Studio Framework处理。

I encourage you to read the previous part of this series, since we are going to continue our program created in it.

The C++ solution

Implementing the algorithm to find the greatest common divisor of two numbers is not simple… it literally given by the standard ! 😉
Indeed, in C++17, the function std::gcd does exactly that !

So all we have to do, is to include the « header from the std and use the std::gcd function.

If you are using an older version of C++ standard, then you will have to implement it.I can recommend you this Stack Exchange post about it, if you want. 🙂

The UI interface on Android Studio

与解决方案的C ++实现不同,用户界面将需要一些工作。实际上,我们希望能够在我们的应用程序中为第一个问题和第二个问题提供解决方案。

为了实现此目标,我们将需要做两件事:

  • 添加一个新的活动,将显示第二个问题并计算解决方案添加按钮以便能够从第一个问题转到第二个问题。

A new Activity for a new problem

要创建新的活动,您需要做的就是右键单击您的应用程序文件夹架构,然后转到空活动如下所示:

然后,指定新活动的名称(我命名为我的问题_2),然后点击完创建新的活动。

好的,但是具体是什么意思?首先,如果打开文件AndroidManifest.xml,您可以看到您的活动已在此处声明:

<activity android:name=".Problem_2"></activity>

然后,如果您查看Java文件,则必须使用以您的新活动命名的类创建一个新文件。在显示结果之前,我们将在该类中处理用户输入以将其提供给C ++算法 给用户。

最后,在子文件夹中布局在您的资源中,您可以看到已添加了一个新的xml。当用户查看第二个问题时,该xml将包含我们要在屏幕上显示的所有元素。

As in the first problem), all we need are EditText and TextView to get the user input and display the solution to the user, so I encourage to read it) if you haven’t.

Linking the two activities

现在我们已经为问题创建了新的活动,我们需要能够通过我们的应用程序访问它,最简单的方法是在第一个问题界面上创建一个按钮以转到第二个问题界面。

为此,我们在第一个Activity中添加回调(将由按钮调用的函数),该函数如下所示:

public void goToNextProblem(View v)
{
    Intent intent = new Intent(this, Problem_2.class);
    startActivity(intent);
}

在此功能中,我们创建了一个意图并为其提供第二个活动的类。然后,我们开始该活动。 很简单,不是吗? 🙂

现在我们有了回调,让我们在活动中创建一个按钮,并将其链接到回调。这可以通过在第一个活动的布局中添加以下元素来实现:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToNextProblem"
    android:text="@string/button_next"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.95"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.954" />

重要的是我们有这个领域android:onClick设置回调的名称。这样,当用户单击按钮时,它将触发我们的功能。

您可以在第二个活动中执行相同的操作以返回到第一个活动,而且您对第一个问题有正确的用户界面,我们要做的就是将用户界面链接到C ++ 算法。

Using C++ native code

在这一部分中,我们将研究第二个活动的Java类。

public native String Gcd(int i, int j);

private int extractNumberFromEditText(EditText et)
{
    assert et.getText().length() != 0 : "The EditText must not be empty";
    return Integer.parseInt(et.getText().toString());
}

private void computeAndDisplayResult()
{
    EditText et1 = findViewById(R.id.first_input_number);
    EditText et2 = findViewById(R.id.second_input_number);

    int firstNumber = extractNumberFromEditText(et1);
    int secondNumber = extractNumberFromEditText(et2);

    TextView tv = findViewById(R.id.result);
    tv.setText(getString(R.string.result_placeholder, Gcd(firstNumber, secondNumber)));
}

If you have read my blog post about the resolution of the first problem, this part doesn’t bring new.First, We declare the function Gcd linked in the C++ library.Then we have a method which get the EditText with the user input, we extract the input from them, we get the TextView where we are going to display the result, we compute the result and we display it.

但是与第一个问题不同,此代码位于私有函数中。 不在编辑文字回调。这是因为一件重要的事情。 如果只有一个输入用户,我们将无法计算GCD算法。我们必须确保同时填写两个用户输入。

所以这是回调的样子:

EditText et = findViewById(R.id.first_input_number);
et.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        EditText et = findViewById(R.id.second_input_number);
        if(et.getText().length() == 0 || count == 0)
        {
            return;
        }
        computeAndDisplayResult();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

所以首先编辑文字,当文本更改时,我们检查新文本是否为空(计数== 0),是第二个编辑文字不为空(et.getText()。length()== 0)。如果没有一个为空,我们调用函数以计算并显示结果。 如果一个是空的,我们将直接返回不做任何事情。编辑文字该代码是相似的,只有编辑文字ID更改。 😉

Conclusion

So, we now have an application which can solve the first two problems of The Modern C++ Challenge and learned new thing on Android Studio which, for this problem, was the real purpose for me, since the C++ algorithm is already integrated in the C++ standard.

You can note that the solutions, written in this post, don’t include all the sources to make a running program, but only the interesting part of the sources to solve this problem.If you want to see the programs from end to end, you can go on my GitHub account, explore the full solution, add comments or ask questions if you want to, on the platform you read this article, it will also help me improve the quality of my articles.

谢谢大家阅读本文,直到我下一篇文章,度过了美好的一天day

Interesting links

from: https://dev.to//10xlearner/the-modern-cpp-challenge-on-mobile-greatest-commont-divisor-1p58

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值