lihuayuli 2025-05-11 11:41 采纳率: 100%
浏览 28
已结题

C语言运行结果显示终端:命令已执行,但失败,退出代码1,是什么情况呀?

终端信息:

PS D:\DEA_code> gcc model3_test.c -o model3_test -IC:\msys64\mingw64\include -LC:\msys64\mingw64\lib -l:libglpk.a -lz -static -lm
PS D:\DEA_code> .\model3_test                                                    
PS D:\DEA_code> .\model3_test

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glpk.h>

#define MAX_LINE_LEN 1024

typedef struct
{
    int year;
    char securities_code[20];
    double financial_expense;       // 阶段一投入1
    double dividend;                // 阶段一投入2
    double operating_cost;          // 阶段二投入1
    double total_asset;             // 阶段二投入2
    double employee;                // 阶段二投入3
    double total_profit;            // 最终产出1
    double operating_income;        // 最终产出2
    double non_current_liabilities; // 中间变量1
    double paid_in_capital;         // 中间变量2
} DMU;

DMU *read_csv(const char *filename, int *count)
{
    // ...(保持原有读取逻辑不变,但确保字段顺序与Python数据一致)...
}

double calculate_efficiency(DMU *dmus, int num_dmus, int target_idx)
{
    DMU *target = &dmus[target_idx];
    glp_prob *lp = glp_create_prob();
    glp_set_obj_dir(lp, GLP_MAX);

    // ================== 定义变量 ==================
    // 变量总数 = v1(2) + v2(3) + w(2) + u(2) = 9
    glp_add_cols(lp, 9); // 列索引1-9

    // 设置变量下界
    for (int i = 1; i <= 9; i++)
    {
        glp_set_col_bnds(lp, i, GLP_LO, 1e-4, 0.0);
    }

    // ================== 目标函数 ==================
    // u0 (最终产出1) -> 列8, u1 (最终产出2) -> 列9
    glp_set_obj_coef(lp, 8, target->operating_income);
    glp_set_obj_coef(lp, 9, target->total_profit);

    // ================== 约束条件 ==================
    int row_count = 0;

    // 约束1: 阶段一投入归一化 (v1_0*financial_expense + v1_1*dividend = 0.5)
    row_count++;
    glp_add_rows(lp, 1);
    glp_set_row_bnds(lp, row_count, GLP_FX, 0.5, 0.5);
    {
        int ind[] = {0, 1, 2}; // v1对应列1-2
        double val[] = {0.0,
                        target->financial_expense,
                        target->dividend};
        glp_set_mat_row(lp, row_count, 2, ind, val);
    }

    // 约束2: 阶段二投入归一化 (v2_0*operating_cost + v2_1*total_asset + v2_2*employee = 0.5)
    row_count++;
    glp_add_rows(lp, 1);
    glp_set_row_bnds(lp, row_count, GLP_FX, 0.5, 0.5);
    {
        int ind[] = {0, 3, 4, 5}; // v2对应列3-5
        double val[] = {0.0,

                        target->operating_cost,
                        target->total_asset,
                        target->employee};
        glp_set_mat_row(lp, row_count, 3, ind, val);
    }

    // 约束3: 所有DMU的约束
    for (int j = 0; j < num_dmus; j++)
    {
        DMU *dmu = &dmus[j];

        // 第一阶段约束: v1*x1 >= w*z
        row_count++;
        glp_add_rows(lp, 1);
        glp_set_row_bnds(lp, row_count, GLP_LO, 0.0, 0.0);
        {
            int ind[] = {0, 1, 2, 6, 7}; // v1(1-2), w(6-7)
            double val[] = {
                0.0,
                dmu->financial_expense,        // v1_0*x1_0
                dmu->dividend,                 // v1_1*x1_1
                -dmu->non_current_liabilities, // -w_0*z_0
                -dmu->paid_in_capital          // -w_1*z_1
            };
            glp_set_mat_row(lp, row_count, 4, ind, val);
        }

        // 第二阶段约束: v2*x2 + w*z >= u*y
        row_count++;
        glp_add_rows(lp, 1);
        glp_set_row_bnds(lp, row_count, GLP_LO, 0.0, 0.0);
        {
            int ind[] = {0, 3, 4, 5, 6, 7, 8, 9}; // v2(3-5), w(6-7), u(8-9)
            double val[] = {
                0.0,
                dmu->operating_cost,          // v2_0*x2_0
                dmu->total_asset,             // v2_1*x2_1
                dmu->employee,                // v2_2*x2_2
                dmu->non_current_liabilities, // w_0*z_0
                dmu->paid_in_capital,         // w_1*z_1
                -dmu->operating_income,       // -u0*y0
                -dmu->total_profit            // -u1*y1
            };
            glp_set_mat_row(lp, row_count, 7, ind, val);
        }
    }

    // ================== 求解 ==================
    glp_simplex(lp, NULL);
    double eff = glp_get_obj_val(lp);
    glp_delete_prob(lp);
    return eff;
}

