求大神教我用C语言编程:求X的n次方乘和 用C语言程序设计:求x的n次方的函数。
\u7528C\u8bed\u8a00\u7a0b\u5e8f\u8bbe\u8ba1 \u6c42x\u7684n\u6b21\u65b9\u7684\u51fd\u6570math.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
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
要求a的b次方,就用pow(a,b)即可。
^符号在C中是位异或操作符,不用于求乘方。
long fun(int x,int n)
{ long p=1; int i;
for (i=1;i<=n;i++)
p*=x;
return p;
}
可以把你的问题细致点吗,起码我没看懂,到底是乘还是和还是?
绛旓細鍙互鐢ㄥ湪math.h澶存枃浠朵腑澹版槑鐨刾ow()鍑芥暟姹傦紝渚嬪锛氳姹俛鐨刡娆℃柟锛屽氨鐢╬ow(a,b)鍗冲彲銆俕绗﹀彿鍦–涓槸浣嶅紓鎴栨搷浣滅锛屼笉鐢ㄤ簬姹備箻鏂广
绛旓細int num = value; while(n>=2) { num *= value; n--; }//num灏辨槸鍊间簡
绛旓細鐢╢or璇彞锛歩nclude <stdio.h>void main(){int i,n;float x,s=1;scanf("%f%d",&x,&n);for(i=1;i<=n;i++)s*=x;printf("璁$畻缁撴灉: %f",s);}鐢╳hile璇彞锛歩nclude <stdio.h>void main(){int i,n;float x,s=1;scanf("%f%d",&x,&n);while(i<=n) {s*=x;i++;}prin...
绛旓細int f(int x){ return x*x*x;}
绛旓細include<stdio.h>int main(void){ int i, x, n, iResult = 1.0; printf("%s\n", "璇疯緭鍏ュ簳鏁 x锛"); scanf("%d", &x); printf("%s\n", "璇疯緭鍏ユ寚鏁 n锛"); scanf("%d", &n); for(i=0; i<n; i++) iResult *= x; printf("%d 鐨 %d...
绛旓細include<math.h>#include<stdio.h>void main(){ float x,y; printf("璇疯緭鍏x鐨鍊:\n"); scanf("%f",&x); if (x<-1) { y =2*x +3; } else if(x>=-1&&x<=3) { y = 4 -cos(x); } else { y =x*x; } printf("...
绛旓細void main(){ double x,y,z;printf("璇疯緭鍏x鐨鍊硷細");scanf("%lf",&x);printf("璇疯緭鍏鐨勫硷細");scanf("%lf",&y);if(x>0&&y>0)z=log(x)+log(y);else if(x<0&&y>0)z=sin(x)+sin(y);else if(x<0&&y<0)z=exp(2*x)+exp(3*y);else z=tan(x+y);printf("z=...
绛旓細1銆侀塀锛屽崟鍚戝间紶閫掞紱鏁伴噺鍙橀噺鍨嬪舰鍙傦紝鎺ユ敹瀵硅薄涓 鏁板硷紝瀹冨彧鑳芥帴鏀讹紝涓嶈兘鍙嶉锛涙寚閽堝彉閲忓瀷褰㈠弬锛屾帴鏀跺璞′负鍦板潃锛屽洜涓哄畠鐩存帴闈㈠鐗╃悊鍦板潃锛屼慨鏀瑰唴瀹癸紝灏辨槸鐩存帴淇敼瀹為檯鍊硷紝鎵浠ュ畠鏃㈡槸鎺ユ敹锛屼篃鏄弽棣堛2銆侀塂锛屼簩涔夋э紝璇寸殑鏄紝鍦ㄤ娇鐢紙璋冪敤锛夎繖涓畾涔夋椂锛屼笉浼氫骇鐢熶笌瀹忓畾涔夋湰鎰忎笉鍚鐨勭紪绋缁撴灉銆傛纭殑...
绛旓細鎸夌収棰樼洰瑕佹眰鐢ㄤ簩鍒嗘硶姹傛柟绋嬬殑瀹炴牴鐨凜璇█绋嬪簭濡備笅 include <stdio.h> include <math.h> double f(double x) {return 2*x*x*x - 4*x*x + x - 2;} int main(){ double root, x1 = 0, x2 = 10,y;root = x1+(x2-x1)/2;y = f(root);while(fabs(y) > 1e-6) { if(y...
绛旓細涓ょ鏂瑰紡濡備笅锛岃閲囩撼 include <stdio.h>long long fac(int n){ if(n==1||n==0) return 1; return n * fac(n-1);}int main(){ int x; printf("璇疯緭鍏x:"); scanf("%d",&x); long long sum = 0; for(int i=1;i<=x;i++) sum+=fac(i); printf("鍜屼负锛%d\n",...