如何改造电阻转换函数以在单个函数内完成多计算?
Got it, let's expand your code to handle both conversion directions in a single function. The key here is that we need to return three resistance values (not just one) for either conversion, so we'll use a struct to bundle those results. We'll also add a parameter to specify which conversion type we want to run.
Step 1: Define a Struct for Conversion Results
First, create a struct to hold the three output resistances—this makes it easy to return all values at once:
#include <stdio.h> #include <stdlib.h> // For exit() in error handling // Struct to hold the three resistance values from conversion typedef struct { float val1; float val2; float val3; } ResistanceResult; // Enum to specify conversion direction (makes code more readable) typedef enum { STAR_TO_DELTA, DELTA_TO_STAR } ConversionType;
Step 2: Implement the Combined Conversion Function
Now update your conversions function to accept the conversion type as a parameter, handle both calculation sets, and return the struct. We'll also add basic error checking to avoid division by zero:
ResistanceResult conversions(ConversionType type, float r1, float r2, float r3) { ResistanceResult result; float sum; switch(type) { case STAR_TO_DELTA: // Star to Delta formulas: // RA = (R1R2 + R2R3 + R3R1)/R3, RB = same sum / R1, RC = same sum / R2 sum = r1*r2 + r2*r3 + r3*r1; if (r3 == 0 || r1 == 0 || r2 == 0) { printf("Error: Star resistances cannot be zero!\n"); exit(EXIT_FAILURE); } result.val1 = sum / r3; // RA result.val2 = sum / r1; // RB result.val3 = sum / r2; // RC break; case DELTA_TO_STAR: // Delta to Star formulas: // R1 = (RARB)/(RA+RB+RC), R2 = (RBRC)/(sum), R3 = (RCRA)/(sum) sum = r1 + r2 + r3; if (sum == 0) { printf("Error: Sum of Delta resistances cannot be zero!\n"); exit(EXIT_FAILURE); } result.val1 = (r1*r2) / sum; // R1 result.val2 = (r2*r3) / sum; // R2 result.val3 = (r3*r1) / sum; // R3 break; default: printf("Error: Invalid conversion type!\n"); exit(EXIT_FAILURE); } return result; }
Step 3: Update Main Function to Handle User Input
Now modify main() to let the user choose the conversion direction, input their resistances, and display the results:
int main(void) { char choice; float r1, r2, r3; ResistanceResult output; printf("Enter S for Star-to-Delta, D for Delta-to-Star, Q to quit: "); scanf(" %c", &choice); // Space before %c skips whitespace if (choice == 'Q' || choice == 'q') { printf("Exiting program.\n"); return 0; } printf("Enter three resistance values separated by spaces: "); scanf("%f %f %f", &r1, &r2, &r3); switch(choice) { case 'S': case 's': output = conversions(STAR_TO_DELTA, r1, r2, r3); printf("\nStar-to-Delta Results:\n"); printf("RA = %.2f, RB = %.2f, RC = %.2f\n", output.val1, output.val2, output.val3); break; case 'D': case 'd': output = conversions(DELTA_TO_STAR, r1, r2, r3); printf("\nDelta-to-Star Results:\n"); printf("R1 = %.2f, R2 = %.2f, R3 = %.2f\n", output.val1, output.val2, output.val3); break; default: printf("Invalid choice! Exiting.\n"); return 1; } return 0; }
Key Improvements Explained
- Struct for Results: Lets us return all three calculated resistances instead of just one, which is essential for both conversion types.
- Enum for Conversion Type: Makes the code more readable and avoids magic numbers/strings to specify conversion direction.
- Error Handling: Checks for division-by-zero scenarios (like a zero resistance in star configuration, or all delta resistances summing to zero) to prevent crashes.
- User-Friendly Input: Lets users choose the conversion type and properly displays all results.
内容的提问来源于stack exchange,提问作者Dean

