我是编码的新手,只是想知道为什么我的第二个结构“ DtoS”在计算时不返回正确的值?我正在编写代码,将电阻值从星形转换为三角形,反之亦然。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct StarAndDelta
float del1, del2, del3, star1, star2, star3;
};
typedef struct StarAndDelta Struct;
Struct StoD(float R1, float R2, float R3)
{
Struct s;
s.del1 = (((R1*R2)+(R2*R3)+(R3*R1))/R3);
s.del2 = (((R1*R2)+(R2*R3)+(R3*R1))/R2);
s.del3 = (((R1*R2)+(R2*R3)+(R3*R1))/R1);
return(s);
}
Struct DtoS(float RA, float RB, float RC)
{
Struct d;
d.star1 = RA*RB/RA+RB+RC;
d.star2 = RA*RC/RA+RB+RC;
d.star3 = RB*RC/RA+RB+RC;
return(d);
}
float LimitsOfResistors (float R)
{
do
{
if((R<1000 || R>1000000))
{
printf("Please enter a value within the range: ");
scanf("%f", &R);
}
}
while((R<1000 || R>1000000));
return(R);
}
int main(void)
{
char str[2];
do
{
float r, q, s, r1, q1, s1;
Struct delta;
printf("\nPress s for Star \nPress d for Delta \nPress q to exit \n"); t
printf("Please enter an input of what calculation you would like to be carried out: ");
fgets(str, 2, stdin);
if(strcmp(str, "q") == 0)
{
printf("Have a great day. Bye! ");
return(0);
}
if(strcmp(str, "s") == 0)
{
printf("Please enter value a for R1, which is between 1K and 1M: ");
scanf("%f", &r);
r1 = LimitsOfResistors(r);
printf("Please enter value a for R2, which is between 1K and 1M: ");
scanf("%f", &q);
q1 = LimitsOfResistors(q);
printf("Please enter value a for R3, which is between 1K and 1M: ");
scanf("%f", &s);
s1 = LimitsOfResistors(s);
delta = StoD(r1, q1, s1);
printf("The value of Ra is: %.2f", delta.del1);
delta = StoD(r1, q1, s1);
printf(" The value of Rb is: %.2f", delta.del2);
delta = StoD(r1, q1, s1);
printf(" The value of Rc is: %.2f", delta.del3);
}
if(strcmp (str, "d") == 0)
{
printf("Please enter value a for RA, which is between 1K and 1M: ");
scanf("%f", &r);
r1 = LimitsOfResistors(r);
printf("Please enter value a for RB, which is between 1K and 1M: ");
scanf("%f", &q);
q1 = LimitsOfResistors(q);
printf("Please enter value a for RC, which is between 1K and 1M: ");
scanf("%f", &s);
s1 = LimitsOfResistors(s);
delta = DtoS(r1, q1, s1);
printf("The value of R1 is: %f", delta.del1);
delta = DtoS(r1, q1, s1);
printf("The value of R2 is: %f", delta.del2);
delta = DtoS(r1, q1, s1);
printf("The value of R3 is: %f", delta.del3);
}
}
while(strcmp(str, "d") || strcmp(str, "s") );
}
(您(很可能)有一个复制粘贴错误,导致未定义的行为。
在if(strcmp(str, "s") == 0)
的块中,您具有:
printf("The value of Ra is: %.2f", delta.del1);
printf(" The value of Rb is: %.2f", delta.del2);
printf(" The value of Rc is: %.2f", delta.del3);
在if(strcmp (str, "d") == 0)
的块中,您具有:
printf("The value of R1 is: %f", delta.del1);
printf("The value of R2 is: %f", delta.del2);
printf("The value of R3 is: %f", delta.del3);
在if(strcmp (str, "d") == 0)
的块中,您应该打印star
字段:
printf("The value of R1 is: %f", delta.star1);
printf("The value of R2 is: %f", delta.star2);
printf("The value of R3 is: %f", delta.star3);
而且,也不必调用StoD
和DtoS
三次。一次就足够了:
delta = StoD(r1, q1, s1);
printf("The value of Ra is: %.2f", delta.del1);
printf(" The value of Rb is: %.2f", delta.del2);
printf(" The value of Rc is: %.2f", delta.del3);
delta = DtoS(r1, q1, s1);
printf("The value of R1 is: %f", delta.star1);
printf("The value of R2 is: %f", delta.star2);
printf("The value of R3 is: %f", delta.star3);