- 论坛徽章:
- 0
|
本帖最后由 txgc_wm 于 2013-03-26 11:07 编辑
- #include <stdio.h>
- #include <math.h>
- double xpow(double base, int power)
- {
- double powval = 1.0;
- if(base == 0)
- return 0;
-
- if(power == 0)
- return 1;
- else if(power > 0)
- powval = base * xpow(base, (power - 1));
- else
- powval = 1/base * xpow(base, (power + 1));
-
- return powval;
- }
- int main(void)
- {
- int pow;
- double base, ans;
- printf("Enter a number and an integer power to which the number will be raised. Enter q to quit.\n");
- while(scanf("%lf%d", &base, &pow) == 2) //输入底数和指数
- {
- ans = xpow(base, pow); //调用求幂函数
- printf("\n%.3g to the power %d is %.5g\n", base, pow, ans);
- printf("Enter the next pair of numbers or q to quit.\n");
- }
- printf("\nHope you enjoyed this power trip -- bye!\n");
-
- return 0;
- }
复制代码 以上仅供参考。 |
|