首页 > 解决方案 > 如何从代码创建一个完整的、可执行的控制台程序

问题描述

我上个月开始学习如何编程(从那以后一直进步很慢),所以我真的不太了解。

我在 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;
}

标签: cvisual-studio-codecodeblocks

解决方案


我在 Linux(Ubuntu 20.04)中编写代码,并使用 VSCode、Sublime 和 Code::Blocks(试图为我找到完美的 IDE)。

一般来说,IDE 或编辑器与编译器是正交的。

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

这意味着您正在交叉编译。也就是说,针对的是与您运行编译器的平台不同的平台。如何做到这一点取决于你的编译器,但它在 GCC 和 Clang 中都是可行的。

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

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

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

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


推荐阅读