<分区>
我是 C 编程的新手,我编写了这段代码,因为我被要求做一些主要使用 printf() 和 scanf() 的事情。我知道可能有更好的方法来处理这个问题,我需要尽快学习,但无论如何,现在这就是我所拥有的:
int add1, add2, exponent, exponent_result, multiplier, parenthese, product, sub, total;
printf("Let's try a slightly more complex calculation, in which we'll use an exponent.\n\n");
printf("Type 5 whole numbers or integers.\n\n");
scanf("%i %i %i %i %i", &add1, &add2, &exponent, &multiplier, &sub);
printf("Out of the numbers you typed, we're going to make this operation: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
exponent_result = pow(add2, exponent);
parenthese = add1 + exponent_result;
product = parenthese * multiplier;
total = (add1 + exponent_result) * multiplier - sub;
printf("Per PEMDAS, the correct order of operation is Parentheses, then Exponents, then Multiplications and Divisions, and finally Additions and Subtractions.\n\n");
printf("Therefore: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
printf("...is equal to: (%i + %i) * %i - %i\n\n", add1, exponent_result, multiplier, sub);
printf("...is equal to: %i * %i - %i\n\n", parenthese, multiplier, sub);
printf("...is equal to: %i - %i\n\n", product, sub);
printf("...is equal to: %i", total);
如果运行此代码,您会发现 exponent_result 的输出是使用 pow() function
计算的, 总是从中减去 1。例如,如果 exponent_result 应该是 5^3
的结果, 结果将是 124
而不是 125
.
我做错了什么?
仅供引用,我在文件的开头有这个。
#include <stdio.h>
#include <math.h>