需要用c语言写一个除法表达式

need to write a division expression in c language

如何用c语言定义这样的表达式:

x+(x^2)/(2*2-1)

其中 x 是实数。

我尝试按原样输入表达式,但这没有用。

问题是我不想使用任何自定义函数,只是循环。

有什么想法吗?

在C语言中,^是按位异或运算符。没有 "power of" 运算符。

所以 C 等价物看起来像这样:

x+(x*x)/(2*2-1)

运算符优先级与数学相同,因此请注意以上等同于

x + ( (x*x) / ((2*2)-1) )

如果你需要一个变量"raise x to the power of y",不幸的是只有pow()函数,它作用于浮点变量,因此有点臃肿和低效。但是,编写自己的整数版本很简单,see this

首先,在代码中,您必须为显示您想要的结果奠定基础。在 C 代码中键入表达式 "as is" 对您没有任何作用。这是一个简单的 c 程序示例,它可以执行您想要的操作:

#include <stdio.h>

int main (void) {
int x, result;
x = 10; // x is 10 for this example, but you may assign any number where both x and result are within the range of integers
result = x + ( x * x ) / ( 2 * 2 - 1 );
printf("The result is : %d", result);
return 0;
}

这里的重点是:

  • a^2在c编程中不代表计算能力,它是a 改为按位异或。
  • a^2可以通过基础数学化简为a*a
  • 要用 c 语言计算一个数字,您需要实际计算它,而不仅仅是 计算它(例如 printf 语句将其打印到 在我的示例中,程序来自 运行 的控制台)