You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言函数计算异常:星-三角转换计算未执行问题求助

Fixing Your Star-Delta Impedance Conversion Code

Let’s walk through the issues in your code and get those calculations working as expected. I see a few key problems that are preventing your conversion from executing properly:

1. Parameter Order is Reversed

Your conversions function is defined to take parameters in the order (float RA, float R1, float R2, float R3), but in main you’re calling it with conversions(R1,R2,R3,RA);. This swaps all the arguments—you’re passing your input resistors as the first three parameters and the uninitialized RA as the last one. That means your calculation is using the wrong values entirely.

2. Pass-by-Value Doesn’t Update the Original Variable

In C, when you pass a variable to a function, it’s passed by value—the function gets a copy of the variable, not the original. So when you modify RA inside conversions, you’re only changing that local copy, not the RA variable in main. The original RA remains uninitialized (holding a garbage value) which is why your final printf shows nothing useful.

3. Missing Return Statement

Your function is declared to return a float, but there’s no return statement inside it. This leads to undefined behavior—your compiler might throw a warning, or the function could return a random value instead of your calculated result.


Fixed Code Options

Option 1: Return the Calculated Value (Simplest for Single Result)

If you just need to compute one resistance value (like RA for Star-to-Delta), rewrite the function to return the result directly:

float starToDeltaRA(float R1, float R2, float R3) {
    // Star to Delta conversion formula for RA
    return ((R1*R2 + R2*R3 + R3*R1) / R3);
}

int main(void){
    float RA, R1, R2, R3;
    
    printf("Please enter values for R1 R2 and R3 separated by a space: ");
    scanf("%f %f %f", &R1, &R2, &R3);
    printf("Input values: %.2f %.2f %.2f\n", R1, R2, R3);

    // Call the function and assign the result to RA
    RA = starToDeltaRA(R1, R2, R3);
    printf("Converted RA: %.2f\n", RA);

    return 0;
}

Option 2: Handle Both Conversions in One Function (Using Pointers)

Since you mentioned needing both Star-to-Delta and Delta-to-Star conversions, here’s how to do it in a single function using pointers to update your result variables in main:

#include <stdio.h>

void impedanceConversion(float* resA, float* resB, float* resC, float R1, float R2, float R3, char convType) {
    if (convType == 'S') {
        // Star to Delta conversion
        float total = R1*R2 + R2*R3 + R3*R1;
        *resA = total / R3;
        *resB = total / R2;
        *resC = total / R1;
    } else if (convType == 'D') {
        // Delta to Star conversion
        float sum = R1 + R2 + R3;
        *resA = (R1*R2) / sum;
        *resB = (R2*R3) / sum;
        *resC = (R3*R1) / sum;
    }
}

int main(void){
    float RA, RB, RC, R1, R2, R3;
    char choice;

    printf("Enter S for Star-to-Delta, D for Delta-to-Star, Q to quit: ");
    scanf(" %c", &choice); // Space before %c skips any leading whitespace

    if (choice != 'Q') {
        printf("Enter values for R1 R2 R3 separated by space: ");
        scanf("%f %f %f", &R1, &R2, &R3);

        impedanceConversion(&RA, &RB, &RC, R1, R2, R3, choice);

        if (choice == 'S') {
            printf("Star-to-Delta Results: RA=%.2f, RB=%.2f, RC=%.2f\n", RA, RB, RC);
        } else if (choice == 'D') {
            printf("Delta-to-Star Results: RA=%.2f, RB=%.2f, RC=%.2f\n", RA, RB, RC);
        }
    }

    return 0;
}

Why This Works

  • Option 1 uses a return value to send the calculated resistance back to main, so you can assign it directly to RA.
  • Option 2 uses pointers (float*) to let the function modify the original variables in main—this is how you pass variables by reference in C. It also adds support for both conversion types as you mentioned in your background.

内容的提问来源于stack exchange,提问作者Dean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:06:29