编写一个C程序来计算线性方程组中x和y的解

时间:2018-08-15 20:14:28

标签: c math equations

我的代码有什么问题,我无法获得x和y的值,假设分母不为零。 在大多数情况下,我无法获得x和y的恒定值,该值等于零。 a1x + b1y = c1 a2x + b2y = c2

#include <stdio.h>
#include <math.h>
int main()
{
 /* Write your program code here */
 int a1,b1,c1,a2,b2,c2,x,y;
 printf("Enter the value for a1: \n");
 scanf("%d", &a1);
 printf("Enter the value for b1: \n");
 scanf("%d", &b1);
 printf("Enter the value for c1: \n");
 scanf("%d", &c1);
 printf("Enter the value for a2: \n");
 scanf("%d", &a2);
 printf("Enter the value for b2: \n");
 scanf("%d", &b2);
 printf("Enter the value for c2: \n");
 scanf("%d", &c2);
 x=((b2*c1)-(b1*c2))/((a1*b2)-(a2*b1));
 printf("x is %d\n", x);
 y=((a1*c2)-(a2*c1))/((a1*b2)-(a2*b1));
 printf("y is %d\n", y);
 return 0;
}

1 个答案:

答案 0 :(得分:3)

如果要获得x和y的准确/精确结果,也许需要将x和y变量的类型从int更改为float或double。

使用int类型不会显示出与float / double类型相同的结果,因为它不会显示小数点后的数字,而是将其截断为零,从而产生最大的整数,该整数小于大于零的数字的浮点数,对于小于零的数字,最小的整数要大于浮点数。