int main()
{
    int num_dmus;
    DMU *dmus = read_csv("./Data_2023.12.20_1.csv", &num_dmus);
    if (!dmus)
        return 1;

    FILE *fp = fopen("output_model3.csv", "w");
    if (!fp)
    {
        printf("文件打开失败\n");
        free(dmus);
        return 1;
    }

    for (int i = 0; i < num_dmus; i++)
    {
        double eff = calculate_efficiency(dmus, num_dmus, i);
        printf("DMU %s (%d): Efficiency = %.4f\n",
               dmus[i].securities_code, dmus[i].year, eff);
        fprintf(fp, "%s,%d,%.4f\n",
                dmus[i].securities_code, dmus[i].year, eff);
    }

    fclose(fp);
    free(dmus);
    return 0;
}

  • 写回答

4条回答 默认 最新

  • 宝码香车 优质创作者: JavaScript技术领域 2025-05-11 15:07
    关注

    让【宝码香车】来帮你解答,本回答参考DeepSeek编写,并整理提供。
    如果还有疑问可以点击头像关注私信或评论。如果答案让您满意,请采纳、点赞、关注,非常感谢!

    根据您提供的信息,程序编译成功但运行时没有输出且返回退出代码1,这通常表示程序遇到了错误但未正确捕获或输出错误信息。以下是可能的原因和解决方案:

    1. 文件读取失败

    read_csv()函数可能未能正确读取输入文件,导致dmus为NULL,直接返回1。

    // 检查文件读取是否成功
    if (!dmus) {
        printf("Error: Failed to read input file\n");
        return 1;
    }
    

    2. 文件路径问题

    确保./Data_2023.12.20_1.csv文件存在于程序运行目录下。建议添加路径检查:

    #include <sys/stat.h>
    
    int file_exists(const char *filename) {
        struct stat buffer;
        return (stat(filename, &buffer) == 0);
    }
    
    // 在main()中添加检查
    if (!file_exists("./Data_2023.12.20_1.csv")) {
        printf("Error: Input file not found\n");
        return 1;
    }
    

    3. GLPK初始化问题

    GLPK可能初始化失败,建议添加错误检查:

    glp_simplex(lp, NULL);
    int status = glp_get_status(lp);
    if (status != GLP_OPT) {
        printf("GLPK optimization failed with status %d\n", status);
        glp_delete_prob(lp);
        return NAN; // 需要#include <math.h>
    }
    

    4. 完整的调试建议

    修改main函数如下:

    int main() {
        printf("Program started\n"); // 调试输出
        
        if (!file_exists("./Data_2023.12.20_1.csv")) {
            printf("Error: Input file not found\n");
            return 1;
        }
    
        int num_dmus;
        DMU *dmus = read_csv("./Data_2023.12.20_1.csv", &num_dmus);
        if (!dmus) {
            printf("Error: Failed to read CSV data\n");
            return 1;
        }
        printf("Loaded %d DMUs\n", num_dmus); // 调试输出
    
        FILE *fp = fopen("output_model3.csv", "w");
        if (!fp) {
            printf("Error: Cannot create output file\n");
            free(dmus);
            return 1;
        }
    
        for (int i = 0; i < num_dmus; i++) {
            printf("Processing DMU %d...\n", i); // 调试输出
            double eff = calculate_efficiency(dmus, num_dmus, i);
            if (isnan(eff)) {
                printf("Warning: DMU %d efficiency calculation failed\n", i);
            }
            // ...其余输出代码...
        }
    
        fclose(fp);
        free(dmus);
        printf("Program completed successfully\n");
        return 0;
    }
    

    5. 编译建议

    确保使用正确的编译选项(已包含静态链接):

    gcc -Wall -Wextra model3_test.c -o model3_test \
    -IC:\msys64\mingw64\include \
    -LC:\msys64\mingw64\lib \
    -l:libglpk.a -lz -static -lm
    

    6. 其他可能问题

    • 数据文件中存在空行或格式错误
    • 内存不足导致分配失败
    • GLPK库版本不兼容

    建议逐步添加调试输出,定位程序在哪个环节失败。如果仍有问题,可以提供read_csv()函数的完整实现以便进一步分析。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月11日