在 C 语言中计算幂和指数

CBeginner
立即练习

简介

在本实验中,你将学习如何在 C 编程中计算幂和指数。本实验涵盖两个主要步骤:从用户那里读取底数和指数值,然后使用手动循环或内置的 pow() 函数来计算幂。完成本实验后,你将对如何在 C 中执行幂计算有扎实的理解,这是许多数学和科学应用中的基本操作。

读取底数和指数

在这一步中,你将学习如何在 C 程序中读取用于计算幂的底数和指数值。我们将创建一个简单的程序,提示用户输入这些值。

首先,让我们在 ~/project 目录中创建一个新的 C 文件:

cd ~/project
nano power_calculation.c

现在,输入以下代码来读取底数和指数:

#include <stdio.h>

int main() {
    int base, exponent;

    // 提示用户输入底数
    printf("Enter the base number: ");
    scanf("%d", &base);

    // 提示用户输入指数
    printf("Enter the exponent: ");
    scanf("%d", &exponent);

    // 打印输入的值
    printf("Base: %d\n", base);
    printf("Exponent: %d\n", exponent);

    return 0;
}

让我们编译并运行该程序:

gcc power_calculation.c -o power_calculation
./power_calculation

示例输出:

Enter the base number: 5
Enter the exponent: 3
Base: 5
Exponent: 3

代码解释:

  • scanf() 函数用于从用户读取整数输入
  • %d 是整数的格式说明符
  • &base&exponent 传递将存储输入值的内存地址
  • 该程序只是读取并显示底数和指数值

这一步通过首先从用户获取必要的输入,为计算幂奠定了基础。

使用循环或 pow() 函数

在这一步中,你将学习在 C 语言中计算幂的两种方法:使用手动循环和使用数学库中的内置 pow() 函数。

首先,让我们修改之前的 power_calculation.c 文件来实现幂的计算:

cd ~/project
nano power_calculation.c

方法一:使用循环

#include <stdio.h>

int calculate_power_loop(int base, int exponent) {
    int result = 1;
    for (int i = 0; i < exponent; i++) {
        result *= base;
    }
    return result;
}

int main() {
    int base, exponent;

    printf("Enter the base number: ");
    scanf("%d", &base);

    printf("Enter the exponent: ");
    scanf("%d", &exponent);

    int power_result = calculate_power_loop(base, exponent);

    printf("%d raised to the power of %d is: %d\n", base, exponent, power_result);

    return 0;
}

方法二:使用 pow() 函数

#include <stdio.h>
#include <math.h>

int main() {
    int base, exponent;

    printf("Enter the base number: ");
    scanf("%d", &base);

    printf("Enter the exponent: ");
    scanf("%d", &exponent);

    // 注意:pow() 返回一个双精度浮点数,所以我们进行强制类型转换为整数
    int power_result = (int)pow(base, exponent);

    printf("%d raised to the power of %d is: %d\n", base, exponent, power_result);

    return 0;
}

使用数学库编译程序:

gcc power_calculation.c -o power_calculation -lm

示例输出:

Enter the base number: 2
Enter the exponent: 3
2的3次方是:8

代码解释:

  • 循环方法手动将底数自身乘以 exponent
  • math.h 中的 pow() 函数提供了一个内置的幂计算功能
  • 编译时需要 -lm 标志来链接数学库
  • 我们将 pow() 的结果强制转换为 int 以匹配我们的整数计算

打印结果

在这一步中,你将增强幂计算程序,以提供更详细和格式化的输出,展示在 C 语言中呈现结果的不同方式。

让我们修改 power_calculation.c 文件:

cd ~/project
nano power_calculation.c

添加以下完整代码:

#include <stdio.h>
#include <math.h>

void print_power_result(int base, int exponent, int result) {
    // 基本打印格式
    printf("基本结果:%d^%d = %d\n", base, exponent, result);

    // 带对齐的格式化打印
    printf("格式化结果:%2d 的 %2d 次幂等于 %5d\n",
           base, exponent, result);

    // 较大数字的科学记数法
    printf("科学记数法:%d^%d = %.2e\n", base, exponent, pow(base, exponent));
}

int calculate_power_loop(int base, int exponent) {
    int result = 1;
    for (int i = 0; i < exponent; i++) {
        result *= base;
    }
    return result;
}

int main() {
    int base, exponent;

    printf("输入底数:");
    scanf("%d", &base);

    printf("输入指数:");
    scanf("%d", &exponent);

    int power_result = calculate_power_loop(base, exponent);

    // 调用函数打印结果
    print_power_result(base, exponent, power_result);

    return 0;
}

编译程序:

gcc power_calculation.c -o power_calculation -lm

运行程序:

./power_calculation

示例输出:

输入底数:5
输入指数:3
基本结果:5^3 = 125
格式化结果: 5 的  3 次幂等于   125
科学记数法:5^3 = 1.25e+02

代码解释:

  • print_power_result() 展示了多种打印格式
  • 基本打印显示简单计算
  • 格式化打印使用宽度说明符进行对齐
  • 科学记数法对较大数字很有用
  • %.2e 以带两位小数的科学格式显示数字

总结

在本实验中,你学习了如何从用户那里读取底数和指数值,然后使用两种不同的方法计算幂:手动循环和内置的 pow() 函数。你实现了基于循环的幂计算函数和 pow() 函数调用,然后比较了结果。这使你能够理解在 C 编程中计算幂和指数的不同方法。