Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 基于用户输入求解表达式_C_Logic_Expression - Fatal编程技术网

C 基于用户输入求解表达式

C 基于用户输入求解表达式,c,logic,expression,C,Logic,Expression,我正在编写一个程序,要求用户输入两个输入:x和y。这些值用于计算此公式:(3x^2-2xy+5)/(y^2+1)。当我输入值-->2,3时,它会计算一个非常小的负数,如-12586511 #include <stdio.h> int main (void) { int x, y; printf("Enter two integer values (x, y): "); scanf("%d,%d", &x, &y); float

我正在编写一个程序,要求用户输入两个输入:x和y。这些值用于计算此公式:(3x^2-2xy+5)/(y^2+1)。当我输入值-->2,3时,它会计算一个非常小的负数,如-12586511

#include <stdio.h>

int main (void) {

    int x, y;

    printf("Enter two integer values (x, y): ");
    scanf("%d,%d", &x, &y);

    float E = (3 * (x*x) - 2 * (x*y) + 5) / (y*y+1);

    printf("The expression value is: %.2f\n", E);


    return 0;
}
#包括
内部主(空){
int x,y;
printf(“输入两个整数值(x,y):”;
scanf(“%d,%d”,&x,&y);
浮点数E=(3*(x*x)-2*(x*y)+5/(y*y+1);
printf(“表达式值为:%.2f\n”,E);
返回0;
}

真正的快速修复应该是这样的:
float E=(3*(x*x)-2*(x*y)+5/(float)y*y+1)
说明:将分母表达式提升为浮点值,最后,整个表达式生成浮点值

编辑:正如Eisa所说,在
scanf()
函数中,除格式说明符外的所有字符都应包含在输入中-在这种情况下,
介于两个整数之间,因此可能:

(空白)第一个数字(空白),(空白)第二个数字(空白)输入


(…)-可选

如果您想要显式的x,y整数,您应该使用MrDonMatti的方法,或者只需在Float E表达式中的一个数字前面添加一个点。例3

float E = (3. * (x*x) - 2 * (x*y) + 5) / (y*y+1);
这将解决返回0作为结果的问题。
但是得到负值的原因是你应该给程序x,y,就像“2,3”一样。逗号是必不可少的。如果不使用逗号,即:“2 3”作为输入,它会将y设置为未给定或我不知道会破坏公式的-nan数字。

-12586511
肯定是无效数据输入的结果

对于像
“2 3”
“2,3”
“2,3”
这样的输入,将不会设置
y

// Risky code
scanf("%d,%d", &x, &y);
健壮代码检查
scanf()
的结果,以确保正确输入

if (scanf("%d,%d", &x, &y) != 2) {
  puts("Error");
  return -1;
}
最好慷慨大方,并在数字后留出空格:

//           v 
if (scanf("%d ,%d", &x, &y) != 2) {
第二个问题涉及使用
int
math进行除法。OP可能希望方程式使用FP数学

// float E = (3 * (x*x) - 2 * (x*y) + 5) / (y*y+1);
float E = (3 * (x*x) - 2 * (x*y) + 5) / (y*y+1.0f);
// or 
float E = (3.0f * (x*x) - 2 * (x*y) + 5) / (y*y+1.0f);

当其他海报向您显示可能的错误时,让我再加上两美分

如果将整数与浮点运算混合使用,最好尽可能长时间地隔离整数部分。这在这里没有多大区别,但你应该记住这一点

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int x, y;
  float n, d;
  int res;

  printf("Enter two integer values (x, y): ");
  res = scanf("%d,%d", &x, &y);
  if (res != 2) {
    fprintf(stderr, "scanf() did not get the expected input\n");
    exit(EXIT_FAILURE);
  }
  // keep integer arithmetic as long as possible
  // TODO: check for overflows
  n = (3 * (x * x) - 2 * (x * y) + 5);
  d = (y * y + 1);

  float E = n / d;

  printf("The expression value is: %.10f\n", E);

  exit(EXIT_SUCCESS);
}
#包括
#包括
内部主(空)
{
int x,y;
浮动n,d;
国际关系;
printf(“输入两个整数值(x,y):”;
res=scanf(“%d,%d”,&x,&y);
如果(res!=2){
fprintf(stderr,“scanf()未获得预期的输入\n”);
退出(退出失败);
}
//尽可能长地保留整数算术
//TODO:检查溢出
n=(3*(x*x)-2*(x*y)+5);
d=(y*y+1);
浮点数E=n/d;
printf(“表达式值为:%.10f\n”,E);
退出(退出成功);
}

当然也有缺点。最重要的是,整数部分可能会溢出,而完整的浮点计算只会失去一些精度。检查溢出/下溢也会添加一些可能会影响运行时的代码。

“当我输入值时”-->将输入的内容的确切文本发布出来。您首先要做的是整数算术。因为
x
y
int
s,它会将每个操作的结果截断为整数,然后分配给浮点。检查
scanf
的返回值@chux,确定负数来自values@chux ....2,3. 我在原始问题中编辑了这个