android - 如何在 Android 设备上直接将简单的 C++ 程序作为可执行文件运行?

标签 android c++ terminal executable

我在 Windows(10-64 位)PC 上使用代码块用 C++ 语言编写了一个非常简单的程序。该代码由GCC编译器编译,生成*.exe格式的可执行文件。现在,我想在我的 Android 设备或任何其他 Android 设备上运行该二进制构建或可执行文件,但是我找不到直接执行此操作的方法。 我的 Android 设备中有两个应用程序可以帮助我编写代码、编译和运行/测试它。第一个是CPPDroid,另一个是解码器。我可以使用这些应用程序将我的代码复制到其中之一并编译代码然后运行它,但是这个过程似乎非常大且风险很大。在编辑器中复制或打开整个代码是有风险的,因为由于 Android 设备的触摸屏较小,我可能会在不知不觉中编辑或修改代码。

我已经尝试在互联网上搜索问题的解决方案,但未能找到。是的,有一些答案针对这个问题并试图解决它们,但是它们要么很难理解,要么没有帮助。我是一个新手,我不太了解如何使用 android studio 来制作一个应用程序来做到这一点。

这是我的代码:

#include<bits/stdc++.h>
#include<conio.h>
#define EPSILON 0.00001

using namespace std;

class Bisection
{
    int noofcaparr, *coeffarr, *powerarr, eqn;
    double var_x, result, var_a, var_b, var_c;
    char *eqnprnt;

    public:


        Bisection()
        {
            int noofcaparr = 0;
        }
        Bisection(const Bisection &obj)
        {
            this->noofcaparr = obj.noofcaparr;
            this->coeffarr = obj.coeffarr;
            this->powerarr = obj.powerarr;
            this->eqn = obj.eqn;
            this->var_x = obj.var_x;
            //this->result = obj.result;
            //this->var_a = obj.var_a;
            this->var_b = obj.var_b;
            this->var_c = obj.var_c;
            this->eqnprnt = obj.eqnprnt;
        }

        void geteqn();
        void showeqn();
        double setupeqn();
        void showresult();
        void getvariable();
        double roundoff(double &);
        Bisection bsmethodcomputn(Bisection &);

};

void Bisection::getvariable()
{
    this->var_a = 0;
    cout<<"\n\n\n\t ENTER THE VALUE OF VARIABLE:  ";
    cin>>this->var_a;
}

void Bisection::geteqn()
{
    int c, i, n;
    system("cls");
    cout<<"\n\n\t\t How many terms do you have in your equation? ";
    cout<<"\n\t For Example:  x^3 - 4x - 9 = 0   , has '3' terms     and ";
    cout<<"\n\t               x^4 + x^3 - 7x^2 - x + 5 = 0   , has '5' terms";
    cout<<"\n\t Enter the number of terms present in your equation:  ";
    cin>>this->noofcaparr;
    n = this->noofcaparr-1;

    this->coeffarr =  new int[n];
    this->powerarr = new int[n];

    for(i=0, c=1; i<=n; i++, c++ )
    {
        cout<<endl<<endl<<"\t\t Please enter the "<<c<<" th/st/nd/rd highest degree of x:  ";
        cin>>this->powerarr[i];

        cout<<endl<<endl<<"\t\t Please enter the coefficient of "<<c<<" th/st/nd/rd highest degree of x (with sign -/+):  ";
        cin>>this->coeffarr[i];
    }
/*    cout<<"\n\n\n\t\t Enter the value of x:  ";
    cin>>this->var_a; */
    cout<<endl<<endl<<"\n\n\t Values Set!";
    getch();


}


