c语言中function的使用

Write a program that will calculate and print bills for the city power company. R means residential use, C means commercial use and I means industrial use. Any other code should be treated as an error. The rates are computed as follows:
R: ₤4.00 plus ₤0.03 per kwh (kilowatt-hour) used
C: ₤40.00 for the first 1000kwh and ₤0.025 for each additional kwh.
I: Rate varies depending on time of usage:
₤50.00 for the first 1000 kwh and ₤0.04 for each additional kwh

The program should display the amount due from the user.
The calculation of the amount should be performed by three functions.

    函数的构成

    function+函数名(参数1,参数2){函数实现;}

    函数名不能是数字开头,可以是字母和下划线;

     å‡½æ•°çš„调用: 函数名();

    作用域

      定义在函数外面的变量,称之为全局变量,整个文档都可以访问。

      定义在函数里面的变量为局部变量,只能在该函数内部访问。

var a=10;

    function aa(){

        var a=20;

        alert(a);

    }

alert(a);

    aa()


     函数是一个数据类型,可以把它赋给变量

var f=function (b){

        return (b=b+1);

    };

  alert(f(5));

  调用的时候使用f(参数)来使用

函数可以访问自身内部的函数

function b(){

       var a=5;

        function bb(){

            alert(a);

        }

        bb();

    }

b();

当内部函数有返回值的时候 调用函数要使用return+函数

function c(){

    var a=10;

    function bb(){

        return a*2;

    }

    return bb();

}

  alert(c())

函数对自身内部函数的调用

function d(a,b){

        function dd(a){

            return a+2

        }

        return c=dd(a)+dd(b);

    }

alert(d(2,3))

函数对其他函数的调用

function add(a,b){

        return a+b;

    }

    function sub(a,b){

        return a-b;

    }

    function bb(x,a,b){

        return  x(a,b);

    }
  alert(bb(sub,2,3))

函数的递归

function cc(a){

        if (a==1){

            return a;

        }else{

            return a*cc(--a);

        }

    }

    alert(cc(4));

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-05-17
#include <stdio.h>

float calcute_R(float r){//计算居民用电费用
return 4+0.03*r;
}
float calcute_C(float c){//计算商业用电费用
//float x;
//x=40+0.025*(c>1000)*(c-1000);
return 40+0.025*(c>1000)*(c-1000);
}
float calcute_I(float i){//计算工业用电费用
return 50+0.04*(i>1000)*(i-1000);
}

int main(){//主函数

float R,C,I;
float feeR,feeC,feeI;
R=1002;
C=804433340.343;
I=342343543.23543;
feeR=calcute_R(R);
feeC=calcute_C(C);
feeI=calcute_I(I);
printf("residential=%f,residential=%f,industrial=%f\n",feeR,feeC,feeI);
;
}
这里面因为对于商业和工业用电在1000以内的没有标明单价,所以假设为商业和工业用电在1000以内时也需要缴纳40/50的电费。
里面有一个地方(c>1000)这个是一个0/1的运算,如果c>1000的话,(c>1000)=1,如果c<1000,(c>1000)=0;

参考资料:在GCC下编译通过,并计算出正确结果。

本回答被提问者采纳
第2个回答  2008-11-04
楼上的已经答的够好的了 我就提一个建议给楼上lele_shu
您的程序有一个警告:希望可以把main()前的int变成void,这个程序没有返回值,呵呵,其实只是美中不足,路过