如何从代码创建完成的可执行控制台程序

问题描述

我上个月开始学习如何编程(此后一直进展缓慢),所以我对这些东西真的不太了解。

我在Linux(Ubuntu 20.04)中进行编码,并使用VSCode,Sublime和Code :: Blocks(试图为我找到理想的IDE)。我想学习如何创建可执行Windows文件,以便与朋友(他们都是Windows用户)共享我编写的程序。尽管这是一个非常愚蠢的目标,但是学习如何做到这一点在将来肯定会非常有用。我将在下面保留代码,以便您可以随意查看。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Cute little program that allows the user to choose between calculating factorials,finding the real roots
// of quadratic equations and counting the quantity of even numbers inside the sequence they can tipe.
// I'm a native (Brazilian) Portuguese speaker and therefore the program will communicate with the user through (Brazilian) Portuguese language.

// this function finds real roots of quadratic equations
void findRoots(){
    float a,b,c;
    printf("Calculadora de raízes de equações do segundo grau. \n Os parâmetros devem ser observados da fórmula: ax² + bx + c\n");
    printf("\nInsira o parâmetro 'a':\n");
    scanf("%f",&a);
    printf("Insira o parâmetro 'b':\n");
    scanf("%f",&b);
    printf("Insira o parâmetro 'c':\n");
    scanf("%f",&c);
    float discriminante = pow(b,2) - (4 * a * c);
    float sqrtds = sqrt(discriminante);
    float x1 = -b - sqrtds;
    float x2 = -b + sqrtds;
    float den = 2 * a;
  if(a == 0){
        printf("Essa não é uma equação do segundo grau!\n");
    }else {
        if (discriminante < 0){
            printf("A equação não possui raízes reais\n");
        }else if (discriminante == 0) {
            float raiz = -b / den;
            printf("A única raíz da equação é %f\n",raiz);
        } else { 
            float raiz1 = x1 / den;
            float raiz2 = x2 / den;
            printf("O discriminante da equação é %f\n",discriminante);
            printf("Logo,a equação possui 2 raízes reais. \n Uma delas é %f \n A outra é %f\n",raiz1,raiz2);
        }     
    }   

}

//this function calculates factorials
void fatorial(){
    int n;
    int x = 0;
    printf("Calculadora de fatoriais.\n Insira um número até 12: \n");
    scanf("%d",&n);
    int y = n;
    if(n == 0){
        printf("0! é 1\n");
    }else if ( n < 13 && n > 0){
        while ( y  > (x + 1) ){
            x = x + 1;
            n = n * (y - x);
        }
        printf("%d! é %d\n",y,n);
    } else{
        printf("O programa não é capaz de calcular esse fatorial.");

    }
    
    
}

//this function asks the user to enter a sequence and then tells how many even numbers there are in it (it also shows the quantity of odd numbers)
int paridade(){
    printf("Digite o tamanho da sequência:\n");
    int n;
    scanf("%d",&n);
    int par = 0;
    int impar = 0;
    int x = 0;
    int y;
    while(x < n){
        x = x + 1;
        printf("Digite o %dº numero inteiro: \n",x);
        scanf("%d",&y);
        if((y%2) == 0 ){
            par = par + 1;
        }else {
            impar = impar + 1;
        }
    }
    printf("Na sequencia existem %d números pares e %d números ímpares.\n",par,impar);
    return 0;
}

// the main functions allows the user to choose between the funcions mentioned above. It also allows the user to finish the program.
int main(){
    printf("Bem vindo(a).\n");
    int x = 1;
    while(x!= 0){
        
        int opcao;
        printf("\nEscolha entre:\n -Calcular o fatorial de um número (Insira 1)\n -Calcular raízes de uma equação do segundo grau (Insira 2)\n -Contar a quantidade de números pares ou ímpares em uma sequência (Insira 3)\n -Terminar o programa (Insira 4)\n");
        scanf("%d",&opcao);
        switch (opcao)
        {
        case 1:
            fatorial(); //calculates factorial
            break;
        case 2:
            findRoots(); //self-explicative
            break;
        case 3:
            paridade();
            break;
        case 4:
            x = 0; /// closes program
            break;
        default:
            printf("Você selecionou uma opção inválida."); // error message (Invalid option)
            break;
        }
    }
    printf("Obrigado por usar o programa."); // "thnks for using the program"
    return 0;
}

解决方法

我在Linux(Ubuntu 20.04)上进行编码,并使用VSCode,Sublime和Code :: Blocks(试图为我找到理想的IDE)。

IDE或编辑器通常与编译器正交。

我想学习如何创建可执行的Windows文件,以便与朋友(他们都是Windows用户)共享我制作的程序。

这意味着您正在交叉编译。也就是说,目标平台不同于正在运行编译器的平台。具体方法取决于您的编译器,但是在GCC和Clang中都是可行的。

话虽如此,如果您刚刚开始,那么您可能会发现它有点复杂。可能需要安装额外的软件包,进行调整或构建编译器。通常,使用Windows设置VM并在其上编译(和测试!)程序更容易。

在相反的情况下也是如此:在Windows下编译Linux程序。

即使这是一个非常愚蠢的目标,但学习如何做到这一点在将来肯定会非常有用。

当然,但这根本不是一个愚蠢的目标。每天都会交叉编译许多重要的项目。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...