void Bisection::showeqn()
{
    int i, n;
    n = this->noofcaparr-1;
    system("cls");
    cout<<endl<<endl<<"\t\t Your equation is:   ";

        for(i=0; i<=n; i++ )
        {

            if(this->powerarr[i]==0)
            {
                if(i==0)
                {
                    if(this->coeffarr[i]>= 0)
                    {
                        if(this->coeffarr[i]==0)
                        {
                            cout<<" +0 ";
                        }

                        else
                        {
                            if(this->coeffarr[i]==1)
                            {
                                cout<<" 1";
                            }

                            else
                            {
                                cout<<" "<<(this->coeffarr[i])<<" ";
                            }
                        }
                    }
                    else
                    {
                        if(this->coeffarr[i]== -1)
                        {
                            cout<<" -"<<"1";
                        }
                        else
                        {
                            cout<<" "<<(this->coeffarr[i])<<" ";
                        }
                    }
                }
                else
                {
                    if(this->coeffarr[i]>= 0)
                    {
                        if(this->coeffarr[i]==0)
                            cout<<" +0 ";

                        else
                            cout<<" +"<<(this->coeffarr[i])<<" ";
                    }
                    else
                    {
                        cout<<" "<<(this->coeffarr[i])<<" ";
                    }
                }
            }

            else
            {
                if(this->powerarr[i]==1)
                {
                    if(i==0)
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            if(this->coeffarr[i]==0)
                            {
                                cout<<" +0 ";
                            }
                            else
                            {
                                if(this->coeffarr[i]==1)
                                {
                                    cout<<"x";
                                }
                                else
                                {
                                    cout<<" +"<<(this->coeffarr[i])<<"x";
                                }
                            }
                        }
                        else
                        {
                            if(this->coeffarr[i]== -1)
                            {
                                cout<<" -"<<"x ";
                            }
                            else
                            {
                                cout<<(this->coeffarr[i])<<"x";
                            }
                        }
                    }
                    else
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            cout<<"+"<<(this->coeffarr[i])<<"x";
                        }
                        else
                        {
                            cout<<(this->coeffarr[i])<<"x";
                        }
                    }
                }
                else
                {
                    if(i==0)
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            if(this->coeffarr[i]==1)
                            {
                                cout<<"x^"<<this->powerarr[i]<<"  ";
                            }
                            else
                            {
                                if(this->coeffarr[i]==0)
                                {
                                    cout<<" +0 ";
                                }
                                else
                                {
                                    cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                                }
                            }

                        }
                        else
                        {
                            if(this->coeffarr[i]== -1)
                            {
                                cout<<" -"<<"x^"<<this->powerarr[i]<<"  ";
                            }
                            else
                            {
                                cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                            }
                        }
                    }
                    else
                    {
                        if(this->coeffarr[i]>= 0)
                        {
                            cout<<" +"<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                        }
                        else
                        {
                            cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                        }
                    }
                }
            }
        }
    cout<<" = 0  = f(x) {let}";
}

double Bisection::setupeqn()
{
    this->result = 0;
    double rslt, rndoff_res;
    rslt = this->result;
    int n = this->noofcaparr - 1;
    for (int i=0; i <= n; i++)
    {
        double pwr, cfr;
        double x;
        x = this->var_a;
        pwr= this->powerarr[i];
        cfr = this->coeffarr[i];
        rslt += (pow(x,pwr)*cfr);
        rndoff_res = this->roundoff(rslt);
        rslt = rndoff_res;
        //cout<<endl<<endl<<endl<<"\t Value of var_a: "<<x;
        //cout<<endl<<endl<<endl<<"\t Value of powerarr: "<<pwr;
        //cout<<endl<<endl<<endl<<"\t Value of coeffarr: "<<cfr;
        //cout<<"\n\n\t result +=  (pow(this->var_a, this->powerarr[i])* this->coeffarr[i]) = "<<rslt;
        this->result= rslt;
    }


    //cout<<endl<<endl<<endl<<"\t Value of result: "<<rslt;
    return (this->result);
}

void Bisection::showresult()
{

    cout<<endl<<endl<<"\t\t This is your result:  "<<this->result;

}

double Bisection::roundoff(double &res)
{
    // 37.66666 * 100 =3766.66
    // 3766.66 + .5 =37.6716    for rounding off value
    // then type cast to int so value is 3766
    // then divided by 100 so the value converted into 37.66
    ///double round_off_result = (int)(res * 100000 + .5);
    ///return (double)round_off_result / 100000;
    double round_off_result = res;
    setprecision(4);
    return round_off_result;
}

