抑制用户定义的函数输出?

时间:2015-03-01 03:07:26

标签: c

短版本顶部,长版本下面。 我们假设我有一个这样的程序:

#include <stdio.h>
#include <stlib.h>

int funcA();
void funcB();

int main(){
    funcA();
    funcB();
return 0;
}

int funcA(){
    int x=7;
    printf("this is the output of funcA()\n");
    return x;
} 

void funcB(){
    int y,z;
    y=funcA();        //this is me trying to store x from funcA() in y from funcB()
    z=y*5;
    printf("the output of funcB() is %d",z);
}

这是这个小函数的输出:

this is the output of funcA()
this is the output of funcA()
the output of funcB() is 35

我只想说funcA()输出一次和funcB()一次,但是当我尝试使用funcA()的返回值作为funcB()中的变量时,它显示了funcA()再次输出。我怎么阻止这个?提前感谢您,感谢帮助我更好地格式化的有用用户。

长篇: 这是我的计划中的一点让我感到悲伤:

float pcRRV(){                                  //calculate power consumption with rated resistor values
    int i;
    float pr=0,ptemp;                       //pr=power with rated values, ptemp=temporary power variable for for() loop
    for(i=0;i<=RMAX;i++){
            R=RRV[i];
            ptemp=(V*V)/R;                  //power= V^2/R
            pr=ptemp+pr;                    //add all power values (could also have added all resistors then calculated total power, either would require for() loop)
    }
    printf("Given the rated resistor values, the power consumed by the resistors, in series with a %dV source, is %fW.\n",V,pr);
    puts("");
    return pr;                              //need power value for part D
}
float pcARV(){                                  //calculate power consumption with actual resistor values
    int i;
    float pa=0,ptemp;                       //same formate as previous function
    for(i=0;i<=RMAX;i++){
            A=ARV[i];
            ptemp=(V*V)/A;
            pa=ptemp+pa;
    }
    printf("Given the actual resistor values, the power consumed by the resistors, in series with a %dV source, is %fW.\n",V,pa);
    puts("");
    return pa;
}

float powerpercentdif(){                        //calculate percent difference between two power consumption values
    float a,b;
    float powerdif,powerave,percentdif;
    a=pcRRV();                              //use return value of pcRRV() as variable
    b=pcARV();                              //use return value of pcARV() as variable
    powerdif=(a-b);
    powerave=(a+b)/2;
    percentdif=(powerdif/powerave)*100;
    printf("The percent difference between the power consumption given the rated values and given the actual values of the resistors is %f%%.\n",percentdif);
}

所以我有这3个用户定义的函数,pcRRV(),pcARV()和powerpercentdif()。在我的主函数中,我按你看到的顺序调用所有3,输出应该如下:

Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

The percent difference between the power consumption given the rated values and given the actual values of the resistors is 3.015677%.

但实际发生的是:

Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

Given the rated resistor values, the power consumed by the resistors, in series with a 10V source, is 66.240997W.

Given the actual resistor values, the power consumed by the resistors, in series with a 10V source, is 64.273056W.

The percent difference between the power consumption given the rated values and given the actual values of the resistors is 3.015677%.

现在我不希望前两个函数的输出显示两次。最简单的方法是不在main中调用前两个函数,但在第三个函数调用它们时让它们显示输出。但是,由于这是一个类,其中一个要求是我写的3个函数必须都在main函数中调用以显示它们的输出,所以我必须调用所有3,但我不想要输出两次。最后,当我将它们分配给第三个函数中的变量时,我想知道如何抑制第一个和第二个函数的输出,因为我只想在该函数中使用它们的返回值,而不是让它们显示它们的完整输出。感谢您提前获得帮助,当我得到一个超级简单回答的答复时,我将是一个放心/愤怒/沮丧/快乐的组合。 (我已经有一段时间了)谢谢你!

1 个答案:

答案 0 :(得分:0)

你不能。你可以做的是让函数打印结果。您可以将打印部件移动到另一个功能,或者如果需要,可以将其直接放在main(或任何调用这些功能)中。

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

int funcA();
void print_funcA();
int funcB();
void print_funcB();

int main(){
    print_funcA();
    print_funcB();
    return 0;
}

int funcA(){
    int x=7;
    return x;
}

void print_funcA() {
    printf("the output of funcA() is: %d\n", funcA());
}

int funcB(){
    int y,z;
    y=funcA();        //this is me trying to store x from funcA() in y from funcB()
    z=y*5;
    return z;
}

void print_funcB() {
    printf("the output of funcB() is: %d\n", funcB());
}