在线编译器 C

#include <stdio.h> int main() { double base; // 底数(支持小数,如1.5、-2.0等) int exponent; // 指数(整数,支持正整数/负整数/0) double result = 1.0; // 初始值设为1.0(关键:乘法单位元,满足0次方结果为1) int i; int is_negative_exp = 0; // 标记是否为负指数,0=非负,1=负指数 // 提示用户输入底数和指数 printf("请输入底数:"); scanf("%lf", &base); printf("请输入整数指数(支持正数/负数/0):"); scanf("%d", &exponent); // 步骤1:处理负指数——先标记,再取绝对值用于循环计算 if (exponent < 0) { is_negative_exp = 1; // 标记为负指数 exponent = -exponent; // 取指数绝对值,转为正整数进行累乘 } // 步骤2:循环累乘——计算底数的|指数|次方 for (i = 0; i < exponent; i++) { result *= base; // 累乘运算,初始值1.0不影响首次计算结果 } // 步骤3:负指数兜底——对正指数计算结果取倒数 if (is_negative_exp == 1) { // 合法性校验:避免0的负指数(数学上无意义,会导致除以0错误) if (result == 0.0) { printf("错误:0的负指数无数学意义!\n"); return 1; // 异常退出程序 } result = 1.0 / result; // 负指数等价于正指数结果的倒数 return 0; }
1) This code calculates the power of a base number raised to an integer exponent, handling positive, negative, and zero exponents. It uses a loop for multiplication and includes a check for invalid input (0 raised to a negative power).

2) Hint 1: Check the code's structure carefully after the `if (is_negative_exp == 1)` block. Look for a missing closing brace that is causing a control flow issue.
Hint 2: The `return 0;` statement is currently placed inside an `if` block. Consider where the main function should end to ensure the program always returns a value.