Bisection Bisection::bsmethodcomputn(Bisection &obj)
{
    double temp_c;
    Bisection obj2 = *this;

    cout<<"\n\n\t First value is = "<<this->result<<"\t\t Second value is = "<<obj.result;
    cout<<endl<<endl<<endl;
    //void bisection(double a, double b)

        if (this->result * obj.result >= 0)
        {

            cout << "\t \t You have not assumed right a and b\n";

            cout<<"\n\n\t First value is = "<<this->var_a<<"\t\t Second value is = "<<obj.var_a;
            //return;
        }



        else
        {

            cout<<"\n\n\t First value is = "<<this->var_a<<"\t\t Second value is = "<<obj.var_a;
            double a = this->var_a;
            double b = obj.var_a;
            //Bisection obj2 = *this ;

            obj2.var_a = a;
            obj2.setupeqn();
            double c = obj2.var_a;
            int counter = 1;

            while ((b-a) >= EPSILON)
            {
                // Find middle point
                temp_c = c;
                c = (a+b)/2;
                //c = obj2.roundoff(c);
                obj2.var_a = c;

                cout<<endl<<endl<<endl<<"\n\t\t\t\t "<<counter<<").";
                cout<<endl<<endl<<endl<<"\t Value of c: "<<obj2.var_a;


                obj2.setupeqn();

                cout<<endl<<endl<<endl<<"\t Value of result:  "<<obj2.result;

                // Check if middle point is root
                if (obj2.result == 0.0001)
                    break;

                else
                {
                    if(temp_c==c)
                        break;
                // Decide the side to repeat the steps
                    else
                    {
                        if (obj2.result* this->result < 0)
                        {
                            b = c;
                        }
                        else
                            a = c;
                    }
                }

                counter++;
            }

            cout << "\n\n\n\n\t\t\t\t *** The value of root is : " << c<<" ***"<<endl<<endl<<endl;
            getch();

        }

}

int main()
{
    Bisection a;

    a.geteqn();
    a.showeqn();

    char choice;

    do
    {
    a.getvariable();
    a.setupeqn();
    a.showresult();
    Bisection b = a;
    b.getvariable();
    b.setupeqn();
    b.showresult();

    a.bsmethodcomputn(b);

    cout<<"\n\n\n\t Do you want to continue? (Y/N)";
    cin>>choice;
    }while(choice=='Y'||choice=='y');
    getch();
    return(0);
}

我有这个程序的 Windows 可执行文件,但我不知道如何在 Android 设备上直接运行这个程序。

我实际上创建这个程序是为了与我的同学分享,以便他们可以检查他们的解决方案是否正确。他们不是程序员,他们不知道如何编写、编译或运行程序。因此,他们无法执行cppdroid解码器过程。我也不想给他们我的代码。 但是,我想与他们分享这个程序,以便他们能够成功使用它。现在我无法理解如何实现它。这个程序非常简单,它需要一些输入,经过计算,它给出所需的输出,就是这样。这不是一个非常复杂的程序。我希望有任何可行的方法来完成这件事。

最佳答案

EXE 是 Windows 可执行文件。可执行文件是与操作系统相关的二进制文件,并且在操作系统之间不兼容。您需要针对 ARM(或 ARM64)进行构建,即便如此,Android 中的控制台应用程序也不容易安装或运行,因为它们需要 shell 访问。如果您将其提供给某人,请完全忘记他们将能够在 shell 中运行它(或者您可能认为它会自动呈现为 GUI 应用程序,但事实并非如此)。

如果您对 Android 感兴趣,您应该阅读一本优秀的 Android 书籍并了解有关可用 API 等的更多信息。

Android、iOS、Linux、Windows 是截然不同的生态系统,它们之间的编程也存在显着差异。

Android 或 iOS 中的开发不能在这些设备中内联完成,您需要合适的 IDE。对于 Windows,有 Android Studio 。对于 iOS,您需要一台 Mac 和 Xcode .

简而言之,您目前距离通过大量阅读、学习曲线、投资和调试而试图达到的目标还很远。

获取 good C++ book (从bits/stdc++ include和using namespace std来看,你当前的书不好),然后开始学习Android、Windows、iOS等上的API以及它们运行所需的语言(Java、Objective-C、Swift、Kotlin、[...])。这需要几年,但像大多数糟糕的书那样欺骗自己是没有意义的。

关于android - 如何在 Android 设备上直接将简单的 C++ 程序作为可执行文件运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57017708/

相关文章:

linux - 在 Linux 终端中,破折号是否可以用作临时文件名?

android - 如何在 android 杀死我的应用程序时存储 cookie 信息

c++ - 一个函数将 *this 列表与另一个列表连接起来,并将结果列表存储在新列表中。避免内存泄漏

c++ - 大量文本应该如何存储?

c++ - 通过重复生成所有排列

jsp - 在终端看不到输出

android - 如何以及在哪里进行 Retrofit 调用?

android - Cordova 构建错误 - 无法找到属性 android :font

java - 更改 ListView 中的字体大小 - Android/Eclipse

linux - 在 Linux 中的 Bash 脚本中检测 key 释放