简体   繁体   English

如何直接在Android设备上以可执行文件的形式运行简单的c ++程序?

[英]How can I run a simple c++ program on android device directly as an executable?

I have made a very simple kind of program in c++ language using code blocks in my Windows (10-64 bit) PC. 我使用Windows(10-64位)PC中的代码块用c ++语言编写了一种非常简单的程序。 This code was compiled by GCC compiler and an executable file of format *.exe is made. 该代码由GCC编译器编译,并生成了* .exe格式的可执行文件。 Now, I want to run that binary build or say executable file on my android device or in any other android device but, I find no way to directly do that. 现在,我想在我的android设备或任何其他android设备上运行该二进制版本或说可执行文件,但是,我找不到直接执行此操作的方法。 I have two apps in my android device that can help me in writing the code, compiling and running/testing it. 我的Android设备中有两个应用程序,可以帮助我编写代码,编译和运行/测试它。 First is CPPDroid and another is Decoder . 首先是CPPDroid ,另一个是Decoder I can use these applications to copy my code into one of them and compiling the code then running it but, this process appears to be very big and risky. 我可以使用这些应用程序将我的代码复制到其中一个程序中,然后编译代码然后运行它,但是此过程似乎非常大且冒险。 In copying or opening the whole code in an editor is risky because I may unknowingly edit or modify the code due to the small touch screen of android devices. 在编辑器中复制或打开整个代码是有风险的,因为由于Android设备的触摸屏较小,我可能会在不知不觉中编辑或修改代码。

I've already tried searching on the internet for the solutions to my problem but was unable to find one. 我已经尝试在Internet上搜索解决我的问题的方法,但是找不到。 Yeah, there are some answers that feature this problem and try to resolve them but, they were either very hard to understand or unhelpful. 是的,有一些答案可以解决此问题,并尝试解决这些问题,但是它们要么很难理解,要么无益。 I am a newbie and I don't know much about working with android studio for making an app to do it. 我是新手,对使用android studio制作应用程序了解得不多。

This is my code: 这是我的代码:

#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);
}

I have the windows executable of this program but I don't know how to directly run this program on an android device. 我有该程序的Windows可执行文件,但我不知道如何在Android设备上直接运行该程序。

I actually created this program to share with my classmates so that they can check their solution that whether it is correct or not. 我实际上创建了此程序以与同学共享,以便他们可以检查自己的解决方案是否正确。 They are not programmers, they do not know how to write, compile or run a program. 他们不是程序员,他们不知道如何编写,编译或运行程序。 So, they cannot do the cppdroid or decoder procedure. 因此,他们无法执行cppdroid解码器过程。 I also don't want to give them my code. 我也不想给他们我的代码。 But, I want to share this program with them so that they can successfully use it. 但是,我想与他们共享此程序,以便他们可以成功使用它。 Now I am unable to understand how to achieve it. 现在我无法理解如何实现它。 This program is very simple it takes some input and after calculations, it gives the required output, that's it. 该程序非常简单,它需要一些输入,经过计算后,它会提供所需的输出,仅此而已。 It is not very complex program. 它不是很复杂的程序。 I hope that there will be any working way to get this thing done. 我希望将有任何可行的方法来完成此任务。

EXE is a Windows executable. EXE是Windows可执行文件。 Executable files are binaries tied to the OS and are not compatible between OSes. 可执行文件是与操作系统绑定的二进制文件,并且在操作系统之间不兼容。 You need to build for ARM (or ARM64), and even so, console apps in Android are not easily installed or run because they require shell access. 您需要针对ARM(或ARM64)进行构建,即使如此,Android中的控制台应用程序也需要外壳程序访问,因此不容易安装或运行。 If you are giving this to someone, forget completely that they will be able to run it in a shell (or perhaps you thought that it will automatically rendered as GUI app, which is not the case). 如果您将其提供给某人,请完全忘记他们将能够在外壳中运行它(或者您可能以为它会自动呈现为GUI应用程序,事实并非如此)。

If you are interested in Android, you should get a good Android book and learn more on the available APIs etc. 如果您对Android感兴趣,则应该获得一本不错的Android书籍,并详细了解可用的API等。

Android, iOS, Linux, Windows are way different ecosystems and programming between them has significant differences. Android,iOS,Linux,Windows是不同的生态系统,并且它们之间的编程存在显着差异。

Development in Android or iOS is not done in these devices inline, you want proper IDEs. 这些设备不能在线进行Android或iOS开发,您需要适当的IDE。 For Windows, there 's Android Studio . 对于Windows,有Android Studio For iOS, you need a Mac and Xcode . 对于iOS,您需要Mac和Xcode

In short, you are currently very far from reaching what one tries to reach with lots of reading, learning curve, investments and debugging. 简而言之,通过大量的阅读,学习曲线,投资和调试,您目前远未达到人们想要达到的目标。

Get a good C++ book (judging from the bits/stdc++ include and the using namespace std your current book is not good), then start learning APIs on Android, Windows, iOS, etc along with the languages that they require to function (Java, Objective-C, Swift, Kotlin, [...]). 取得一本不错的C ++书 (从bits/stdc++ include和当前书中using namespace std判断不好),然后开始学习Android,Windows,iOS等设备上的API及其所需的语言(Java, Objective-C,Swift,Kotlin等。 It will take years , but there is no point in fooling yourself like most bad books attempt to. 这将花费数年时间 ,但是像大多数坏书一样试图欺骗自己是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM