【问题标题】:How to adapt my function so that i can have multiple calculations happen inside the one function如何调整我的函数,以便我可以在一个函数内进行多次计算
【发布时间】:2020-12-19 15:38:01
【问题描述】:

所以我有这个用于电阻转换的代码,我需要在 1 个函数内进行所有计算,但我不确定如何执行此操作,所以这里是公式的屏幕截图。Formulas for calculations。有没有人知道如何做到这一点,任何帮助都会很棒。 我注释掉的可以忽略,因为与函数计算无关。

float conversions (float R1, float R2, float R3)
{
    //Star to Delta conversion
    //RA calculation
    return ((R1*R2+R2*R3+R3*R1)/R3);

    
}


int main(void){
    float R1,R2,R3;
    
    //printf("Please enter an S to convert Star to Delta, a D to convert Delta to Star, and Q to quit ");
    //char x = UserInput();
   // printf ("You selected %c\n", x);
    
    
    printf("Please enter a value for R1 R2 and R3 seperated by a space: ");
    scanf("%f %f %f", &R1, &R2, &R3);
    
    printf("\n %f", conversions (R1, R2, R3));  // test to ensure values were being passed to variables

【问题讨论】:

  • 在你的 c 课程中,你有没有接触过指针、数组或结构体?
  • 添加一个浮点类型的函数来计算这个:R1 = Ra*Rb/Ra+Rb+Rc 并且你改变参数的顺序 (Ra,Rb,Rc) 来计算其他的,也计算 ((R2*R1+R2*R3+R3*R1)/R3)
  • 你的意思是创建一个与我已有的函数分开的新函数,因为它要求所有计算都在同一个函数中完成

标签: c function calculation


【解决方案1】:

这段代码可以帮助你:

float conversions (float R1, float R2,float R3)
{
    return R2*R1+R2*R3+R3*R1;
}

float conversions2(float R1, float R2,float R3)
{
    return R1+R2+R3;         
}

int main(void){
float R1,R2,R3;

float Ra,Rb,Rc;

printf("Please enter a value for R1 R2 and R3 seperated by a space: ");
scanf("%f %f %f", &R1, &R2, &R3);
printf("\n\n");
printf("Please enter a value for Ra Rb and Rc seperated by a space: ");
scanf("%f %f %f", &Ra, &Rb, &Rc);
printf("\n %f", conversions (R1, R2, R3)/R1);  // test to ensure values were being passed to variables

printf("\n %f", conversions (R2, R2, R2)/R2);

printf("\n %f", conversions (R2,R2,R2)/R3);


printf("\n %f", (Ra*Rb)/conversions2 (Ra,Rb,Rc));

printf("\n %f",(Ra*Rc)/ conversions2 (Ra,Rb,Rc));

printf("\n %f",(Rb*Rc)/conversions2 (Ra,Rb,Rc));

}

【讨论】:

    【解决方案2】:

    “调整我的函数,以便我可以在一个函数内进行多项计算”

    在一次调用中提供多种计算选择的步骤:

    • 声明一个int变量:int choice = 0;
    • 可选 -(但推荐)创建一个struct 以包含输入和计算结果值。
    • 使用printf(如您所愿)指示用户该做什么。
    • 使用scanf() 或fgets() 读取输入(假设scanf() 用于其余步骤。)
    • 扫描用户选择:int count = scanf(" %c", &choice);
    • 验证choice == 1
    • 如果choice == 1 继续switch() 语句,例如:

    编辑地址 OP 评论

    这是一个代码示例。 在这个例子中,所有的输入工作都在 main 中完成,但计算都在函数中完成。它使用单个变量(类型struct)来简化函数中传递的参数。):

    void calculations(Calc_s *in);
    
    int main(int argc, char **argv)
    {   
        Calc_s calc = {0};
        int choice;
        printf("Enter cconversion type:\n0 for 'D->S'\n1 for S->D  \n");
        scanf("%d", &choice);
    
        switch(choice) {
            case DELTA_STAR://D->S
                calc.t = DELTA_STAR;
                printf("Enter 3 R values separated by space:\n");
                scanf("%lf %lf %lf", &calc.s.Ra, &calc.s.Rb, &calc.s.Rc);
                calculations(&calc);
                printf("Results:\nR1: %lf\nR2: %lf\nR3: %lf\n", calc.d.R1, calc.d.R2, calc.d.R3);
                break;
            case STAR_DELTA://D->S
                calc.t = STAR_DELTA;
                printf("Enter 3 R values separated by space:\n");
                scanf("%lf %lf %lf", &calc.d.R1, &calc.d.R2, &calc.d.R3);
                calculations(&calc);
                printf("Results:\nRa: %lf\nRb: %lf\nRc: %lf\n", calc.s.Ra, calc.s.Rb, calc.s.Rc);
                break;
        }
    
        
        
        return 0;
    }
    
    void calculations(Calc_s *in)
    {
        double temp = 0.0;
        switch(in->t) {
            case DELTA_STAR:
                temp = (in->s.Ra + in->s.Rb + in->s.Rc);
                in->d.R1 = (in->s.Ra * in->s.Rb)/temp;
                in->d.R2 = (in->s.Ra * in->s.Rc)/temp;
                in->d.R3 = (in->s.Rb * in->s.Rc)/temp;
                break;
            case STAR_DELTA:
                temp = (in->d.R1*in->d.R2 + in->d.R2*in->d.R3 + in->d.R3*in->d.R1);
                in->s.Ra = temp/in->d.R3;
                in->s.Rb = temp/in->d.R2;
                in->s.Rc = temp/in->d.R1;
    
                break;
        }
            
    }            
    

    【讨论】:

    • 好的,谢谢你几个问题。首先,这个 char 变量是否要在 main.xml 中声明?其次,这个 switch 语句是否在我的函数中。
    • @Dean - 有很多方法。这可以全部在您正在调用的 function 中。或者,可以适应移动组件,即在 main 中选择哪个计算,然后将其作为参数传递给函数。例如:double * multi_calc(char choice, choice_s *calcs);。但是在某些时候需要用户输入和计算,并且在某个位置是在 main 中还是全部在被调用函数中,实际上取决于您需要做什么。
    • 我知道我问了很多问题,但是您能否将 switch 语句集成到我尝试过的函数中,即使使用您显示的代码,我仍然在苦苦挣扎。在此先感谢我非常感谢您的帮助。
    • 我根本不想催你,只是想知道你的代码完成了多远?
    • @Dean - 谢谢你也可以通过标记为已接受(空心复选标记)来表达:)
    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2021-11-03
    • 2015-07-15
    • 1970-01-01
    相关资源
    最近更新 更多