求 用调用自定义函数的方式计算x的n次幂的C语言程序 用C语言程序设计 求x的n次方的函数

\u7528C\u8bed\u8a00\u7a0b\u5e8f\u8bbe\u8ba1\uff1a\u6c42x\u7684n\u6b21\u65b9\u7684\u51fd\u6570\u3002

double pow(double x, double y);
pow()\u7528\u6765\u8ba1\u7b97\u4ee5x \u4e3a\u5e95\u7684 y \u6b21\u65b9\u503c\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u8fd4\u56de
\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u60c5\u51b5\uff1a
\u5982\u679c\u5e95\u6570 x \u4e3a\u8d1f\u6570\u5e76\u4e14\u6307\u6570 y \u4e0d\u662f\u6574\u6570\uff0c\u5c06\u4f1a\u5bfc\u81f4 domain error \u9519\u8bef\u3002
\u5982\u679c\u5e95\u6570 x \u548c\u6307\u6570 y \u90fd\u662f 0\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4 domain error \u9519\u8bef\uff0c\u4e5f\u53ef\u80fd\u6ca1\u6709\uff1b\u8fd9\u8ddf\u5e93\u7684\u5b9e\u73b0\u6709\u5173\u3002
\u5982\u679c\u5e95\u6570 x \u662f 0\uff0c\u6307\u6570 y \u662f\u8d1f\u6570\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4 domain error \u6216 pole error \u9519\u8bef\uff0c\u4e5f\u53ef\u80fd\u6ca1\u6709\uff1b\u8fd9\u8ddf\u5e93\u7684\u5b9e\u73b0\u6709\u5173\u3002
\u5982\u679c\u8fd4\u56de\u503c ret \u592a\u5927\u6216\u8005\u592a\u5c0f\uff0c\u5c06\u4f1a\u5bfc\u81f4 range error \u9519\u8bef\u3002
\u9519\u8bef\u4ee3\u7801\uff1a
\u5982\u679c\u53d1\u751f domain error \u9519\u8bef\uff0c\u90a3\u4e48\u5168\u5c40\u53d8\u91cf errno \u5c06\u88ab\u8bbe\u7f6e\u4e3a EDOM\uff1b
\u5982\u679c\u53d1\u751f pole error \u6216 range error \u9519\u8bef\uff0c\u90a3\u4e48\u5168\u5c40\u53d8\u91cf errno \u5c06\u88ab\u8bbe\u7f6e\u4e3a ERANGE\u3002
\u6ce8\u610f\uff0c\u4f7f\u7528 GCC \u7f16\u8bd1\u65f6\u8bf7\u52a0\u5165-lm\u3002
#include
#include
intmain()
{ printf("7 ^ 3 = %f\n",pow(7.0,3.0));
printf("4.73 ^ 12 = %f\n",pow(4.73,12.0));
printf("32.01 ^ 1.54 = %f\n",pow(32.01,1.54));
return0;}
\u8f93\u51fa\u7ed3\u679c\uff1a
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691

math.h\u5e93\u91cc\u6709\u8fd9\u4e2a\u51fd\u6570\uff0cpow(x,n)\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5df1\u7528\u5faa\u73af\u6765\u5199

long fun(int x,int n)
{ long p=1; int i;
for (i=1;i<=n;i++)
p*=x;
return p;
}

\u5728\u4e3b\u51fd\u6570main()\u4e2d\uff0c\u76f4\u63a5\u8c03\u7528fun\u51fd\u6570\u5373\u53ef

#include <stdio.h>
float xn(int x,int n)
{
    int i;
    float s=1;
    for(i=1;i<=n;i++)
    {
        s=s*x;
    }
    return s;
}
int main()
{
    int x,n;
    float result;
    printf("please input x:");
    scanf("%d",&x);
    printf("please input n:");
    scanf("%d",&n);
    result=xn(x,n);
    printf("x^n=%.0f
",result);
    return 0;
}


一楼的回答是对的,但是有一个错误,在第六行应该是i++。

扩展阅读:调用自定义函数使用 ... java调用自定义函数 ... 怎么调用自定义函数py ... python调用自定义函数 ... 自定义函数的调用方法 ... vba如何调用自定义函数 ... c自定义函数怎么调用 ... c语言自定义函数例子 ... c语言自定义函数怎么声明 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网